Skip to content

Commit 7815b13

Browse files
committed
ParameterTypeHintSniff, PropertyTypeHintSniff, ReturnTypeHintSniff: Fixed false positives
1 parent e7286b9 commit 7815b13

File tree

7 files changed

+30
-20
lines changed

7 files changed

+30
-20
lines changed

SlevomatCodingStandard/Helpers/AnnotationTypeHelper.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
use PHPStan\PhpDocParser\Ast\Type\ThisTypeNode;
2020
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
2121
use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode;
22-
use function array_filter;
2322
use function array_merge;
2423
use function count;
2524
use function in_array;
@@ -661,12 +660,16 @@ public static function getTraversableTypeHintsFromType(
661660
return [];
662661
}
663662

664-
return array_filter($typeHints, static function (string $typeHint) use ($phpcsFile, $pointer, $traversableTypeHints): bool {
665-
return TypeHintHelper::isTraversableType(
663+
foreach ($typeHints as $typeHint) {
664+
if (!TypeHintHelper::isTraversableType(
666665
TypeHintHelper::getFullyQualifiedTypeHint($phpcsFile, $pointer, $typeHint),
667666
$traversableTypeHints
668-
);
669-
});
667+
)) {
668+
return [];
669+
}
670+
}
671+
672+
return $typeHints;
670673
}
671674

672675
/**

SlevomatCodingStandard/Sniffs/TypeHints/ParameterTypeHintSniff.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -245,17 +245,13 @@ private function checkTypeHints(
245245

246246
$itemsSpecificationTypeHint = AnnotationTypeHelper::getItemsSpecificationTypeFromType($parameterTypeNode);
247247
if ($itemsSpecificationTypeHint !== null) {
248-
$possibleParameterTypeHints = AnnotationTypeHelper::getTraversableTypeHintsFromType(
248+
$typeHints = AnnotationTypeHelper::getTraversableTypeHintsFromType(
249249
$parameterTypeNode,
250250
$phpcsFile,
251251
$functionPointer,
252252
$this->getTraversableTypeHints(),
253253
$canTryUnionTypeHint
254254
);
255-
256-
if (count($possibleParameterTypeHints) > 0) {
257-
$typeHints = $possibleParameterTypeHints;
258-
}
259255
}
260256
}
261257

SlevomatCodingStandard/Sniffs/TypeHints/PropertyTypeHintSniff.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -267,17 +267,13 @@ private function checkTypeHint(
267267

268268
$itemsSpecificationTypeHint = AnnotationTypeHelper::getItemsSpecificationTypeFromType($typeNode);
269269
if ($itemsSpecificationTypeHint !== null) {
270-
$possibleTypeHints = AnnotationTypeHelper::getTraversableTypeHintsFromType(
270+
$typeHints = AnnotationTypeHelper::getTraversableTypeHintsFromType(
271271
$typeNode,
272272
$phpcsFile,
273273
$propertyPointer,
274274
$this->getTraversableTypeHints(),
275275
$this->enableUnionTypeHint
276276
);
277-
278-
if (count($possibleTypeHints) > 0) {
279-
$typeHints = $possibleTypeHints;
280-
}
281277
}
282278
}
283279

SlevomatCodingStandard/Sniffs/TypeHints/ReturnTypeHintSniff.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -287,17 +287,13 @@ private function checkFunctionTypeHint(
287287

288288
$itemsSpecificationTypeHint = AnnotationTypeHelper::getItemsSpecificationTypeFromType($returnTypeNode);
289289
if ($itemsSpecificationTypeHint !== null) {
290-
$possibleReturnTypeHints = AnnotationTypeHelper::getTraversableTypeHintsFromType(
290+
$typeHints = AnnotationTypeHelper::getTraversableTypeHintsFromType(
291291
$returnTypeNode,
292292
$phpcsFile,
293293
$functionPointer,
294294
$this->getTraversableTypeHints(),
295295
$canTryUnionTypeHint
296296
);
297-
298-
if (count($possibleReturnTypeHints) > 0) {
299-
$typeHints = $possibleReturnTypeHints;
300-
}
301297
}
302298
}
303299

tests/Sniffs/TypeHints/data/parameterTypeHintWithUnionNoErrors.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,11 @@ private function withTrue(string|bool $a)
1414
{
1515
}
1616

17+
/**
18+
* @param ArrayHash|array|mixed[] $a
19+
*/
20+
public function arrayAndArrayHash($a)
21+
{
22+
}
23+
1724
}

tests/Sniffs/TypeHints/data/propertyTypeHintEnabledNativeWithUnionNoErrors.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,9 @@ class Whatever
88
/** @var string|true */
99
private string|bool $withTrue;
1010

11+
/**
12+
* @var ArrayHash|array|mixed[]
13+
*/
14+
protected $arrayAndArrayHash;
15+
1116
}

tests/Sniffs/TypeHints/data/returnTypeHintWithUnionNoErrors.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,11 @@ private function withTrue(): string|bool
1212
{
1313
}
1414

15+
/**
16+
* @return ArrayHash|array|mixed[]
17+
*/
18+
public function arrayAndArrayHash()
19+
{
20+
}
21+
1522
}

0 commit comments

Comments
 (0)