Skip to content

Commit 85c3791

Browse files
committed
ParameterTypeHintSniff: Fixed false positive
1 parent bb9db1a commit 85c3791

File tree

5 files changed

+41
-2
lines changed

5 files changed

+41
-2
lines changed

SlevomatCodingStandard/Sniffs/TypeHints/ParameterTypeHintSniff.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ private function checkTypeHints(File $phpcsFile, int $functionPointer, array $pa
151151
$typeHints[] = AnnotationTypeHelper::getTypeHintFromOneType($parameterTypeNode);
152152

153153
} elseif ($parameterTypeNode instanceof UnionTypeNode || $parameterTypeNode instanceof IntersectionTypeNode) {
154+
$traversableTypeHints = [];
154155
foreach ($parameterTypeNode->types as $typeNode) {
155156
if (!AnnotationTypeHelper::containsOneType($typeNode)) {
156157
continue 2;
@@ -159,7 +160,22 @@ private function checkTypeHints(File $phpcsFile, int $functionPointer, array $pa
159160
/** @var ArrayTypeNode|ArrayShapeNode|IdentifierTypeNode|ThisTypeNode|GenericTypeNode|CallableTypeNode $typeNode */
160161
$typeNode = $typeNode;
161162

162-
$typeHints[] = AnnotationTypeHelper::getTypeHintFromOneType($typeNode);
163+
$typeHint = AnnotationTypeHelper::getTypeHintFromOneType($typeNode);
164+
165+
if (
166+
!$typeNode instanceof ArrayTypeNode
167+
&& !$typeNode instanceof ArrayShapeNode
168+
&& TypeHintHelper::isTraversableType(TypeHintHelper::getFullyQualifiedTypeHint($phpcsFile, $functionPointer, $typeHint), $this->getTraversableTypeHints())
169+
) {
170+
$traversableTypeHints[] = $typeHint;
171+
}
172+
173+
$typeHints[] = $typeHint;
174+
}
175+
176+
$traversableTypeHints = array_values(array_unique($traversableTypeHints));
177+
if (count($traversableTypeHints) > 1) {
178+
continue;
163179
}
164180
}
165181

tests/Sniffs/TypeHints/ParameterTypeHintSniffTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function testErrors(): void
2222
'traversableTypeHints' => ['Traversable'],
2323
]);
2424

25-
self::assertSame(28, $report->getErrorCount());
25+
self::assertSame(29, $report->getErrorCount());
2626

2727
self::assertSniffError($report, 6, ParameterTypeHintSniff::CODE_MISSING_ANY_TYPE_HINT);
2828
self::assertSniffError($report, 14, ParameterTypeHintSniff::CODE_MISSING_NATIVE_TYPE_HINT);
@@ -52,6 +52,7 @@ public function testErrors(): void
5252
self::assertSniffError($report, 164, ParameterTypeHintSniff::CODE_MISSING_NATIVE_TYPE_HINT);
5353
self::assertSniffError($report, 171, ParameterTypeHintSniff::CODE_MISSING_NATIVE_TYPE_HINT);
5454
self::assertSniffError($report, 178, ParameterTypeHintSniff::CODE_MISSING_NATIVE_TYPE_HINT);
55+
self::assertSniffError($report, 185, ParameterTypeHintSniff::CODE_MISSING_NATIVE_TYPE_HINT);
5556

5657
self::assertAllFixedInFile($report);
5758
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,4 +174,11 @@ public function nullable(?int $a)
174174
{
175175
}
176176

177+
/**
178+
* @param mixed[]|array $a
179+
*/
180+
public function traversableArray(array $a)
181+
{
182+
}
183+
177184
}

tests/Sniffs/TypeHints/data/parameterTypeHintErrors.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,4 +179,11 @@ public function nullable($a)
179179
{
180180
}
181181

182+
/**
183+
* @param mixed[]|array $a
184+
*/
185+
public function traversableArray($a)
186+
{
187+
}
188+
182189
}

tests/Sniffs/TypeHints/data/parameterTypeHintNoErrors.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use Doctrine\Common\Collections\ArrayCollection;
34
use SlevomatCodingStandard\Helpers\ParameterTypeHint;
45
use SlevomatCodingStandard\Helpers\PropertyTypeHint;
56
use SlevomatCodingStandard\Helpers\ReturnTypeHint;
@@ -175,4 +176,11 @@ public function unionWithDifferentNullableBase($a)
175176
{
176177
}
177178

179+
/**
180+
* @param mixed[]|array|Traversable $a
181+
*/
182+
public function moreTraverasableTypes($a)
183+
{
184+
}
185+
178186
}

0 commit comments

Comments
 (0)