|
| 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\CodeSample; |
| 15 | +use PhpCsFixer\FixerDefinition\FixerDefinition; |
| 16 | +use PhpCsFixer\FixerDefinition\FixerDefinitionInterface; |
| 17 | +use PhpCsFixer\Tokenizer\Token; |
| 18 | +use PhpCsFixer\Tokenizer\Tokens; |
| 19 | + |
| 20 | +final class ClassConstantUsageFixer extends AbstractFixer |
| 21 | +{ |
| 22 | + public function getDefinition(): FixerDefinitionInterface |
| 23 | + { |
| 24 | + return new FixerDefinition( |
| 25 | + 'Class constant must be used instead of a copy of string.', |
| 26 | + [new CodeSample( |
| 27 | + <<<'PHP' |
| 28 | + <?php |
| 29 | + class Foo |
| 30 | + { |
| 31 | + public const BAR = 'bar'; |
| 32 | + public function bar() |
| 33 | + { |
| 34 | + return 'bar'; |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + PHP, |
| 39 | + )], |
| 40 | + '', |
| 41 | + ); |
| 42 | + } |
| 43 | + |
| 44 | + public function getPriority(): int |
| 45 | + { |
| 46 | + return 0; |
| 47 | + } |
| 48 | + |
| 49 | + public function isCandidate(Tokens $tokens): bool |
| 50 | + { |
| 51 | + return $tokens->isAllTokenKindsFound([\T_CLASS, \T_CONSTANT_ENCAPSED_STRING]); |
| 52 | + } |
| 53 | + |
| 54 | + public function isRisky(): bool |
| 55 | + { |
| 56 | + return false; |
| 57 | + } |
| 58 | + |
| 59 | + public function fix(\SplFileInfo $file, Tokens $tokens): void |
| 60 | + { |
| 61 | + for ($index = $tokens->count() - 1; $index > 0; $index--) { |
| 62 | + if (!$tokens[$index]->isGivenKind(\T_CLASS)) { |
| 63 | + continue; |
| 64 | + } |
| 65 | + |
| 66 | + $openParenthesisIndex = $tokens->getNextTokenOfKind($index, ['{']); |
| 67 | + \assert(\is_int($openParenthesisIndex)); |
| 68 | + |
| 69 | + $closeParenthesisIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $openParenthesisIndex); |
| 70 | + |
| 71 | + $this->fixClass($tokens, $openParenthesisIndex, $closeParenthesisIndex); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + private function fixClass(Tokens $tokens, int $openParenthesisIndex, int $closeParenthesisIndex): void |
| 76 | + { |
| 77 | + [$constantsMap, $constantsIndices] = $this->getClassConstants($tokens, $openParenthesisIndex, $closeParenthesisIndex); |
| 78 | + |
| 79 | + for ($index = $closeParenthesisIndex; $index > $openParenthesisIndex; $index--) { |
| 80 | + if (!$tokens[$index]->isGivenKind(\T_CONSTANT_ENCAPSED_STRING)) { |
| 81 | + continue; |
| 82 | + } |
| 83 | + |
| 84 | + if (!isset($constantsMap[$tokens[$index]->getContent()])) { |
| 85 | + continue; |
| 86 | + } |
| 87 | + |
| 88 | + if (isset($constantsIndices[$index])) { |
| 89 | + continue; |
| 90 | + } |
| 91 | + |
| 92 | + $tokens->overrideRange( |
| 93 | + $index, |
| 94 | + $index, |
| 95 | + [ |
| 96 | + new Token([\T_STRING, 'self']), |
| 97 | + new Token([\T_DOUBLE_COLON, '::']), |
| 98 | + new Token([\T_STRING, $constantsMap[$tokens[$index]->getContent()]]), |
| 99 | + ], |
| 100 | + ); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * @return array{array<string, string>, array<int, true>} |
| 106 | + */ |
| 107 | + private function getClassConstants(Tokens $tokens, int $openParenthesisIndex, int $closeParenthesisIndex): array |
| 108 | + { |
| 109 | + $constants = []; |
| 110 | + $constantsIndices = []; |
| 111 | + for ($index = $openParenthesisIndex; $index < $closeParenthesisIndex; $index++) { |
| 112 | + if (!$tokens[$index]->isGivenKind(\T_CONST)) { |
| 113 | + continue; |
| 114 | + } |
| 115 | + |
| 116 | + $assignTokenIndex = $tokens->getNextTokenOfKind($index, ['=']); |
| 117 | + \assert(\is_int($assignTokenIndex)); |
| 118 | + |
| 119 | + $constantNameIndex = $tokens->getPrevMeaningfulToken($assignTokenIndex); |
| 120 | + \assert(\is_int($constantNameIndex)); |
| 121 | + |
| 122 | + $constantValueIndex = $tokens->getNextMeaningfulToken($assignTokenIndex); |
| 123 | + \assert(\is_int($constantValueIndex)); |
| 124 | + |
| 125 | + $constantsIndices[$constantValueIndex] = true; |
| 126 | + |
| 127 | + if (!$tokens[$constantValueIndex]->isGivenKind(\T_CONSTANT_ENCAPSED_STRING)) { |
| 128 | + continue; |
| 129 | + } |
| 130 | + |
| 131 | + $constants[$tokens[$constantNameIndex]->getContent()] = $tokens[$constantValueIndex]->getContent(); |
| 132 | + } |
| 133 | + |
| 134 | + return [$this->getClassConstantsMap($constants), $constantsIndices]; |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * @param array<string, string> $constants |
| 139 | + * |
| 140 | + * @return array<string, string> |
| 141 | + */ |
| 142 | + private function getClassConstantsMap(array $constants): array |
| 143 | + { |
| 144 | + $map = []; |
| 145 | + $valuesCount = []; |
| 146 | + |
| 147 | + foreach ($constants as $name => $value) { |
| 148 | + $map[$value] = $name; |
| 149 | + $valuesCount[$value] = ($valuesCount[$value] ?? 0) + 1; |
| 150 | + |
| 151 | + if ($valuesCount[$value] > 1) { |
| 152 | + unset($map[$value]); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + return $map; |
| 157 | + } |
| 158 | +} |
0 commit comments