Skip to content

Commit ef29b5a

Browse files
kubawerloskeradus
authored andcommitted
PhpUnitDedicateAssertFixer - fix for count with additional operations
1 parent d120ba2 commit ef29b5a

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/Fixer/PhpUnit/PhpUnitDedicateAssertFixer.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ private function fixAssertSameEquals(Tokens $tokens, array $assertCall)
352352
$defaultNamespaceTokenIndex = false;
353353
}
354354

355-
if (!$tokens[$countCallIndex]->isGivenKind(T_STRING) || 'count' !== strtolower($tokens[$countCallIndex]->getContent())) {
355+
if (!$tokens[$countCallIndex]->equals([T_STRING, 'count'], false)) {
356356
return; // not a call to "count"
357357
}
358358

@@ -362,12 +362,19 @@ private function fixAssertSameEquals(Tokens $tokens, array $assertCall)
362362
return;
363363
}
364364

365+
$countCallCloseBraceIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $countCallOpenBraceIndex);
366+
367+
$afterCountCallCloseBraceIndex = $tokens->getNextMeaningfulToken($countCallCloseBraceIndex);
368+
if (!$tokens[$afterCountCallCloseBraceIndex]->equalsAny([')', ','])) {
369+
return;
370+
}
371+
365372
$this->removeFunctionCall(
366373
$tokens,
367374
$defaultNamespaceTokenIndex,
368375
$countCallIndex,
369376
$countCallOpenBraceIndex,
370-
$tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $countCallOpenBraceIndex)
377+
$countCallCloseBraceIndex
371378
);
372379

373380
$tokens[$assertCall['index']] = new Token([

tests/Fixer/PhpUnit/PhpUnitDedicateAssertFixerTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,11 @@ public function provideTestAssertCountCases()
474474
$this->test(); // $this->assertSame($b, count($a));
475475
',
476476
],
477+
'do not fix 7' => [
478+
'<?php
479+
$this->assertSame(2, count($array) - 1);
480+
',
481+
],
477482
];
478483
}
479484

0 commit comments

Comments
 (0)