|
| 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 | + |
| 22 | +final class TypedClassConstantFixer extends AbstractFixer |
| 23 | +{ |
| 24 | + public function getDefinition(): FixerDefinitionInterface |
| 25 | + { |
| 26 | + return new FixerDefinition( |
| 27 | + 'Class constants must have a type.', |
| 28 | + [ |
| 29 | + new VersionSpecificCodeSample( |
| 30 | + <<<'PHP' |
| 31 | + <?php |
| 32 | + class Foo { |
| 33 | + public const MAX_VALUE_OF_SOMETHING = 42; |
| 34 | + public const THE_NAME_OF_SOMEONE = 'John Doe'; |
| 35 | + } |
| 36 | + |
| 37 | + PHP, |
| 38 | + new VersionSpecification(80300), |
| 39 | + ), |
| 40 | + ], |
| 41 | + '', |
| 42 | + 'when constant is not of single type', |
| 43 | + ); |
| 44 | + } |
| 45 | + |
| 46 | + public function getPriority(): int |
| 47 | + { |
| 48 | + return 0; |
| 49 | + } |
| 50 | + |
| 51 | + public function isCandidate(Tokens $tokens): bool |
| 52 | + { |
| 53 | + return $tokens->isAllTokenKindsFound([\T_CLASS, \T_CONST]); |
| 54 | + } |
| 55 | + |
| 56 | + public function isRisky(): bool |
| 57 | + { |
| 58 | + return true; |
| 59 | + } |
| 60 | + |
| 61 | + public function fix(\SplFileInfo $file, Tokens $tokens): void |
| 62 | + { |
| 63 | + for ($index = $tokens->count() - 1; $index > 0; $index--) { |
| 64 | + if (!$tokens[$index]->isGivenKind(\T_CLASS)) { |
| 65 | + continue; |
| 66 | + } |
| 67 | + |
| 68 | + $openParenthesisIndex = $tokens->getNextTokenOfKind($index, ['{']); |
| 69 | + \assert(\is_int($openParenthesisIndex)); |
| 70 | + |
| 71 | + $closeParenthesisIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $openParenthesisIndex); |
| 72 | + |
| 73 | + self::fixClass($tokens, $openParenthesisIndex, $closeParenthesisIndex); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + private static function fixClass(Tokens $tokens, int $openParenthesisIndex, int $closeParenthesisIndex): void |
| 78 | + { |
| 79 | + for ($index = $closeParenthesisIndex; $index > $openParenthesisIndex; $index--) { |
| 80 | + if (!$tokens[$index]->isGivenKind(\T_CONST)) { |
| 81 | + continue; |
| 82 | + } |
| 83 | + |
| 84 | + $constantNameIndex = $tokens->getNextMeaningfulToken($index); |
| 85 | + |
| 86 | + $assignmentIndex = $tokens->getNextMeaningfulToken($constantNameIndex); |
| 87 | + if (!$tokens[$assignmentIndex]->equals('=')) { |
| 88 | + continue; |
| 89 | + } |
| 90 | + |
| 91 | + $tokens->insertAt( |
| 92 | + $constantNameIndex, |
| 93 | + [ |
| 94 | + self::getTypeOfExpression($tokens, $assignmentIndex), |
| 95 | + new Token([\T_WHITESPACE, ' ']), |
| 96 | + ], |
| 97 | + ); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + private static function getTypeOfExpression(Tokens $tokens, int $assignmentIndex): Token |
| 102 | + { |
| 103 | + $semicolonIndex = $tokens->getNextTokenOfKind($assignmentIndex, [';']); |
| 104 | + $beforeSemicolonIndex = $tokens->getPrevMeaningfulToken($semicolonIndex); |
| 105 | + |
| 106 | + $map = [ |
| 107 | + \T_DNUMBER => [\T_STRING, 'float'], |
| 108 | + \T_LNUMBER => [\T_STRING, 'int'], |
| 109 | + \T_CONSTANT_ENCAPSED_STRING => [\T_STRING, 'string'], |
| 110 | + CT::T_ARRAY_SQUARE_BRACE_CLOSE => [CT::T_ARRAY_TYPEHINT, 'array'], |
| 111 | + ]; |
| 112 | + |
| 113 | + $tokenId = $tokens[$beforeSemicolonIndex]->getId(); |
| 114 | + |
| 115 | + if ($tokens[$beforeSemicolonIndex]->isGivenKind(\T_STRING)) { |
| 116 | + return new Token([\T_STRING, $tokens[$beforeSemicolonIndex]->getContent()]); |
| 117 | + } |
| 118 | + |
| 119 | + if ($tokenId === null & $tokens[$beforeSemicolonIndex]->equals(')')) { |
| 120 | + $tokenId = CT::T_ARRAY_SQUARE_BRACE_CLOSE; |
| 121 | + } |
| 122 | + |
| 123 | + if (isset($map[$tokenId])) { |
| 124 | + return new Token($map[$tokenId]); |
| 125 | + } |
| 126 | + |
| 127 | + \dd($tokens[$beforeSemicolonIndex], \token_name($tokens[$beforeSemicolonIndex]->getId())); |
| 128 | + |
| 129 | + return new Token([\T_STRING, 'mixed']); |
| 130 | + } |
| 131 | +} |
0 commit comments