Skip to content

Commit 30a0d30

Browse files
authored
Update PhpUnitNoUselessReturnFixer (#251)
1 parent 8a895d8 commit 30a0d30

File tree

1 file changed

+4
-14
lines changed

1 file changed

+4
-14
lines changed

src/Fixer/PhpUnitNoUselessReturnFixer.php

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PhpCsFixer\FixerDefinition\FixerDefinition;
99
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
1010
use PhpCsFixer\Indicator\PhpUnitTestCaseIndicator;
11+
use PhpCsFixer\Tokenizer\Analyzer\FunctionsAnalyzer;
1112
use PhpCsFixer\Tokenizer\Tokens;
1213
use PhpCsFixer\Utils;
1314
use PhpCsFixerCustomFixers\TokenRemover;
@@ -69,6 +70,8 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
6970

7071
private function removeUselessReturns(Tokens $tokens, int $startIndex, int $endIndex): void
7172
{
73+
$functionsAnalyzer = new FunctionsAnalyzer();
74+
7275
for ($index = $startIndex; $index < $endIndex; $index++) {
7376
if (!$tokens[$index]->equalsAny(self::FUNCTION_TOKENS, false)) {
7477
continue;
@@ -80,7 +83,7 @@ private function removeUselessReturns(Tokens $tokens, int $startIndex, int $endI
8083
continue;
8184
}
8285

83-
if (!$this->isTheSameClassCall($tokens, $index)) {
86+
if (!$functionsAnalyzer->isTheSameClassCall($tokens, $index)) {
8487
continue;
8588
}
8689

@@ -108,17 +111,4 @@ private function removeUselessReturns(Tokens $tokens, int $startIndex, int $endI
108111
TokenRemover::removeWithLinesIfPossible($tokens, $semicolonAfterReturnIndex);
109112
}
110113
}
111-
112-
private function isTheSameClassCall(Tokens $tokens, int $index): bool
113-
{
114-
/** @var int $operatorIndex */
115-
$operatorIndex = $tokens->getPrevMeaningfulToken($index);
116-
117-
/** @var int $referenceIndex */
118-
$referenceIndex = $tokens->getPrevMeaningfulToken($operatorIndex);
119-
120-
return $tokens[$operatorIndex]->isGivenKind(T_OBJECT_OPERATOR) && $tokens[$referenceIndex]->equals([T_VARIABLE, '$this'], false)
121-
|| $tokens[$operatorIndex]->isGivenKind(T_DOUBLE_COLON) && $tokens[$referenceIndex]->equals([T_STRING, 'self'], false)
122-
|| $tokens[$operatorIndex]->isGivenKind(T_DOUBLE_COLON) && $tokens[$referenceIndex]->isGivenKind(T_STATIC);
123-
}
124114
}

0 commit comments

Comments
 (0)