-
Notifications
You must be signed in to change notification settings - Fork 426
/
ArrayFilterRule.php
121 lines (101 loc) · 3.57 KB
/
ArrayFilterRule.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Functions;
use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\StaticTypeFactory;
use PHPStan\Type\VerbosityLevel;
use function count;
use function sprintf;
use function strtolower;
/**
* @implements Rule<Node\Expr\FuncCall>
*/
class ArrayFilterRule implements Rule
{
public function __construct(
private ReflectionProvider $reflectionProvider,
private bool $treatPhpDocTypesAsCertain,
)
{
}
public function getNodeType(): string
{
return FuncCall::class;
}
public function processNode(Node $node, Scope $scope): array
{
if (!($node->name instanceof Node\Name)) {
return [];
}
$functionName = $this->reflectionProvider->resolveFunctionName($node->name, $scope);
if ($functionName === null || strtolower($functionName) !== 'array_filter') {
return [];
}
$args = $node->getArgs();
if (count($args) !== 1) {
return [];
}
if ($this->treatPhpDocTypesAsCertain) {
$arrayType = $scope->getType($args[0]->value);
} else {
$arrayType = $scope->getNativeType($args[0]->value);
}
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 [
$errorBuilder->build(),
];
}
$falsyType = StaticTypeFactory::falsey();
$isSuperType = $falsyType->isSuperTypeOf($arrayType->getIterableValueType());
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 [
$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 [
$errorBuilder->build(),
];
}
return [];
}
}