Skip to content

Commit c51edb8

Browse files
committed
SlevomatCodingStandard.TypeHints.ParameterTypeHintSpacing: Fixed false positive when parameter has attribute but no type hint
1 parent 326d195 commit c51edb8

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

SlevomatCodingStandard/Sniffs/TypeHints/ParameterTypeHintSpacingSniff.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use SlevomatCodingStandard\Helpers\TypeHintHelper;
99
use function array_keys;
1010
use function sprintf;
11+
use const T_ATTRIBUTE_END;
1112
use const T_BITWISE_AND;
1213
use const T_COMMA;
1314
use const T_ELLIPSIS;
@@ -63,7 +64,14 @@ public function process(File $phpcsFile, $functionPointer): void
6364
$parameterEndPointer = $parametersEndPointer;
6465
}
6566

66-
$typeHintEndPointer = TokenHelper::findPrevious($phpcsFile, $typeHintTokenCodes, $parameterPointer - 1, $parameterStartPointer);
67+
$attributeCloserPointer = TokenHelper::findPrevious($phpcsFile, T_ATTRIBUTE_END, $parameterPointer - 1, $parameterStartPointer);
68+
69+
$typeHintEndPointer = TokenHelper::findPrevious(
70+
$phpcsFile,
71+
$typeHintTokenCodes,
72+
$parameterPointer - 1,
73+
$attributeCloserPointer ?? $parameterStartPointer
74+
);
6775
if ($typeHintEndPointer === null) {
6876
continue;
6977
}

tests/Sniffs/TypeHints/data/parameterTypeHintSpacingNoErrors.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php // lint >= 8.0
1+
<?php // lint >= 8.1
22

33
$a = function (?bool $a, ?string &$b, ?int ...$c)
44
{
@@ -8,7 +8,9 @@
88
function b(
99
?bool $a,
1010
array $b,
11-
$c
11+
#[SomeAttribute]
12+
$c,
13+
$d
1214
) {
1315

1416
}

0 commit comments

Comments
 (0)