|
19 | 19 |
|
20 | 20 | class ArgumentTypeDeclarationSniff implements Sniff |
21 | 21 | { |
22 | | - const TYPE_CODES = [ |
| 22 | + public const TYPE_CODES = [ |
23 | 23 | T_STRING, |
24 | 24 | T_ARRAY_HINT, |
25 | 25 | T_CALLABLE, |
26 | 26 | T_SELF, |
27 | 27 | ]; |
28 | 28 |
|
29 | | - const METHODS_WHITELIST = [ |
| 29 | + public const METHODS_WHITELIST = [ |
30 | 30 | 'unserialize', |
31 | 31 | 'seek', |
32 | 32 | ]; |
33 | 33 |
|
34 | 34 | /** |
35 | | - * @return array<int|string> |
36 | | - * |
37 | | - * phpcs:disable Inpsyde.CodeQuality.ReturnTypeDeclaration |
| 35 | + * @return list<int|string> |
38 | 36 | */ |
39 | | - public function register() |
| 37 | + public function register(): array |
40 | 38 | { |
41 | | - // phpcs:enable Inpsyde.CodeQuality.ReturnTypeDeclaration |
42 | | - |
43 | 39 | return [T_FUNCTION, T_CLOSURE, T_FN]; |
44 | 40 | } |
45 | 41 |
|
46 | 42 | /** |
47 | | - * @param File $file |
48 | | - * @param int $position |
| 43 | + * @param File $phpcsFile |
| 44 | + * @param int $stackPtr |
49 | 45 | * @return void |
50 | 46 | * |
51 | | - * phpcs:disable Inpsyde.CodeQuality |
| 47 | + * phpcs:disable Inpsyde.CodeQuality.ArgumentTypeDeclaration |
| 48 | + * phpcs:disable Generic.Metrics.CyclomaticComplexity |
52 | 49 | */ |
53 | | - public function process(File $file, $position) |
| 50 | + public function process(File $phpcsFile, $stackPtr): void |
54 | 51 | { |
55 | | - // phpcs:enable Inpsyde.CodeQuality |
| 52 | + // phpcs:enable Inpsyde.CodeQuality.ArgumentTypeDeclaration |
| 53 | + // phpcs:enable Generic.Metrics.CyclomaticComplexity |
56 | 54 |
|
57 | | - if ( |
58 | | - PhpcsHelpers::functionIsArrayAccess($file, $position) |
59 | | - || PhpcsHelpers::isHookClosure($file, $position) |
60 | | - || PhpcsHelpers::isHookFunction($file, $position) |
61 | | - || PhpcsHelpers::isUntypedPsrMethod($file, $position) |
62 | | - || ( |
63 | | - PhpcsHelpers::functionIsMethod($file, $position) |
64 | | - && in_array($file->getDeclarationName($position), self::METHODS_WHITELIST, true) |
65 | | - ) |
66 | | - ) { |
| 55 | + if ($this->shouldIgnore($phpcsFile, $stackPtr)) { |
67 | 56 | return; |
68 | 57 | } |
69 | 58 |
|
70 | 59 | /** @var array<int, array<string, mixed>> $tokens */ |
71 | | - $tokens = $file->getTokens(); |
72 | | - $paramsStart = (int)($tokens[$position]['parenthesis_opener'] ?? 0); |
73 | | - $paramsEnd = (int)($tokens[$position]['parenthesis_closer'] ?? 0); |
| 60 | + $tokens = $phpcsFile->getTokens(); |
| 61 | + $paramsStart = (int)($tokens[$stackPtr]['parenthesis_opener'] ?? 0); |
| 62 | + $paramsEnd = (int)($tokens[$stackPtr]['parenthesis_closer'] ?? 0); |
74 | 63 |
|
75 | 64 | if (!$paramsStart || !$paramsEnd || $paramsStart >= ($paramsEnd - 1)) { |
76 | 65 | return; |
77 | 66 | } |
78 | 67 |
|
79 | | - $docBlockTypes = PhpcsHelpers::functionDocBlockParamTypes($file, $position); |
80 | | - $variables = PhpcsHelpers::filterTokensByType($paramsStart, $paramsEnd, $file, T_VARIABLE); |
| 68 | + $docBlockTypes = PhpcsHelpers::functionDocBlockParamTypes($phpcsFile, $stackPtr); |
| 69 | + $variables = PhpcsHelpers::filterTokensByType($paramsStart, $paramsEnd, $phpcsFile, T_VARIABLE); |
81 | 70 |
|
82 | 71 | foreach ($variables as $varPosition => $varToken) { |
83 | 72 | // Not triggering error for variable explicitly declared as mixed (or mixed|null) |
84 | 73 | if ($this->isMixed((string)($varToken['content'] ?? ''), $docBlockTypes)) { |
85 | 74 | continue; |
86 | 75 | } |
87 | 76 |
|
88 | | - $typePosition = $file->findPrevious( |
| 77 | + $typePosition = $phpcsFile->findPrevious( |
89 | 78 | [T_WHITESPACE, T_ELLIPSIS, T_BITWISE_AND], |
90 | 79 | $varPosition - 1, |
91 | 80 | $paramsStart + 1, |
92 | 81 | true |
93 | 82 | ); |
94 | 83 |
|
95 | 84 | $type = $tokens[$typePosition] ?? null; |
96 | | - /** @psalm-suppress MixedArgument */ |
97 | | - if ($type && !in_array($type['code'], self::TYPE_CODES, true)) { |
98 | | - $file->addWarning('Argument type is missing', $position, 'NoArgumentType'); |
| 85 | + if ($type && !in_array($type['code'] ?? '', self::TYPE_CODES, true)) { |
| 86 | + $phpcsFile->addWarning('Argument type is missing', $stackPtr, 'NoArgumentType'); |
99 | 87 | } |
100 | 88 | } |
101 | 89 | } |
102 | 90 |
|
| 91 | + /** |
| 92 | + * @param File $phpcsFile |
| 93 | + * @param int $stackPtr |
| 94 | + * @return bool |
| 95 | + * |
| 96 | + * phpcs:disable Inpsyde.CodeQuality.ArgumentTypeDeclaration |
| 97 | + */ |
| 98 | + private function shouldIgnore(File $phpcsFile, $stackPtr): bool |
| 99 | + { |
| 100 | + // phpcs:enable Inpsyde.CodeQuality.ArgumentTypeDeclaration |
| 101 | + |
| 102 | + $name = $phpcsFile->getDeclarationName($stackPtr); |
| 103 | + |
| 104 | + return PhpcsHelpers::functionIsArrayAccess($phpcsFile, $stackPtr) |
| 105 | + || PhpcsHelpers::isHookClosure($phpcsFile, $stackPtr) |
| 106 | + || PhpcsHelpers::isHookFunction($phpcsFile, $stackPtr) |
| 107 | + || PhpcsHelpers::isUntypedPsrMethod($phpcsFile, $stackPtr) |
| 108 | + || ( |
| 109 | + PhpcsHelpers::functionIsMethod($phpcsFile, $stackPtr) |
| 110 | + && in_array($name, self::METHODS_WHITELIST, true) |
| 111 | + ); |
| 112 | + } |
| 113 | + |
103 | 114 | /** |
104 | 115 | * @param string $paramName |
105 | 116 | * @param array<string, array<string>> $docBlockTypes |
|
0 commit comments