* Dariusz Rumiński * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Fixer\LanguageConstruct; use PhpCsFixer\AbstractFixer; use PhpCsFixer\Fixer\ExperimentalFixerInterface; use PhpCsFixer\FixerDefinition\CodeSample; use PhpCsFixer\FixerDefinition\FixerDefinition; use PhpCsFixer\FixerDefinition\FixerDefinitionInterface; use PhpCsFixer\Tokenizer\Tokens; /** * @author Dariusz Rumiński */ final class ClassKeywordFixer extends AbstractFixer implements ExperimentalFixerInterface { public function getDefinition(): FixerDefinitionInterface { return new FixerDefinition( 'Converts FQCN strings to `*::class` keywords.', [ new CodeSample( 'count() - 1; $index >= 0; --$index) { $token = $tokens[$index]; if ($token->isGivenKind(T_CONSTANT_ENCAPSED_STRING)) { $name = substr($token->getContent(), 1, -1); $name = ltrim($name, '\\'); $name = str_replace('\\\\', '\\', $name); if ($this->exists($name)) { $substitution = Tokens::fromCode("clearRange(0, 2); $substitution->clearAt($substitution->getSize() - 1); $substitution->clearEmptyTokens(); $tokens->clearAt($index); $tokens->insertAt($index, $substitution); } } } } private function exists(string $name): bool { if (class_exists($name) || interface_exists($name) || trait_exists($name)) { $rc = new \ReflectionClass($name); return $rc->getName() === $name; } return false; } }