|
4 | 4 |
|
5 | 5 | use PHP_CodeSniffer\Files\File; |
6 | 6 | use PHP_CodeSniffer\Sniffs\Sniff; |
| 7 | +use PHP_CodeSniffer\Util\Tokens; |
7 | 8 | use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode; |
8 | 9 | use PHPStan\PhpDocParser\Ast\PhpDoc\TypelessParamTagValueNode; |
9 | 10 | use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode; |
|
44 | 45 | use function sprintf; |
45 | 46 | use function strtolower; |
46 | 47 | use const T_BITWISE_AND; |
| 48 | +use const T_COMMA; |
47 | 49 | use const T_DOC_COMMENT_CLOSE_TAG; |
48 | 50 | use const T_DOC_COMMENT_OPEN_TAG; |
49 | 51 | use const T_DOC_COMMENT_STAR; |
50 | 52 | use const T_ELLIPSIS; |
51 | 53 | use const T_FUNCTION; |
| 54 | +use const T_OPEN_PARENTHESIS; |
52 | 55 | use const T_VARIABLE; |
53 | 56 |
|
54 | 57 | class ParameterTypeHintSniff implements Sniff |
@@ -162,7 +165,30 @@ private function checkTypeHints( |
162 | 165 | }) |
163 | 166 | ); |
164 | 167 |
|
| 168 | + $tokens = $phpcsFile->getTokens(); |
| 169 | + |
| 170 | + $isConstructor = FunctionHelper::isMethod($phpcsFile, $functionPointer) |
| 171 | + && strtolower(FunctionHelper::getName($phpcsFile, $functionPointer)) === '__construct'; |
| 172 | + |
165 | 173 | foreach ($parametersWithoutTypeHint as $parameterName) { |
| 174 | + $isPropertyPromotion = false; |
| 175 | + |
| 176 | + if ($isConstructor) { |
| 177 | + $parameterPointer = TokenHelper::findNextContent( |
| 178 | + $phpcsFile, |
| 179 | + T_VARIABLE, |
| 180 | + $parameterName, |
| 181 | + $tokens[$functionPointer]['parenthesis_opener'], |
| 182 | + $tokens[$functionPointer]['parenthesis_closer'] |
| 183 | + ); |
| 184 | + |
| 185 | + $pointerBeforeParameter = TokenHelper::findPrevious($phpcsFile, [T_COMMA, T_OPEN_PARENTHESIS], $parameterPointer - 1); |
| 186 | + |
| 187 | + $visibilityPointer = TokenHelper::findNextEffective($phpcsFile, $pointerBeforeParameter + 1); |
| 188 | + |
| 189 | + $isPropertyPromotion = in_array($tokens[$visibilityPointer]['code'], Tokens::$scopeModifiers, true); |
| 190 | + } |
| 191 | + |
166 | 192 | if ( |
167 | 193 | !array_key_exists($parameterName, $parametersAnnotations) |
168 | 194 | || $parametersAnnotations[$parameterName]->getValue() instanceof TypelessParamTagValueNode |
@@ -313,6 +339,10 @@ private function checkTypeHints( |
313 | 339 | continue; |
314 | 340 | } |
315 | 341 |
|
| 342 | + if ($isPropertyPromotion && $typeHint === 'callable') { |
| 343 | + continue 2; |
| 344 | + } |
| 345 | + |
316 | 346 | if (!TypeHintHelper::isValidTypeHint( |
317 | 347 | $typeHint, |
318 | 348 | $this->enableObjectTypeHint, |
|
0 commit comments