-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loosen up ArrayFilterStrictRule for unions with clearly truthy/falsey…
… types
- Loading branch information
1 parent
568210b
commit 8afd4af
Showing
3 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
use PHPStan\Type\Type; | ||
use PHPStan\Type\UnionType; | ||
use PHPStan\Type\VerbosityLevel; | ||
use function count; | ||
use function sprintf; | ||
|
@@ -82,6 +83,41 @@ public function processNode(Node $node, Scope $scope): array | |
} | ||
|
||
if (count($args) === 1) { | ||
if ($this->treatPhpDocTypesAsCertain) { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
ondrejmirtes
Author
Member
|
||
$arrayType = $scope->getType($args[0]->value); | ||
} else { | ||
$arrayType = $scope->getNativeType($args[0]->value); | ||
} | ||
|
||
$itemType = $arrayType->getIterableValueType(); | ||
if ($itemType instanceof UnionType) { | ||
$hasTruthy = false; | ||
$hasFalsey = false; | ||
foreach ($itemType->getTypes() as $innerType) { | ||
$booleanType = $innerType->toBoolean(); | ||
if ($booleanType->isTrue()->yes()) { | ||
$hasTruthy = true; | ||
continue; | ||
} | ||
if ($booleanType->isFalse()->yes()) { | ||
$hasFalsey = true; | ||
continue; | ||
} | ||
|
||
$hasTruthy = false; | ||
$hasFalsey = false; | ||
break; | ||
} | ||
|
||
if ($hasTruthy && $hasFalsey) { | ||
return []; | ||
} | ||
} elseif ($itemType->isBoolean()->yes()) { | ||
return []; | ||
} elseif ($itemType->isArray()->yes()) { | ||
return []; | ||
} | ||
|
||
return [RuleErrorBuilder::message('Call to function array_filter() requires parameter #2 to be passed to avoid loose comparison semantics.')->build()]; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
namespace ArrayFilterAllow; | ||
|
||
use function array_filter; | ||
|
||
class Foo | ||
{ | ||
|
||
/** | ||
* @param array<self|null> $a | ||
*/ | ||
public function doFoo( | ||
array $a | ||
): array | ||
{ | ||
return array_filter($a); | ||
} | ||
|
||
/** | ||
* @param array<self> $a | ||
*/ | ||
public function doFoo2( | ||
array $a | ||
): array | ||
{ | ||
return array_filter($a); | ||
} | ||
|
||
/** | ||
* @param array<int|null> $a | ||
*/ | ||
public function doFoo3( | ||
array $a | ||
): array | ||
{ | ||
return array_filter($a); | ||
} | ||
|
||
/** @param array<bool> $a */ | ||
public function doFoo4(array $a): array | ||
{ | ||
return array_filter($a); | ||
} | ||
|
||
/** @param array<int> $a */ | ||
public function doFoo5(array $a): array | ||
{ | ||
return array_filter($a); | ||
} | ||
|
||
/** @param array<array<int>> $a */ | ||
public function doFoo6(array $a): array | ||
{ | ||
return array_filter($a); | ||
} | ||
|
||
} |
@ondrejmirtes would you be please so king to tag a new phpstan 1.10.x release to release this change? Thank you.