From c51edb898bebd36aac70a190c6a41a7c056bb5b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaroslav=20Hansl=C3=ADk?= Date: Wed, 14 Dec 2022 09:46:04 +0100 Subject: [PATCH] SlevomatCodingStandard.TypeHints.ParameterTypeHintSpacing: Fixed false positive when parameter has attribute but no type hint --- .../Sniffs/TypeHints/ParameterTypeHintSpacingSniff.php | 10 +++++++++- .../data/parameterTypeHintSpacingNoErrors.php | 6 ++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/SlevomatCodingStandard/Sniffs/TypeHints/ParameterTypeHintSpacingSniff.php b/SlevomatCodingStandard/Sniffs/TypeHints/ParameterTypeHintSpacingSniff.php index 34f57df16..69beededb 100644 --- a/SlevomatCodingStandard/Sniffs/TypeHints/ParameterTypeHintSpacingSniff.php +++ b/SlevomatCodingStandard/Sniffs/TypeHints/ParameterTypeHintSpacingSniff.php @@ -8,6 +8,7 @@ use SlevomatCodingStandard\Helpers\TypeHintHelper; use function array_keys; use function sprintf; +use const T_ATTRIBUTE_END; use const T_BITWISE_AND; use const T_COMMA; use const T_ELLIPSIS; @@ -63,7 +64,14 @@ public function process(File $phpcsFile, $functionPointer): void $parameterEndPointer = $parametersEndPointer; } - $typeHintEndPointer = TokenHelper::findPrevious($phpcsFile, $typeHintTokenCodes, $parameterPointer - 1, $parameterStartPointer); + $attributeCloserPointer = TokenHelper::findPrevious($phpcsFile, T_ATTRIBUTE_END, $parameterPointer - 1, $parameterStartPointer); + + $typeHintEndPointer = TokenHelper::findPrevious( + $phpcsFile, + $typeHintTokenCodes, + $parameterPointer - 1, + $attributeCloserPointer ?? $parameterStartPointer + ); if ($typeHintEndPointer === null) { continue; } diff --git a/tests/Sniffs/TypeHints/data/parameterTypeHintSpacingNoErrors.php b/tests/Sniffs/TypeHints/data/parameterTypeHintSpacingNoErrors.php index bd8850e46..04cbc2f51 100644 --- a/tests/Sniffs/TypeHints/data/parameterTypeHintSpacingNoErrors.php +++ b/tests/Sniffs/TypeHints/data/parameterTypeHintSpacingNoErrors.php @@ -1,4 +1,4 @@ -= 8.0 += 8.1 $a = function (?bool $a, ?string &$b, ?int ...$c) { @@ -8,7 +8,9 @@ function b( ?bool $a, array $b, - $c + #[SomeAttribute] + $c, + $d ) { }