88use PhpCsFixer \FixerDefinition \FixerDefinition ;
99use PhpCsFixer \FixerDefinition \FixerDefinitionInterface ;
1010use PhpCsFixer \Indicator \PhpUnitTestCaseIndicator ;
11+ use PhpCsFixer \Tokenizer \Analyzer \FunctionsAnalyzer ;
1112use PhpCsFixer \Tokenizer \Tokens ;
1213use PhpCsFixer \Utils ;
1314use 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