|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of PHP CS Fixer: custom fixers. |
| 5 | + * |
| 6 | + * (c) 2018 Kuba Werłos |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view |
| 9 | + * the LICENSE file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace PhpCsFixerCustomFixers\Fixer; |
| 13 | + |
| 14 | +use PhpCsFixer\FixerDefinition\FixerDefinition; |
| 15 | +use PhpCsFixer\FixerDefinition\FixerDefinitionInterface; |
| 16 | +use PhpCsFixer\FixerDefinition\VersionSpecification; |
| 17 | +use PhpCsFixer\FixerDefinition\VersionSpecificCodeSample; |
| 18 | +use PhpCsFixer\Tokenizer\CT; |
| 19 | +use PhpCsFixer\Tokenizer\Token; |
| 20 | +use PhpCsFixer\Tokenizer\Tokens; |
| 21 | +use PhpCsFixerCustomFixers\Analyzer\ConstructorAnalyzer; |
| 22 | + |
| 23 | +final class ReadonlyPromotedPropertiesFixer extends AbstractFixer |
| 24 | +{ |
| 25 | + public function getDefinition(): FixerDefinitionInterface |
| 26 | + { |
| 27 | + return new FixerDefinition( |
| 28 | + 'Promoted properties must readonly.', |
| 29 | + [ |
| 30 | + new VersionSpecificCodeSample( |
| 31 | + '<?php class Foo { |
| 32 | + public function __construct( |
| 33 | + public array $a, |
| 34 | + public bool $b, |
| 35 | + ) {} |
| 36 | +} |
| 37 | +', |
| 38 | + new VersionSpecification(80100) |
| 39 | + ), |
| 40 | + ], |
| 41 | + null, |
| 42 | + 'when property is written' |
| 43 | + ); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Must run after PromotedConstructorPropertyFixer. |
| 48 | + */ |
| 49 | + public function getPriority(): int |
| 50 | + { |
| 51 | + return 0; |
| 52 | + } |
| 53 | + |
| 54 | + public function isCandidate(Tokens $tokens): bool |
| 55 | + { |
| 56 | + return \defined('T_READONLY') && $tokens->isAnyTokenKindsFound([ |
| 57 | + CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PRIVATE, |
| 58 | + CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PROTECTED, |
| 59 | + CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PUBLIC, |
| 60 | + ]); |
| 61 | + } |
| 62 | + |
| 63 | + public function isRisky(): bool |
| 64 | + { |
| 65 | + return true; |
| 66 | + } |
| 67 | + |
| 68 | + public function fix(\SplFileInfo $file, Tokens $tokens): void |
| 69 | + { |
| 70 | + $constructorAnalyzer = new ConstructorAnalyzer(); |
| 71 | + |
| 72 | + for ($index = $tokens->count() - 1; $index > 0; $index--) { |
| 73 | + if (!$tokens[$index]->isGivenKind(\T_CLASS)) { |
| 74 | + continue; |
| 75 | + } |
| 76 | + |
| 77 | + $constructorAnalysis = $constructorAnalyzer->findNonAbstractConstructor($tokens, $index); |
| 78 | + if ($constructorAnalysis === null) { |
| 79 | + continue; |
| 80 | + } |
| 81 | + |
| 82 | + $openParenthesis = $tokens->getNextTokenOfKind($constructorAnalysis->getConstructorIndex(), ['(']); |
| 83 | + \assert(\is_int($openParenthesis)); |
| 84 | + $closeParenthesis = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $openParenthesis); |
| 85 | + |
| 86 | + $this->fixParameters($tokens, $openParenthesis, $closeParenthesis); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + private function fixParameters(Tokens $tokens, int $openParenthesis, int $closeParenthesis): void |
| 91 | + { |
| 92 | + for ($index = $closeParenthesis; $index > $openParenthesis; $index--) { |
| 93 | + if ( |
| 94 | + !$tokens[$index]->isGivenKind([ |
| 95 | + CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PRIVATE, |
| 96 | + CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PROTECTED, |
| 97 | + CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PUBLIC, |
| 98 | + ]) |
| 99 | + ) { |
| 100 | + continue; |
| 101 | + } |
| 102 | + |
| 103 | + $nextIndex = $tokens->getNextMeaningfulToken($index); |
| 104 | + if ($tokens[$nextIndex]->isGivenKind(\T_READONLY)) { |
| 105 | + continue; |
| 106 | + } |
| 107 | + |
| 108 | + $prevIndex = $tokens->getPrevMeaningfulToken($index); |
| 109 | + if ($tokens[$prevIndex]->isGivenKind(\T_READONLY)) { |
| 110 | + continue; |
| 111 | + } |
| 112 | + |
| 113 | + $tokens->insertAt( |
| 114 | + $index + 1, |
| 115 | + [ |
| 116 | + new Token([\T_WHITESPACE, ' ']), |
| 117 | + new Token([\T_READONLY, 'readonly']), |
| 118 | + ] |
| 119 | + ); |
| 120 | + } |
| 121 | + } |
| 122 | +} |
0 commit comments