Skip to content

Commit 538ab63

Browse files
committed
DisallowArrayTypeHintSyntaxSniff: Fixed internal error
1 parent 2cc7c72 commit 538ab63

File tree

4 files changed

+26
-2
lines changed

4 files changed

+26
-2
lines changed

SlevomatCodingStandard/Sniffs/TypeHints/DisallowArrayTypeHintSyntaxSniff.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,11 @@ private function findGenericIdentifier(File $phpcsFile, TypeNode $typeNode, Anno
172172
return null;
173173
}
174174

175-
$functionPointer = TokenHelper::findNext($phpcsFile, T_FUNCTION, $annotation->getStartPointer() + 1);
175+
$functionPointer = TokenHelper::findNext($phpcsFile, TokenHelper::$functionTokenCodes, $annotation->getStartPointer() + 1);
176+
177+
if ($functionPointer === null || $phpcsFile->getTokens()[$functionPointer]['code'] !== T_FUNCTION) {
178+
return null;
179+
}
176180

177181
if ($annotation instanceof ParameterAnnotation) {
178182
$parameterTypeHints = FunctionHelper::getParametersTypeHints($phpcsFile, $functionPointer);

tests/Sniffs/TypeHints/DisallowArrayTypeHintSyntaxSniffTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function testErrors(): void
2121
],
2222
]);
2323

24-
self::assertSame(23, $report->getErrorCount());
24+
self::assertSame(25, $report->getErrorCount());
2525

2626
self::assertSniffError($report, 6, DisallowArrayTypeHintSyntaxSniff::CODE_DISALLOWED_ARRAY_TYPE_HINT_SYNTAX, 'Usage of array type hint syntax in "\DateTimeImmutable[]" is disallowed, use generic type hint syntax instead.');
2727
self::assertSniffError($report, 7, DisallowArrayTypeHintSyntaxSniff::CODE_DISALLOWED_ARRAY_TYPE_HINT_SYNTAX, 'Usage of array type hint syntax in "bool[]" is disallowed, use generic type hint syntax instead.');
@@ -46,6 +46,8 @@ public function testErrors(): void
4646
self::assertSniffError($report, 60, DisallowArrayTypeHintSyntaxSniff::CODE_DISALLOWED_ARRAY_TYPE_HINT_SYNTAX, 'Usage of array type hint syntax in "string[][]" is disallowed, use generic type hint syntax instead.');
4747
self::assertSniffError($report, 65, DisallowArrayTypeHintSyntaxSniff::CODE_DISALLOWED_ARRAY_TYPE_HINT_SYNTAX, 'Usage of array type hint syntax in "string[][]" is disallowed, use generic type hint syntax instead.');
4848
self::assertSniffError($report, 70, DisallowArrayTypeHintSyntaxSniff::CODE_DISALLOWED_ARRAY_TYPE_HINT_SYNTAX, 'Usage of array type hint syntax in "int[]" is disallowed, use generic type hint syntax instead.');
49+
self::assertSniffError($report, 78, DisallowArrayTypeHintSyntaxSniff::CODE_DISALLOWED_ARRAY_TYPE_HINT_SYNTAX, 'Usage of array type hint syntax in "int[]" is disallowed, use generic type hint syntax instead.');
50+
self::assertSniffError($report, 79, DisallowArrayTypeHintSyntaxSniff::CODE_DISALLOWED_ARRAY_TYPE_HINT_SYNTAX, 'Usage of array type hint syntax in "mixed[]" is disallowed, use generic type hint syntax instead.');
4951

5052
self::assertAllFixedInFile($report);
5153
}

tests/Sniffs/TypeHints/data/disallowArrayTypeHintSyntaxErrors.fixed.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,13 @@ public function r($r)
7272
{
7373
}
7474

75+
private function s(): void
76+
{
77+
/**
78+
* @param array<int> $data
79+
* @return array<mixed>
80+
*/
81+
$closure = function (array $data): array {};
82+
}
83+
7584
}

tests/Sniffs/TypeHints/data/disallowArrayTypeHintSyntaxErrors.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,13 @@ public function r($r)
7272
{
7373
}
7474

75+
private function s(): void
76+
{
77+
/**
78+
* @param int[] $data
79+
* @return mixed[]
80+
*/
81+
$closure = function (array $data): array {};
82+
}
83+
7584
}

0 commit comments

Comments
 (0)