-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathArrayFilterStrictRule.php
158 lines (128 loc) · 3.89 KB
/
ArrayFilterStrictRule.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Functions;
use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Name;
use PHPStan\Analyser\ArgumentsNormalizer;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Reflection\ReflectionProvider;
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;
/**
* @implements Rule<FuncCall>
*/
class ArrayFilterStrictRule implements Rule
{
private ReflectionProvider $reflectionProvider;
private bool $treatPhpDocTypesAsCertain;
private bool $checkNullables;
public function __construct(
ReflectionProvider $reflectionProvider,
bool $treatPhpDocTypesAsCertain,
bool $checkNullables
)
{
$this->reflectionProvider = $reflectionProvider;
$this->treatPhpDocTypesAsCertain = $treatPhpDocTypesAsCertain;
$this->checkNullables = $checkNullables;
}
public function getNodeType(): string
{
return FuncCall::class;
}
public function processNode(Node $node, Scope $scope): array
{
if (!$node->name instanceof Name) {
return [];
}
if (!$this->reflectionProvider->hasFunction($node->name, $scope)) {
return [];
}
$functionReflection = $this->reflectionProvider->getFunction($node->name, $scope);
if ($functionReflection->getName() !== 'array_filter') {
return [];
}
$parametersAcceptor = ParametersAcceptorSelector::selectFromArgs(
$scope,
$node->getArgs(),
$functionReflection->getVariants(),
$functionReflection->getNamedArgumentsVariants(),
);
$normalizedFuncCall = ArgumentsNormalizer::reorderFuncArguments($parametersAcceptor, $node);
if ($normalizedFuncCall === null) {
return [];
}
$args = $normalizedFuncCall->getArgs();
if (count($args) === 0) {
return [];
}
if (count($args) === 1) {
$arrayType = $scope->getType($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.')
->identifier('arrayFilter.strict')
->build(),
];
}
$nativeCallbackType = $scope->getNativeType($args[1]->value);
if ($this->treatPhpDocTypesAsCertain) {
$callbackType = $scope->getType($args[1]->value);
} else {
$callbackType = $nativeCallbackType;
}
if ($this->isCallbackTypeNull($callbackType)) {
$message = 'Parameter #2 of array_filter() cannot be null to avoid loose comparison semantics (%s given).';
$errorBuilder = RuleErrorBuilder::message(sprintf(
$message,
$callbackType->describe(VerbosityLevel::typeOnly()),
))->identifier('arrayFilter.strict');
if (!$this->isCallbackTypeNull($nativeCallbackType) && $this->treatPhpDocTypesAsCertain) {
$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 [];
}
private function isCallbackTypeNull(Type $callbackType): bool
{
if ($callbackType->isNull()->yes()) {
return true;
}
if ($callbackType->isNull()->no()) {
return false;
}
return $this->checkNullables;
}
}