|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace SlevomatCodingStandard\Sniffs\Classes; |
| 6 | + |
| 7 | +use PHP_CodeSniffer\Files\File; |
| 8 | +use PHP_CodeSniffer\Sniffs\Sniff; |
| 9 | +use SlevomatCodingStandard\Helpers\ClassHelper; |
| 10 | +use SlevomatCodingStandard\Helpers\FunctionHelper; |
| 11 | +use SlevomatCodingStandard\Helpers\SniffSettingsHelper; |
| 12 | +use SlevomatCodingStandard\Helpers\TokenHelper; |
| 13 | + |
| 14 | +use function count; |
| 15 | +use function in_array; |
| 16 | +use function sprintf; |
| 17 | +use function strtolower; |
| 18 | + |
| 19 | +use const T_ABSTRACT; |
| 20 | +use const T_ATTRIBUTE_END; |
| 21 | +use const T_CLASS; |
| 22 | +use const T_COMMA; |
| 23 | +use const T_FINAL; |
| 24 | +use const T_FUNCTION; |
| 25 | +use const T_OPEN_PARENTHESIS; |
| 26 | +use const T_READONLY; |
| 27 | +use const T_VARIABLE; |
| 28 | +use const T_WHITESPACE; |
| 29 | + |
| 30 | +class ReadonlyClassSniff implements Sniff |
| 31 | +{ |
| 32 | + public const CODE_CLASS_CAN_BE_READONLY = 'ClassCanBeReadonly'; |
| 33 | + public const CODE_PROMOTED_PROPERTY_CANNOT_BE_READONLY_IN_READONLY_CLASS = 'PromotedPropertyCannotBeReadonlyInReadonlyClass'; |
| 34 | + public ?bool $enable = null; |
| 35 | +/** |
| 36 | + * @return array<int, (int|string)> |
| 37 | + */ |
| 38 | + public function register(): array |
| 39 | + { |
| 40 | + return [T_CLASS]; |
| 41 | + } |
| 42 | + |
| 43 | + public function process(File $phpcsFile, int $classPointer): void |
| 44 | + { |
| 45 | + $this->enable = SniffSettingsHelper::isEnabledByPhpVersion($this->enable, 80200); |
| 46 | + if (!$this->enable) { |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + $constructorPointer = $this->findConstructorPointer($phpcsFile, $classPointer); |
| 51 | + if ($constructorPointer === null) { |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + $promotedProperties = $this->getPromotedProperties($phpcsFile, $constructorPointer); |
| 56 | + if (count($promotedProperties) === 0) { |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + $tokens = $phpcsFile->getTokens(); |
| 61 | + if ($this->isReadonlyClass($phpcsFile, $classPointer)) { |
| 62 | + foreach ($promotedProperties as $promotedProperty) { |
| 63 | + $readonlyModifierPointer = $promotedProperty['readonlyModifierPointer']; |
| 64 | + if ($readonlyModifierPointer === null) { |
| 65 | + continue; |
| 66 | + } |
| 67 | + |
| 68 | + $fix = $phpcsFile->addFixableError(sprintf('Promoted property %s in readonly class cannot be declared as readonly.', $tokens[$promotedProperty['propertyPointer']]['content'],), $readonlyModifierPointer, self::CODE_PROMOTED_PROPERTY_CANNOT_BE_READONLY_IN_READONLY_CLASS,); |
| 69 | + if (!$fix) { |
| 70 | + continue; |
| 71 | + } |
| 72 | + |
| 73 | + $phpcsFile->fixer->beginChangeset(); |
| 74 | + $this->removeReadonlyModifier($phpcsFile, $readonlyModifierPointer); |
| 75 | + $phpcsFile->fixer->endChangeset(); |
| 76 | + } |
| 77 | + |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + foreach ($promotedProperties as $promotedProperty) { |
| 82 | + if ($promotedProperty['readonlyModifierPointer'] === null) { |
| 83 | + return; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + $fix = $phpcsFile->addFixableError(sprintf('Class %s can be marked as readonly.', ClassHelper::getName($phpcsFile, $classPointer)), $classPointer, self::CODE_CLASS_CAN_BE_READONLY,); |
| 88 | + if (!$fix) { |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + $phpcsFile->fixer->beginChangeset(); |
| 93 | + foreach ($promotedProperties as $promotedProperty) { |
| 94 | + $this->removeReadonlyModifier($phpcsFile, $promotedProperty['readonlyModifierPointer']); |
| 95 | + } |
| 96 | + |
| 97 | + $phpcsFile->fixer->addContentBefore($classPointer, 'readonly '); |
| 98 | + $phpcsFile->fixer->endChangeset(); |
| 99 | + } |
| 100 | + |
| 101 | + private function findConstructorPointer(File $phpcsFile, int $classPointer): ?int |
| 102 | + { |
| 103 | + $tokens = $phpcsFile->getTokens(); |
| 104 | + $classLevel = $tokens[$classPointer]['level']; |
| 105 | + |
| 106 | + for ($i = $tokens[$classPointer]['scope_opener'] + 1; $i < $tokens[$classPointer]['scope_closer']; $i++) { |
| 107 | + if ($tokens[$i]['code'] !== T_FUNCTION) { |
| 108 | + continue; |
| 109 | + } |
| 110 | + |
| 111 | + if ($tokens[$i]['level'] !== $classLevel + 1) { |
| 112 | + continue; |
| 113 | + } |
| 114 | + |
| 115 | + if (strtolower(FunctionHelper::getName($phpcsFile, $i)) !== '__construct') { |
| 116 | + continue; |
| 117 | + } |
| 118 | + |
| 119 | + return $i; |
| 120 | + } |
| 121 | + |
| 122 | + return null; |
| 123 | + } |
| 124 | + |
| 125 | + private function isReadonlyClass(File $phpcsFile, int $classPointer): bool |
| 126 | + { |
| 127 | + $tokens = $phpcsFile->getTokens(); |
| 128 | + $modifierPointer = TokenHelper::findPreviousEffective($phpcsFile, $classPointer - 1); |
| 129 | + while ( |
| 130 | + $modifierPointer !== null |
| 131 | + && in_array($tokens[$modifierPointer]['code'], [T_FINAL, T_ABSTRACT, T_READONLY], true) |
| 132 | + ) { |
| 133 | + if ($tokens[$modifierPointer]['code'] === T_READONLY) { |
| 134 | + return true; |
| 135 | + } |
| 136 | + |
| 137 | + $modifierPointer = TokenHelper::findPreviousEffective($phpcsFile, $modifierPointer - 1); |
| 138 | + } |
| 139 | + |
| 140 | + return false; |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * @return list<array{propertyPointer: int, readonlyModifierPointer: ?int}> |
| 145 | + */ |
| 146 | + private function getPromotedProperties(File $phpcsFile, int $constructorPointer): array |
| 147 | + { |
| 148 | + $tokens = $phpcsFile->getTokens(); |
| 149 | + $promotedProperties = []; |
| 150 | + |
| 151 | + for ($i = $tokens[$constructorPointer]['parenthesis_opener'] + 1; $i < $tokens[$constructorPointer]['parenthesis_closer']; $i++) { |
| 152 | + if ($tokens[$i]['code'] !== T_VARIABLE) { |
| 153 | + continue; |
| 154 | + } |
| 155 | + |
| 156 | + $pointerBefore = TokenHelper::findPrevious($phpcsFile, [T_COMMA, T_OPEN_PARENTHESIS, T_ATTRIBUTE_END], $i - 1); |
| 157 | + $modifierPointer = TokenHelper::findNextEffective($phpcsFile, $pointerBefore + 1); |
| 158 | + if (!in_array($tokens[$modifierPointer]['code'], TokenHelper::PROPERTY_MODIFIERS_TOKEN_CODES, true)) { |
| 159 | + continue; |
| 160 | + } |
| 161 | + |
| 162 | + $readonlyModifierPointer = TokenHelper::findNext($phpcsFile, T_READONLY, $modifierPointer, $i); |
| 163 | + $promotedProperties[] = [ |
| 164 | + 'propertyPointer' => $i, |
| 165 | + 'readonlyModifierPointer' => $readonlyModifierPointer, |
| 166 | + ]; |
| 167 | + } |
| 168 | + |
| 169 | + return $promotedProperties; |
| 170 | + } |
| 171 | + |
| 172 | + private function removeReadonlyModifier(File $phpcsFile, int $readonlyModifierPointer): void |
| 173 | + { |
| 174 | + $phpcsFile->fixer->replaceToken($readonlyModifierPointer, ''); |
| 175 | + $nextPointer = TokenHelper::findNext($phpcsFile, T_WHITESPACE, $readonlyModifierPointer + 1, $readonlyModifierPointer + 2); |
| 176 | + if ($nextPointer !== null) { |
| 177 | + $phpcsFile->fixer->replaceToken($nextPointer, ''); |
| 178 | + } |
| 179 | + } |
| 180 | +} |
0 commit comments