Skip to content

Commit 8e11fbe

Browse files
committed
Invalid @param annotation should be ignored
1 parent ec13aba commit 8e11fbe

5 files changed

+71
-5
lines changed

SlevomatCodingStandard/Sniffs/TypeHints/TypeHintDeclarationSniff.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -781,12 +781,14 @@ private function getFunctionParameterTypeHintsDefinitions(\PHP_CodeSniffer_File
781781
continue;
782782
}
783783

784-
$parameterAnnotationParts = preg_split('~\\s+~', $parameterAnnotation->getContent(), 2);
785-
$parameterTypeHintDefinition = $parameterAnnotationParts[0];
786-
if (isset($parameterAnnotationParts[1]) && preg_match('~^(?:\.{3}\\s*)?(\$\\S+)~', $parameterAnnotationParts[1], $matches)) {
787-
$parametersTypeHintsDefinitions[$matches[1]] = $parameterTypeHintDefinition;
784+
if (!preg_match('~^([^$]\\S*)(?:\\s+(?:\.{3}\\s*)?(\$\\S+))?~', $parameterAnnotation->getContent(), $matches)) {
785+
continue;
786+
}
787+
788+
if (isset($matches[2])) {
789+
$parametersTypeHintsDefinitions[$matches[2]] = $matches[1];
788790
} elseif (isset($parametersNames[$parameterAnnotationNo])) {
789-
$parametersTypeHintsDefinitions[$parametersNames[$parameterAnnotationNo]] = $parameterTypeHintDefinition;
791+
$parametersTypeHintsDefinitions[$parametersNames[$parameterAnnotationNo]] = $matches[1];
790792
}
791793
}
792794

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ function optionalWithoutTypeHint(string $a = null)
6464

6565
}
6666

67+
/**
68+
* @param $a string
69+
*/
70+
function invalidAnnotation($a)
71+
{
72+
73+
}
74+
6775
class Foo
6876
{
6977

@@ -127,4 +135,12 @@ public function optionalWithoutTypeHint(string $a = null)
127135

128136
}
129137

138+
/**
139+
* @param $a string
140+
*/
141+
private function invalidAnnotation($a)
142+
{
143+
144+
}
145+
130146
}

tests/Sniffs/TypeHints/data/fixableParameterTypeHintsWithDisabledNullableTypeHints.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ function optionalWithoutTypeHint($a = null)
6464

6565
}
6666

67+
/**
68+
* @param $a string
69+
*/
70+
function invalidAnnotation($a)
71+
{
72+
73+
}
74+
6775
class Foo
6876
{
6977

@@ -127,4 +135,12 @@ public function optionalWithoutTypeHint($a = null)
127135

128136
}
129137

138+
/**
139+
* @param $a string
140+
*/
141+
private function invalidAnnotation($a)
142+
{
143+
144+
}
145+
130146
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,14 @@ function traversableDateTimeImmutable($a)
174174

175175
}
176176

177+
/**
178+
* @param $a string
179+
*/
180+
function invalidAnnotation($a)
181+
{
182+
183+
}
184+
177185
abstract class Foo
178186
{
179187

@@ -356,4 +364,12 @@ protected function traversableDateTimeImmutable($a)
356364

357365
}
358366

367+
/**
368+
* @param $a string
369+
*/
370+
private function invalidAnnotation($a)
371+
{
372+
373+
}
374+
359375
}

tests/Sniffs/TypeHints/data/fixableParameterTypeHintsWithEnabledNullableTypeHints.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,14 @@ function traversableDateTimeImmutable($a)
174174

175175
}
176176

177+
/**
178+
* @param $a string
179+
*/
180+
function invalidAnnotation($a)
181+
{
182+
183+
}
184+
177185
abstract class Foo
178186
{
179187

@@ -356,4 +364,12 @@ protected function traversableDateTimeImmutable($a)
356364

357365
}
358366

367+
/**
368+
* @param $a string
369+
*/
370+
private function invalidAnnotation($a)
371+
{
372+
373+
}
374+
359375
}

0 commit comments

Comments
 (0)