|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace SlevomatCodingStandard\Sniffs\TypeHints; |
| 4 | + |
| 5 | +use PHP_CodeSniffer\Files\File; |
| 6 | +use PHP_CodeSniffer\Sniffs\Sniff; |
| 7 | +use SlevomatCodingStandard\Helpers\AnnotationHelper; |
| 8 | +use SlevomatCodingStandard\Helpers\ClassHelper; |
| 9 | +use SlevomatCodingStandard\Helpers\DocCommentHelper; |
| 10 | +use SlevomatCodingStandard\Helpers\FixerHelper; |
| 11 | +use SlevomatCodingStandard\Helpers\SniffSettingsHelper; |
| 12 | +use SlevomatCodingStandard\Helpers\TokenHelper; |
| 13 | +use function array_key_exists; |
| 14 | +use function count; |
| 15 | +use function sprintf; |
| 16 | +use const T_CONST; |
| 17 | +use const T_CONSTANT_ENCAPSED_STRING; |
| 18 | +use const T_DNUMBER; |
| 19 | +use const T_DOC_COMMENT_WHITESPACE; |
| 20 | +use const T_EQUAL; |
| 21 | +use const T_FALSE; |
| 22 | +use const T_LNUMBER; |
| 23 | +use const T_MINUS; |
| 24 | +use const T_NULL; |
| 25 | +use const T_OPEN_SHORT_ARRAY; |
| 26 | +use const T_START_HEREDOC; |
| 27 | +use const T_START_NOWDOC; |
| 28 | +use const T_TRUE; |
| 29 | + |
| 30 | +class ClassConstantTypeHintSniff implements Sniff |
| 31 | +{ |
| 32 | + |
| 33 | + public const CODE_MISSING_NATIVE_TYPE_HINT = 'MissingNativeTypeHint'; |
| 34 | + public const CODE_USELESS_DOC_COMMENT = 'UselessDocComment'; |
| 35 | + public const CODE_USELESS_VAR_ANNOTATION = 'UselessVarAnnotation'; |
| 36 | + |
| 37 | + public ?bool $enableNativeTypeHint = null; |
| 38 | + |
| 39 | + /** @var array<int|string, string> */ |
| 40 | + private static array $tokenToTypeHintMapping = [ |
| 41 | + T_FALSE => 'false', |
| 42 | + T_TRUE => 'true', |
| 43 | + T_DNUMBER => 'float', |
| 44 | + T_LNUMBER => 'int', |
| 45 | + T_NULL => 'null', |
| 46 | + T_OPEN_SHORT_ARRAY => 'array', |
| 47 | + T_CONSTANT_ENCAPSED_STRING => 'string', |
| 48 | + T_START_NOWDOC => 'string', |
| 49 | + T_START_HEREDOC => 'string', |
| 50 | + ]; |
| 51 | + |
| 52 | + /** |
| 53 | + * @return array<int, (int|string)> |
| 54 | + */ |
| 55 | + public function register(): array |
| 56 | + { |
| 57 | + return [ |
| 58 | + T_CONST, |
| 59 | + ]; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint |
| 64 | + * @param int $constantPointer |
| 65 | + */ |
| 66 | + public function process(File $phpcsFile, $constantPointer): void |
| 67 | + { |
| 68 | + if (ClassHelper::getClassPointer($phpcsFile, $constantPointer) === null) { |
| 69 | + // Constant in namespace |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + $this->checkNativeTypeHint($phpcsFile, $constantPointer); |
| 74 | + $this->checkDocComment($phpcsFile, $constantPointer); |
| 75 | + } |
| 76 | + |
| 77 | + private function checkNativeTypeHint(File $phpcsFile, int $constantPointer): void |
| 78 | + { |
| 79 | + $this->enableNativeTypeHint = SniffSettingsHelper::isEnabledByPhpVersion($this->enableNativeTypeHint, 80300); |
| 80 | + |
| 81 | + if (!$this->enableNativeTypeHint) { |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + $namePointer = $this->getConstantNamePointer($phpcsFile, $constantPointer); |
| 86 | + $typeHintPointer = TokenHelper::findPreviousEffective($phpcsFile, $namePointer - 1); |
| 87 | + |
| 88 | + if ($typeHintPointer !== $constantPointer) { |
| 89 | + // Has type hint |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + $tokens = $phpcsFile->getTokens(); |
| 94 | + |
| 95 | + $namePointer = $this->getConstantNamePointer($phpcsFile, $constantPointer); |
| 96 | + $equalPointer = TokenHelper::findNext($phpcsFile, T_EQUAL, $constantPointer + 1); |
| 97 | + |
| 98 | + $valuePointer = TokenHelper::findNextEffective($phpcsFile, $equalPointer + 1); |
| 99 | + if ($tokens[$valuePointer]['code'] === T_MINUS) { |
| 100 | + $valuePointer = TokenHelper::findNextEffective($phpcsFile, $valuePointer + 1); |
| 101 | + } |
| 102 | + |
| 103 | + $constantName = $tokens[$namePointer]['content']; |
| 104 | + |
| 105 | + $typeHint = null; |
| 106 | + if (array_key_exists($tokens[$valuePointer]['code'], self::$tokenToTypeHintMapping)) { |
| 107 | + $typeHint = self::$tokenToTypeHintMapping[$tokens[$valuePointer]['code']]; |
| 108 | + } |
| 109 | + |
| 110 | + $errorParameters = [ |
| 111 | + sprintf('Constant %s does not have native type hint.', $constantName), |
| 112 | + $constantPointer, |
| 113 | + self::CODE_MISSING_NATIVE_TYPE_HINT, |
| 114 | + ]; |
| 115 | + |
| 116 | + if ($typeHint === null) { |
| 117 | + $phpcsFile->addError(...$errorParameters); |
| 118 | + return; |
| 119 | + } |
| 120 | + |
| 121 | + $fix = $phpcsFile->addFixableError(...$errorParameters); |
| 122 | + |
| 123 | + if (!$fix) { |
| 124 | + return; |
| 125 | + } |
| 126 | + |
| 127 | + $phpcsFile->fixer->beginChangeset(); |
| 128 | + $phpcsFile->fixer->addContent($constantPointer, ' ' . $typeHint); |
| 129 | + $phpcsFile->fixer->endChangeset(); |
| 130 | + } |
| 131 | + |
| 132 | + private function checkDocComment(File $phpcsFile, int $constantPointer): void |
| 133 | + { |
| 134 | + $docCommentOpenPointer = DocCommentHelper::findDocCommentOpenPointer($phpcsFile, $constantPointer); |
| 135 | + if ($docCommentOpenPointer === null) { |
| 136 | + return; |
| 137 | + } |
| 138 | + |
| 139 | + $annotations = AnnotationHelper::getAnnotations($phpcsFile, $constantPointer, '@var'); |
| 140 | + |
| 141 | + if ($annotations === []) { |
| 142 | + return; |
| 143 | + } |
| 144 | + |
| 145 | + $tokens = $phpcsFile->getTokens(); |
| 146 | + |
| 147 | + $namePointer = $this->getConstantNamePointer($phpcsFile, $constantPointer); |
| 148 | + $constantName = $tokens[$namePointer]['content']; |
| 149 | + |
| 150 | + $uselessDocComment = !DocCommentHelper::hasDocCommentDescription($phpcsFile, $constantPointer) && count($annotations) === 1; |
| 151 | + if ($uselessDocComment) { |
| 152 | + $fix = $phpcsFile->addFixableError( |
| 153 | + sprintf('Useless documentation comment for constant %s.', $constantName), |
| 154 | + $docCommentOpenPointer, |
| 155 | + self::CODE_USELESS_DOC_COMMENT, |
| 156 | + ); |
| 157 | + |
| 158 | + /** @var int $fixerStart */ |
| 159 | + $fixerStart = TokenHelper::findLastTokenOnPreviousLine($phpcsFile, $docCommentOpenPointer); |
| 160 | + $fixerEnd = $tokens[$docCommentOpenPointer]['comment_closer']; |
| 161 | + } else { |
| 162 | + $annotation = $annotations[0]; |
| 163 | + |
| 164 | + $fix = $phpcsFile->addFixableError( |
| 165 | + sprintf('Useless @var annotation for constant %s.', $constantName), |
| 166 | + $annotation->getStartPointer(), |
| 167 | + self::CODE_USELESS_VAR_ANNOTATION, |
| 168 | + ); |
| 169 | + |
| 170 | + /** @var int $fixerStart */ |
| 171 | + $fixerStart = TokenHelper::findPreviousContent( |
| 172 | + $phpcsFile, |
| 173 | + T_DOC_COMMENT_WHITESPACE, |
| 174 | + $phpcsFile->eolChar, |
| 175 | + $annotation->getStartPointer() - 1, |
| 176 | + ); |
| 177 | + $fixerEnd = $annotation->getEndPointer(); |
| 178 | + } |
| 179 | + |
| 180 | + if (!$fix) { |
| 181 | + return; |
| 182 | + } |
| 183 | + |
| 184 | + $phpcsFile->fixer->beginChangeset(); |
| 185 | + FixerHelper::removeBetweenIncluding($phpcsFile, $fixerStart, $fixerEnd); |
| 186 | + $phpcsFile->fixer->endChangeset(); |
| 187 | + } |
| 188 | + |
| 189 | + private function getConstantNamePointer(File $phpcsFile, int $constantPointer): int |
| 190 | + { |
| 191 | + $equalPointer = TokenHelper::findNext($phpcsFile, T_EQUAL, $constantPointer + 1); |
| 192 | + |
| 193 | + return TokenHelper::findPreviousEffective($phpcsFile, $equalPointer - 1); |
| 194 | + } |
| 195 | + |
| 196 | +} |
0 commit comments