forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FilterVarDynamicReturnTypeExtension.php
251 lines (204 loc) · 7.71 KB
/
FilterVarDynamicReturnTypeExtension.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<?php declare(strict_types = 1);
namespace PHPStan\Type\Php;
use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\Accessory\AccessoryNonEmptyStringType;
use PHPStan\Type\ArrayType;
use PHPStan\Type\BooleanType;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
use PHPStan\Type\ErrorType;
use PHPStan\Type\FloatType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\MixedType;
use PHPStan\Type\NullType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
class FilterVarDynamicReturnTypeExtension implements DynamicFunctionReturnTypeExtension
{
/**
* All validation filters match 0x100.
*/
private const VALIDATION_FILTER_BITMASK = 0x100;
private ReflectionProvider $reflectionProvider;
private ConstantStringType $flagsString;
/** @var array<int, Type>|null */
private ?array $filterTypeMap = null;
public function __construct(ReflectionProvider $reflectionProvider)
{
$this->reflectionProvider = $reflectionProvider;
$this->flagsString = new ConstantStringType('flags');
}
/**
* @return array<int, Type>
*/
private function getFilterTypeMap(): array
{
if ($this->filterTypeMap !== null) {
return $this->filterTypeMap;
}
$booleanType = new BooleanType();
$floatType = new FloatType();
$intType = new IntegerType();
$stringType = new StringType();
$this->filterTypeMap = [
$this->getConstant('FILTER_UNSAFE_RAW') => $stringType,
$this->getConstant('FILTER_SANITIZE_EMAIL') => $stringType,
$this->getConstant('FILTER_SANITIZE_ENCODED') => $stringType,
$this->getConstant('FILTER_SANITIZE_NUMBER_FLOAT') => $stringType,
$this->getConstant('FILTER_SANITIZE_NUMBER_INT') => $stringType,
$this->getConstant('FILTER_SANITIZE_SPECIAL_CHARS') => $stringType,
$this->getConstant('FILTER_SANITIZE_STRING') => $stringType,
$this->getConstant('FILTER_SANITIZE_URL') => $stringType,
$this->getConstant('FILTER_VALIDATE_BOOLEAN') => $booleanType,
$this->getConstant('FILTER_VALIDATE_DOMAIN') => $stringType,
$this->getConstant('FILTER_VALIDATE_EMAIL') => $stringType,
$this->getConstant('FILTER_VALIDATE_FLOAT') => $floatType,
$this->getConstant('FILTER_VALIDATE_INT') => $intType,
$this->getConstant('FILTER_VALIDATE_IP') => $stringType,
$this->getConstant('FILTER_VALIDATE_MAC') => $stringType,
$this->getConstant('FILTER_VALIDATE_REGEXP') => $stringType,
$this->getConstant('FILTER_VALIDATE_URL') => $stringType,
];
if ($this->reflectionProvider->hasConstant(new Node\Name('FILTER_SANITIZE_MAGIC_QUOTES'), null)) {
$this->filterTypeMap[$this->getConstant('FILTER_SANITIZE_MAGIC_QUOTES')] = $stringType;
}
if ($this->reflectionProvider->hasConstant(new Node\Name('FILTER_SANITIZE_ADD_SLASHES'), null)) {
$this->filterTypeMap[$this->getConstant('FILTER_SANITIZE_ADD_SLASHES')] = $stringType;
}
return $this->filterTypeMap;
}
private function getConstant(string $constantName): int
{
$constant = $this->reflectionProvider->getConstant(new Node\Name($constantName), null);
$valueType = $constant->getValueType();
if (!$valueType instanceof ConstantIntegerType) {
throw new \PHPStan\ShouldNotHappenException(sprintf('Constant %s does not have integer type.', $constantName));
}
return $valueType->getValue();
}
public function isFunctionSupported(FunctionReflection $functionReflection): bool
{
return strtolower($functionReflection->getName()) === 'filter_var';
}
public function getTypeFromFunctionCall(
FunctionReflection $functionReflection,
FuncCall $functionCall,
Scope $scope
): Type
{
$mixedType = new MixedType();
$filterArg = $functionCall->args[1] ?? null;
if ($filterArg === null) {
$filterValue = $this->getConstant('FILTER_DEFAULT');
} else {
$filterType = $scope->getType($filterArg->value);
if (!$filterType instanceof ConstantIntegerType) {
return $mixedType;
}
$filterValue = $filterType->getValue();
}
$flagsArg = $functionCall->args[2] ?? null;
$inputType = $scope->getType($functionCall->args[0]->value);
$exactType = $this->determineExactType($inputType, $filterValue);
if ($exactType !== null) {
$type = $exactType;
} else {
$type = $this->getFilterTypeMap()[$filterValue] ?? $mixedType;
$otherType = $this->getOtherType($flagsArg, $scope);
if ($inputType->isNonEmptyString()->yes()
&& $type instanceof StringType
&& !$this->canStringBeSanitized($filterValue, $flagsArg, $scope)) {
$type = new IntersectionType([$type, new AccessoryNonEmptyStringType()]);
}
if ($otherType->isSuperTypeOf($type)->no()) {
$type = new UnionType([$type, $otherType]);
}
}
if ($this->hasFlag($this->getConstant('FILTER_FORCE_ARRAY'), $flagsArg, $scope)) {
return new ArrayType(new MixedType(), $type);
}
return $type;
}
private function determineExactType(Type $in, int $filterValue): ?Type
{
if (($filterValue === $this->getConstant('FILTER_VALIDATE_BOOLEAN') && $in instanceof BooleanType)
|| ($filterValue === $this->getConstant('FILTER_VALIDATE_INT') && $in instanceof IntegerType)
|| ($filterValue === $this->getConstant('FILTER_VALIDATE_FLOAT') && $in instanceof FloatType)) {
return $in;
}
if ($filterValue === $this->getConstant('FILTER_VALIDATE_FLOAT') && $in instanceof IntegerType) {
return $in->toFloat();
}
return null;
}
private function getOtherType(?Node\Arg $flagsArg, Scope $scope): Type
{
$falseType = new ConstantBooleanType(false);
if ($flagsArg === null) {
return $falseType;
}
$defaultType = $this->getDefault($flagsArg, $scope);
if ($defaultType !== null) {
return $defaultType;
}
if ($this->hasFlag($this->getConstant('FILTER_NULL_ON_FAILURE'), $flagsArg, $scope)) {
return new NullType();
}
return $falseType;
}
private function getDefault(Node\Arg $expression, Scope $scope): ?Type
{
$exprType = $scope->getType($expression->value);
if (!$exprType instanceof ConstantArrayType) {
return null;
}
$optionsType = $exprType->getOffsetValueType(new ConstantStringType('options'));
if (!$optionsType instanceof ConstantArrayType) {
return null;
}
$defaultType = $optionsType->getOffsetValueType(new ConstantStringType('default'));
if (!$defaultType instanceof ErrorType) {
return $defaultType;
}
return null;
}
private function hasFlag(int $flag, ?Node\Arg $expression, Scope $scope): bool
{
if ($expression === null) {
return false;
}
$type = $this->getFlagsValue($scope->getType($expression->value));
return $type instanceof ConstantIntegerType && ($type->getValue() & $flag) === $flag;
}
private function getFlagsValue(Type $exprType): Type
{
if (!$exprType instanceof ConstantArrayType) {
return $exprType;
}
return $exprType->getOffsetValueType($this->flagsString);
}
private function canStringBeSanitized(int $filterValue, ?Node\Arg $flagsArg, Scope $scope): bool
{
// If it is a validation filter, the string will not be changed
if (($filterValue & self::VALIDATION_FILTER_BITMASK) !== 0) {
return false;
}
// FILTER_DEFAULT will not sanitize, unless it has FILTER_FLAG_STRIP_LOW,
// FILTER_FLAG_STRIP_HIGH, or FILTER_FLAG_STRIP_BACKTICK
if ($filterValue === $this->getConstant('FILTER_DEFAULT')) {
return $this->hasFlag($this->getConstant('FILTER_FLAG_STRIP_LOW'), $flagsArg, $scope)
|| $this->hasFlag($this->getConstant('FILTER_FLAG_STRIP_HIGH'), $flagsArg, $scope)
|| $this->hasFlag($this->getConstant('FILTER_FLAG_STRIP_BACKTICK'), $flagsArg, $scope);
}
return true;
}
}