*/ final class RemoveClassNameMethodUsagesFixer extends AbstractFixer { public function getDefinition(): FixerDefinitionInterface { return new FixerDefinition( 'Converts Yii2 `BaseObject::className()` method usage into `::class` keyword.', [ new CodeSample( 'isTokenKindFound(T_STRING); } public function isRisky(): bool { return true; } protected function applyFix(SplFileInfo $file, Tokens $tokens): void { for ($index = $tokens->count() - 4; $index > 0; --$index) { $candidate = $this->getReplaceCandidate($tokens, $index); if ($candidate === null) { continue; } $this->fixClassNameMethodUsage( $tokens, $index, $candidate[0], // brace open $candidate[1], // brace close ); } } private function getReplaceCandidate(Tokens $tokens, int $index): ?array { if (!$tokens[$index]->isGivenKind(T_STRING)) { return null; } $braceOpenIndex = $tokens->getNextMeaningfulToken($index); if (!$tokens[$braceOpenIndex]->equals('(')) { return null; } $braceCloseIndex = $tokens->getNextMeaningfulToken($braceOpenIndex); if (!$tokens[$braceCloseIndex]->equals(')')) { return null; } $doubleColon = $tokens->getPrevMeaningfulToken($index); if (!$tokens[$doubleColon]->isGivenKind([T_DOUBLE_COLON])) { return null; } $methodName = $tokens[$index]->getContent(); if ($methodName !== 'className') { return null; } return [ $braceOpenIndex, $braceCloseIndex, ]; } private function fixClassNameMethodUsage( Tokens $tokens, int $index, int $braceOpenIndex, int $braceCloseIndex ): void { $tokens->clearTokenAndMergeSurroundingWhitespace($braceCloseIndex); $tokens->clearTokenAndMergeSurroundingWhitespace($braceOpenIndex); $tokens->clearAt($index); $tokens->insertAt($index, new Token([CT::T_CLASS_CONSTANT, 'class'])); } }