Skip to content

Commit f5b198c

Browse files
committed
ArrayFilterRule - tip message about treatPhpDocTypesAsCertain
1 parent 37f3c82 commit f5b198c

File tree

2 files changed

+45
-12
lines changed

2 files changed

+45
-12
lines changed

src/Rules/Functions/ArrayFilterRule.php

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,18 @@ public function processNode(Node $node, Scope $scope): array
5757

5858
if ($arrayType->isIterableAtLeastOnce()->no()) {
5959
$message = 'Parameter #1 $array (%s) to function array_filter is empty, call has no effect.';
60+
$errorBuilder = RuleErrorBuilder::message(sprintf(
61+
$message,
62+
$arrayType->describe(VerbosityLevel::value()),
63+
));
64+
if ($this->treatPhpDocTypesAsCertain) {
65+
$nativeArrayType = $scope->getNativeType($args[0]->value);
66+
if (!$nativeArrayType->isIterableAtLeastOnce()->no()) {
67+
$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%</>.');
68+
}
69+
}
6070
return [
61-
RuleErrorBuilder::message(sprintf(
62-
$message,
63-
$arrayType->describe(VerbosityLevel::value()),
64-
))->build(),
71+
$errorBuilder->build(),
6572
];
6673
}
6774

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

7178
if ($isSuperType->no()) {
7279
$message = 'Parameter #1 $array (%s) to function array_filter does not contain falsy values, the array will always stay the same.';
80+
$errorBuilder = RuleErrorBuilder::message(sprintf(
81+
$message,
82+
$arrayType->describe(VerbosityLevel::value()),
83+
));
84+
85+
if ($this->treatPhpDocTypesAsCertain) {
86+
$nativeArrayType = $scope->getNativeType($args[0]->value);
87+
$isNativeSuperType = $falsyType->isSuperTypeOf($nativeArrayType->getIterableValueType());
88+
if (!$isNativeSuperType->no()) {
89+
$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%</>.');
90+
}
91+
}
92+
7393
return [
74-
RuleErrorBuilder::message(sprintf(
75-
$message,
76-
$arrayType->describe(VerbosityLevel::value()),
77-
))->build(),
94+
$errorBuilder->build(),
7895
];
7996
}
8097

8198
if ($isSuperType->yes()) {
8299
$message = 'Parameter #1 $array (%s) to function array_filter contains falsy values only, the result will always be an empty array.';
100+
$errorBuilder = RuleErrorBuilder::message(sprintf(
101+
$message,
102+
$arrayType->describe(VerbosityLevel::value()),
103+
));
104+
105+
if ($this->treatPhpDocTypesAsCertain) {
106+
$nativeArrayType = $scope->getNativeType($args[0]->value);
107+
$isNativeSuperType = $falsyType->isSuperTypeOf($nativeArrayType->getIterableValueType());
108+
if (!$isNativeSuperType->yes()) {
109+
$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%</>.');
110+
}
111+
}
112+
83113
return [
84-
RuleErrorBuilder::message(sprintf(
85-
$message,
86-
$arrayType->describe(VerbosityLevel::value()),
87-
))->build(),
114+
$errorBuilder->build(),
88115
];
89116
}
90117

tests/PHPStan/Rules/Functions/ArrayFilterRuleTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ protected function getRule(): Rule
2020

2121
public function testFile(): void
2222
{
23+
$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%</>.';
2324
$expectedErrors = [
2425
[
2526
'Parameter #1 $array (array{1, 3}) to function array_filter does not contain falsy values, the array will always stay the same.',
@@ -40,6 +41,7 @@ public function testFile(): void
4041
[
4142
'Parameter #1 $array (array<stdClass>) to function array_filter does not contain falsy values, the array will always stay the same.',
4243
20,
44+
$tipText,
4345
],
4446
[
4547
'Parameter #1 $array (array{0}) to function array_filter contains falsy values only, the result will always be an empty array.',
@@ -60,6 +62,7 @@ public function testFile(): void
6062
[
6163
'Parameter #1 $array (array<false|null>) to function array_filter contains falsy values only, the result will always be an empty array.',
6264
27,
65+
$tipText,
6366
],
6467
[
6568
'Parameter #1 $array (array{}) to function array_filter is empty, call has no effect.',
@@ -72,10 +75,13 @@ public function testFile(): void
7275

7376
public function testBug2065WithPhpDocTypesAsCertain(): void
7477
{
78+
$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%</>.';
79+
7580
$expectedErrors = [
7681
[
7782
'Parameter #1 $array (array<class-string>) to function array_filter does not contain falsy values, the array will always stay the same.',
7883
12,
84+
$tipText,
7985
],
8086
];
8187

0 commit comments

Comments
 (0)