Skip to content
Permalink
Browse files

Update PhpUnitNoUselessReturnFixer (#251)

  • Loading branch information
kubawerlos committed Mar 15, 2020
1 parent 8a895d8 commit 30a0d30c48f992cc2c0ad3efc2877cb66199217e
Showing with 4 additions and 14 deletions.
  1. +4 −14 src/Fixer/PhpUnitNoUselessReturnFixer.php
@@ -8,6 +8,7 @@
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\Indicator\PhpUnitTestCaseIndicator;
use PhpCsFixer\Tokenizer\Analyzer\FunctionsAnalyzer;
use PhpCsFixer\Tokenizer\Tokens;
use PhpCsFixer\Utils;
use PhpCsFixerCustomFixers\TokenRemover;
@@ -69,6 +70,8 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void

private function removeUselessReturns(Tokens $tokens, int $startIndex, int $endIndex): void
{
$functionsAnalyzer = new FunctionsAnalyzer();

for ($index = $startIndex; $index < $endIndex; $index++) {
if (!$tokens[$index]->equalsAny(self::FUNCTION_TOKENS, false)) {
continue;
@@ -80,7 +83,7 @@ private function removeUselessReturns(Tokens $tokens, int $startIndex, int $endI
continue;
}

if (!$this->isTheSameClassCall($tokens, $index)) {
if (!$functionsAnalyzer->isTheSameClassCall($tokens, $index)) {
continue;
}

@@ -108,17 +111,4 @@ private function removeUselessReturns(Tokens $tokens, int $startIndex, int $endI
TokenRemover::removeWithLinesIfPossible($tokens, $semicolonAfterReturnIndex);
}
}

private function isTheSameClassCall(Tokens $tokens, int $index): bool
{
/** @var int $operatorIndex */
$operatorIndex = $tokens->getPrevMeaningfulToken($index);

/** @var int $referenceIndex */
$referenceIndex = $tokens->getPrevMeaningfulToken($operatorIndex);

return $tokens[$operatorIndex]->isGivenKind(T_OBJECT_OPERATOR) && $tokens[$referenceIndex]->equals([T_VARIABLE, '$this'], false)
|| $tokens[$operatorIndex]->isGivenKind(T_DOUBLE_COLON) && $tokens[$referenceIndex]->equals([T_STRING, 'self'], false)
|| $tokens[$operatorIndex]->isGivenKind(T_DOUBLE_COLON) && $tokens[$referenceIndex]->isGivenKind(T_STATIC);
}
}

0 comments on commit 30a0d30

Please sign in to comment.
You can’t perform that action at this time.