|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Symplify\CodingStandard\Fixer\Annotation; |
| 6 | + |
| 7 | +use Nette\Utils\Strings; |
| 8 | +use PhpCsFixer\FixerDefinition\FixerDefinition; |
| 9 | +use PhpCsFixer\FixerDefinition\FixerDefinitionInterface; |
| 10 | +use PhpCsFixer\Tokenizer\Token; |
| 11 | +use PhpCsFixer\Tokenizer\Tokens; |
| 12 | +use SplFileInfo; |
| 13 | +use Symplify\CodingStandard\Fixer\AbstractSymplifyFixer; |
| 14 | +use Symplify\CodingStandard\Fixer\Naming\MethodNameResolver; |
| 15 | +use Symplify\CodingStandard\TokenRunner\Traverser\TokenReverser; |
| 16 | + |
| 17 | +/** |
| 18 | + * @see \Symplify\CodingStandard\Tests\Fixer\Annotation\RemoveSetterGetterDocblockFixer\RemoveSetterGetterDocblockFixerTest |
| 19 | + */ |
| 20 | +final class RemoveSetterGetterDocblockFixer extends AbstractSymplifyFixer |
| 21 | +{ |
| 22 | + /** |
| 23 | + * @var string |
| 24 | + */ |
| 25 | + private const ERROR_MESSAGE = 'Remove setter and getter only docblocks'; |
| 26 | + |
| 27 | + private readonly MethodNameResolver $methodNameResolver; |
| 28 | + |
| 29 | + public function __construct( |
| 30 | + private readonly TokenReverser $tokenReverser |
| 31 | + ) { |
| 32 | + $this->methodNameResolver = new MethodNameResolver(); |
| 33 | + } |
| 34 | + |
| 35 | + public function getDefinition(): FixerDefinitionInterface |
| 36 | + { |
| 37 | + return new FixerDefinition(self::ERROR_MESSAGE, []); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * @param Tokens<Token> $tokens |
| 42 | + */ |
| 43 | + public function isCandidate(Tokens $tokens): bool |
| 44 | + { |
| 45 | + if (! $tokens->isTokenKindFound(T_FUNCTION)) { |
| 46 | + return false; |
| 47 | + } |
| 48 | + |
| 49 | + return $tokens->isAnyTokenKindsFound([T_DOC_COMMENT, T_COMMENT]); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * @param Tokens<Token> $tokens |
| 54 | + */ |
| 55 | + public function fix(SplFileInfo $fileInfo, Tokens $tokens): void |
| 56 | + { |
| 57 | + $reversedTokens = $this->tokenReverser->reverse($tokens); |
| 58 | + |
| 59 | + foreach ($reversedTokens as $index => $token) { |
| 60 | + if (! $token->isGivenKind([T_DOC_COMMENT, T_COMMENT])) { |
| 61 | + continue; |
| 62 | + } |
| 63 | + |
| 64 | + $methodName = $this->methodNameResolver->resolve($tokens, $index); |
| 65 | + if (is_null($methodName)) { |
| 66 | + continue; |
| 67 | + } |
| 68 | + |
| 69 | + // skip if not setter or getter |
| 70 | + $originalDocContent = $token->getContent(); |
| 71 | + |
| 72 | + $hasChanged = false; |
| 73 | + |
| 74 | + $docblockLines = explode("\n", $originalDocContent); |
| 75 | + foreach ($docblockLines as $key => $docblockLine) { |
| 76 | + $spacelessDocblockLine = Strings::replace($docblockLine, '#[\s\n]+#', ''); |
| 77 | + if (strtolower($spacelessDocblockLine) !== strtolower('*' . $methodName)) { |
| 78 | + continue; |
| 79 | + } |
| 80 | + |
| 81 | + $hasChanged = true; |
| 82 | + unset($docblockLines[$key]); |
| 83 | + } |
| 84 | + |
| 85 | + if (! $hasChanged) { |
| 86 | + continue; |
| 87 | + } |
| 88 | + |
| 89 | + $tokens[$index] = new Token([T_DOC_COMMENT, implode("\n", $docblockLines)]); |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments