forked from phpstan/phpstan-src
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIssetCheck.php
319 lines (268 loc) · 10.2 KB
/
IssetCheck.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
<?php declare(strict_types = 1);
namespace PHPStan\Rules;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PHPStan\Analyser\Scope;
use PHPStan\Node\Expr\PropertyInitializationExpr;
use PHPStan\Rules\Properties\PropertyDescriptor;
use PHPStan\Rules\Properties\PropertyReflectionFinder;
use PHPStan\Type\NeverType;
use PHPStan\Type\Type;
use PHPStan\Type\VerbosityLevel;
use function is_string;
use function sprintf;
/**
* @phpstan-type ErrorIdentifier = 'empty'|'isset'|'nullCoalesce'
*/
final class IssetCheck
{
public function __construct(
private PropertyDescriptor $propertyDescriptor,
private PropertyReflectionFinder $propertyReflectionFinder,
private bool $checkAdvancedIsset,
private bool $treatPhpDocTypesAsCertain,
)
{
}
/**
* @param ErrorIdentifier $identifier
* @param callable(Type): ?string $typeMessageCallback
*/
public function check(Expr $expr, Scope $scope, string $operatorDescription, string $identifier, callable $typeMessageCallback, ?IdentifierRuleError $error = null): ?IdentifierRuleError
{
// mirrored in PHPStan\Analyser\MutatingScope::issetCheck()
if ($expr instanceof Node\Expr\Variable && is_string($expr->name)) {
$hasVariable = $scope->hasVariableType($expr->name);
if ($hasVariable->maybe()) {
return null;
}
if ($error === null) {
if ($hasVariable->yes()) {
if ($expr->name === '_SESSION') {
return null;
}
$type = $this->treatPhpDocTypesAsCertain ? $scope->getType($expr) : $scope->getNativeType($expr);
if (!$type instanceof NeverType) {
return $this->generateError(
$type,
sprintf('Variable $%s %s always exists and', $expr->name, $operatorDescription),
$typeMessageCallback,
$identifier,
'variable',
);
}
}
return RuleErrorBuilder::message(sprintf('Variable $%s %s is never defined.', $expr->name, $operatorDescription))
->identifier(sprintf('%s.variable', $identifier))
->build();
}
return $error;
} elseif ($expr instanceof Node\Expr\ArrayDimFetch && $expr->dim !== null) {
$type = $this->treatPhpDocTypesAsCertain
? $scope->getType($expr->var)
: $scope->getNativeType($expr->var);
if (!$type->isOffsetAccessible()->yes()) {
return $error ?? $this->checkUndefined($expr->var, $scope, $operatorDescription, $identifier);
}
$dimType = $this->treatPhpDocTypesAsCertain
? $scope->getType($expr->dim)
: $scope->getNativeType($expr->dim);
$hasOffsetValue = $type->hasOffsetValueType($dimType);
if ($hasOffsetValue->no()) {
if (!$this->checkAdvancedIsset) {
return null;
}
return RuleErrorBuilder::message(
sprintf(
'Offset %s on %s %s does not exist.',
$dimType->describe(VerbosityLevel::value()),
$type->describe(VerbosityLevel::value()),
$operatorDescription,
),
)->identifier(sprintf('%s.offset', $identifier))->build();
}
// If offset cannot be null, store this error message and see if one of the earlier offsets is.
// E.g. $array['a']['b']['c'] ?? null; is a valid coalesce if a OR b or C might be null.
if ($hasOffsetValue->yes() || $scope->hasExpressionType($expr)->yes()) {
if (!$this->checkAdvancedIsset) {
return null;
}
$error ??= $this->generateError($type->getOffsetValueType($dimType), sprintf(
'Offset %s on %s %s always exists and',
$dimType->describe(VerbosityLevel::value()),
$type->describe(VerbosityLevel::value()),
$operatorDescription,
), $typeMessageCallback, $identifier, 'offset');
if ($error !== null) {
return $this->check($expr->var, $scope, $operatorDescription, $identifier, $typeMessageCallback, $error);
}
}
// Has offset, it is nullable
return null;
} elseif ($expr instanceof Node\Expr\PropertyFetch || $expr instanceof Node\Expr\StaticPropertyFetch) {
$propertyReflection = $this->propertyReflectionFinder->findPropertyReflectionFromNode($expr, $scope);
if ($propertyReflection === null) {
if ($expr instanceof Node\Expr\PropertyFetch) {
return $this->checkUndefined($expr->var, $scope, $operatorDescription, $identifier);
}
if ($expr->class instanceof Expr) {
return $this->checkUndefined($expr->class, $scope, $operatorDescription, $identifier);
}
return null;
}
if (!$propertyReflection->isNative()) {
if ($expr instanceof Node\Expr\PropertyFetch) {
return $this->checkUndefined($expr->var, $scope, $operatorDescription, $identifier);
}
if ($expr->class instanceof Expr) {
return $this->checkUndefined($expr->class, $scope, $operatorDescription, $identifier);
}
return null;
}
if ($propertyReflection->hasNativeType() && !$propertyReflection->isVirtual()->yes()) {
if (
$expr instanceof Node\Expr\PropertyFetch
&& $expr->name instanceof Node\Identifier
&& $expr->var instanceof Expr\Variable
&& $expr->var->name === 'this'
&& $propertyReflection->getNativeType()->isNull()->no()
&& $scope->hasExpressionType(new PropertyInitializationExpr($propertyReflection->getName()))->yes()
) {
return $this->generateError(
$propertyReflection->getNativeType(),
sprintf(
'%s %s',
$this->propertyDescriptor->describeProperty($propertyReflection, $scope, $expr),
$operatorDescription,
),
$typeMessageCallback,
$identifier,
'propertyNeverNullOrUninitialized',
);
}
if (!$scope->hasExpressionType($expr)->yes()) {
if ($expr instanceof Node\Expr\PropertyFetch) {
return $this->checkUndefined($expr->var, $scope, $operatorDescription, $identifier);
}
if ($expr->class instanceof Expr) {
return $this->checkUndefined($expr->class, $scope, $operatorDescription, $identifier);
}
return null;
}
}
$propertyDescription = $this->propertyDescriptor->describeProperty($propertyReflection, $scope, $expr);
$propertyType = $propertyReflection->getWritableType();
if ($error !== null) {
return $error;
}
if (!$this->checkAdvancedIsset) {
if ($expr instanceof Node\Expr\PropertyFetch) {
return $this->checkUndefined($expr->var, $scope, $operatorDescription, $identifier);
}
if ($expr->class instanceof Expr) {
return $this->checkUndefined($expr->class, $scope, $operatorDescription, $identifier);
}
return null;
}
$error = $this->generateError(
$propertyReflection->getWritableType(),
sprintf('%s (%s) %s', $propertyDescription, $propertyType->describe(VerbosityLevel::typeOnly()), $operatorDescription),
$typeMessageCallback,
$identifier,
'property',
);
if ($error !== null) {
if ($expr instanceof Node\Expr\PropertyFetch) {
return $this->check($expr->var, $scope, $operatorDescription, $identifier, $typeMessageCallback, $error);
}
if ($expr->class instanceof Expr) {
return $this->check($expr->class, $scope, $operatorDescription, $identifier, $typeMessageCallback, $error);
}
}
return $error;
}
if ($error !== null) {
return $error;
}
if (!$this->checkAdvancedIsset) {
return null;
}
$error = $this->generateError(
$this->treatPhpDocTypesAsCertain ? $scope->getType($expr) : $scope->getNativeType($expr),
sprintf('Expression %s', $operatorDescription),
$typeMessageCallback,
$identifier,
'expr',
);
if ($error !== null) {
return $error;
}
if ($expr instanceof Expr\NullsafePropertyFetch) {
if ($expr->name instanceof Node\Identifier) {
return RuleErrorBuilder::message(sprintf('Using nullsafe property access "?->%s" %s is unnecessary. Use -> instead.', $expr->name->name, $operatorDescription))
->identifier('nullsafe.neverNull')
->build();
}
return RuleErrorBuilder::message(sprintf('Using nullsafe property access "?->(Expression)" %s is unnecessary. Use -> instead.', $operatorDescription))
->identifier('nullsafe.neverNull')
->build();
}
return null;
}
/**
* @param ErrorIdentifier $identifier
*/
private function checkUndefined(Expr $expr, Scope $scope, string $operatorDescription, string $identifier): ?IdentifierRuleError
{
if ($expr instanceof Node\Expr\Variable && is_string($expr->name)) {
$hasVariable = $scope->hasVariableType($expr->name);
if (!$hasVariable->no()) {
return null;
}
return RuleErrorBuilder::message(sprintf('Variable $%s %s is never defined.', $expr->name, $operatorDescription))
->identifier(sprintf('%s.variable', $identifier))
->build();
}
if ($expr instanceof Node\Expr\ArrayDimFetch && $expr->dim !== null) {
$type = $this->treatPhpDocTypesAsCertain ? $scope->getType($expr->var) : $scope->getNativeType($expr->var);
$dimType = $this->treatPhpDocTypesAsCertain ? $scope->getType($expr->dim) : $scope->getNativeType($expr->dim);
$hasOffsetValue = $type->hasOffsetValueType($dimType);
if (!$type->isOffsetAccessible()->yes()) {
return $this->checkUndefined($expr->var, $scope, $operatorDescription, $identifier);
}
if (!$hasOffsetValue->no()) {
return $this->checkUndefined($expr->var, $scope, $operatorDescription, $identifier);
}
return RuleErrorBuilder::message(
sprintf(
'Offset %s on %s %s does not exist.',
$dimType->describe(VerbosityLevel::value()),
$type->describe(VerbosityLevel::value()),
$operatorDescription,
),
)->identifier(sprintf('%s.offset', $identifier))->build();
}
if ($expr instanceof Expr\PropertyFetch) {
return $this->checkUndefined($expr->var, $scope, $operatorDescription, $identifier);
}
if ($expr instanceof Expr\StaticPropertyFetch && $expr->class instanceof Expr) {
return $this->checkUndefined($expr->class, $scope, $operatorDescription, $identifier);
}
return null;
}
/**
* @param callable(Type): ?string $typeMessageCallback
* @param ErrorIdentifier $identifier
* @param 'variable'|'offset'|'property'|'expr'|'propertyNeverNullOrUninitialized' $identifierSecondPart
*/
private function generateError(Type $type, string $message, callable $typeMessageCallback, string $identifier, string $identifierSecondPart): ?IdentifierRuleError
{
$typeMessage = $typeMessageCallback($type);
if ($typeMessage === null) {
return null;
}
return RuleErrorBuilder::message(
sprintf('%s %s.', $message, $typeMessage),
)->identifier(sprintf('%s.%s', $identifier, $identifierSecondPart))->build();
}
}