* 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\Phpdoc; use PhpCsFixer\AbstractFixer; use PhpCsFixer\DocBlock\DocBlock; use PhpCsFixer\DocBlock\Line; use PhpCsFixer\DocBlock\TypeExpression; use PhpCsFixer\FixerDefinition\CodeSample; use PhpCsFixer\FixerDefinition\FixerDefinition; use PhpCsFixer\FixerDefinition\FixerDefinitionInterface; use PhpCsFixer\Preg; use PhpCsFixer\Tokenizer\Token; use PhpCsFixer\Tokenizer\Tokens; /** * @author Graham Campbell * @author Dave van der Brugge */ final class PhpdocVarWithoutNameFixer extends AbstractFixer { public function getDefinition(): FixerDefinitionInterface { return new FixerDefinition( '`@var` and `@type` annotations of classy properties should not contain the name.', [new CodeSample('isTokenKindFound(T_DOC_COMMENT) && $tokens->isAnyTokenKindsFound([T_CLASS, T_TRAIT]); } protected function applyFix(\SplFileInfo $file, Tokens $tokens): void { foreach ($tokens as $index => $token) { if (!$token->isGivenKind(T_DOC_COMMENT)) { continue; } $nextIndex = $tokens->getNextMeaningfulToken($index); if (null === $nextIndex) { continue; } // For people writing "static public $foo" instead of "public static $foo" if ($tokens[$nextIndex]->isGivenKind(T_STATIC)) { $nextIndex = $tokens->getNextMeaningfulToken($nextIndex); } // We want only doc blocks that are for properties and thus have specified access modifiers next $propertyModifierKinds = [T_PRIVATE, T_PROTECTED, T_PUBLIC, T_VAR]; if (\defined('T_READONLY')) { // @TODO: drop condition when PHP 8.1+ is required $propertyModifierKinds[] = T_READONLY; } if (!$tokens[$nextIndex]->isGivenKind($propertyModifierKinds)) { continue; } $doc = new DocBlock($token->getContent()); $firstLevelLines = $this->getFirstLevelLines($doc); $annotations = $doc->getAnnotationsOfType(['type', 'var']); foreach ($annotations as $annotation) { if (isset($firstLevelLines[$annotation->getStart()])) { $this->fixLine($firstLevelLines[$annotation->getStart()]); } } $tokens[$index] = new Token([T_DOC_COMMENT, $doc->getContent()]); } } private function fixLine(Line $line): void { Preg::matchAll('/ \$'.TypeExpression::REGEX_IDENTIFIER.'(?getContent(), $matches); if (isset($matches[0])) { foreach ($matches[0] as $match) { $line->setContent(str_replace($match, '', $line->getContent())); } } } /** * @return Line[] */ private function getFirstLevelLines(DocBlock $docBlock): array { $nested = 0; $lines = $docBlock->getLines(); foreach ($lines as $index => $line) { $content = $line->getContent(); if (Preg::match('/\s*\*\s*}$/', $content)) { --$nested; } if ($nested > 0) { unset($lines[$index]); } if (Preg::match('/\s\{$/', $content)) { ++$nested; } } return $lines; } }