Skip to content

Commit

Permalink
ArrayFilterRule - tip message about treatPhpDocTypesAsCertain
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Feb 15, 2024
1 parent 37f3c82 commit f5b198c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 12 deletions.
51 changes: 39 additions & 12 deletions src/Rules/Functions/ArrayFilterRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,18 @@ public function processNode(Node $node, Scope $scope): array

if ($arrayType->isIterableAtLeastOnce()->no()) {
$message = 'Parameter #1 $array (%s) to function array_filter is empty, call has no effect.';
$errorBuilder = RuleErrorBuilder::message(sprintf(
$message,
$arrayType->describe(VerbosityLevel::value()),
));
if ($this->treatPhpDocTypesAsCertain) {
$nativeArrayType = $scope->getNativeType($args[0]->value);
if (!$nativeArrayType->isIterableAtLeastOnce()->no()) {
$errorBuilder->tip('Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.');
}
}
return [
RuleErrorBuilder::message(sprintf(
$message,
$arrayType->describe(VerbosityLevel::value()),
))->build(),
$errorBuilder->build(),
];
}

Expand All @@ -70,21 +77,41 @@ public function processNode(Node $node, Scope $scope): array

if ($isSuperType->no()) {
$message = 'Parameter #1 $array (%s) to function array_filter does not contain falsy values, the array will always stay the same.';
$errorBuilder = RuleErrorBuilder::message(sprintf(
$message,
$arrayType->describe(VerbosityLevel::value()),
));

if ($this->treatPhpDocTypesAsCertain) {
$nativeArrayType = $scope->getNativeType($args[0]->value);
$isNativeSuperType = $falsyType->isSuperTypeOf($nativeArrayType->getIterableValueType());
if (!$isNativeSuperType->no()) {
$errorBuilder->tip('Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.');
}
}

return [
RuleErrorBuilder::message(sprintf(
$message,
$arrayType->describe(VerbosityLevel::value()),
))->build(),
$errorBuilder->build(),
];
}

if ($isSuperType->yes()) {
$message = 'Parameter #1 $array (%s) to function array_filter contains falsy values only, the result will always be an empty array.';
$errorBuilder = RuleErrorBuilder::message(sprintf(
$message,
$arrayType->describe(VerbosityLevel::value()),
));

if ($this->treatPhpDocTypesAsCertain) {
$nativeArrayType = $scope->getNativeType($args[0]->value);
$isNativeSuperType = $falsyType->isSuperTypeOf($nativeArrayType->getIterableValueType());
if (!$isNativeSuperType->yes()) {
$errorBuilder->tip('Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.');
}
}

return [
RuleErrorBuilder::message(sprintf(
$message,
$arrayType->describe(VerbosityLevel::value()),
))->build(),
$errorBuilder->build(),
];
}

Expand Down
6 changes: 6 additions & 0 deletions tests/PHPStan/Rules/Functions/ArrayFilterRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ protected function getRule(): Rule

public function testFile(): void
{
$tipText = 'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.';
$expectedErrors = [
[
'Parameter #1 $array (array{1, 3}) to function array_filter does not contain falsy values, the array will always stay the same.',
Expand All @@ -40,6 +41,7 @@ public function testFile(): void
[
'Parameter #1 $array (array<stdClass>) to function array_filter does not contain falsy values, the array will always stay the same.',
20,
$tipText,
],
[
'Parameter #1 $array (array{0}) to function array_filter contains falsy values only, the result will always be an empty array.',
Expand All @@ -60,6 +62,7 @@ public function testFile(): void
[
'Parameter #1 $array (array<false|null>) to function array_filter contains falsy values only, the result will always be an empty array.',
27,
$tipText,
],
[
'Parameter #1 $array (array{}) to function array_filter is empty, call has no effect.',
Expand All @@ -72,10 +75,13 @@ public function testFile(): void

public function testBug2065WithPhpDocTypesAsCertain(): void
{
$tipText = 'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.';

$expectedErrors = [
[
'Parameter #1 $array (array<class-string>) to function array_filter does not contain falsy values, the array will always stay the same.',
12,
$tipText,
],
];

Expand Down

0 comments on commit f5b198c

Please sign in to comment.