/
CoalesceRule.php
196 lines (153 loc) · 5.34 KB
/
CoalesceRule.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
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Variables;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Properties\PropertyDescriptor;
use PHPStan\Rules\Properties\PropertyReflectionFinder;
use PHPStan\Rules\RuleError;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\NullType;
use PHPStan\Type\Type;
use PHPStan\Type\VerbosityLevel;
/**
* @implements \PHPStan\Rules\Rule<\PhpParser\Node\Expr>
*/
class CoalesceRule implements \PHPStan\Rules\Rule
{
/** @var \PHPStan\Rules\Properties\PropertyDescriptor */
private $propertyDescriptor;
/** @var \PHPStan\Rules\Properties\PropertyReflectionFinder */
private $propertyReflectionFinder;
public function __construct(
PropertyDescriptor $propertyDescriptor,
PropertyReflectionFinder $propertyReflectionFinder
)
{
$this->propertyDescriptor = $propertyDescriptor;
$this->propertyReflectionFinder = $propertyReflectionFinder;
}
public function getNodeType(): string
{
return \PhpParser\Node\Expr::class;
}
public function processNode(Node $node, Scope $scope): array
{
if ($node instanceof Node\Expr\BinaryOp\Coalesce) {
$error = $this->canBeCoalesced($node->left, $scope, 'Coalesce');
} elseif ($node instanceof Node\Expr\AssignOp\Coalesce) {
$error = $this->canBeCoalesced($node->var, $scope, 'Null-coalescing assignment');
} else {
return [];
}
if ($error === null) {
return [];
}
return [$error];
}
private function canBeCoalesced(Node $node, Scope $scope, string $action, ?RuleError $error = null): ?RuleError
{
if ($node instanceof \PhpParser\Node\Expr\FuncCall ||
$node instanceof \PhpParser\Node\Expr\MethodCall ||
$node instanceof \PhpParser\Node\Expr\StaticCall) {
if ($node->name instanceof Expr) {
return null;
}
$resultingType = $scope->getType($node);
$error = $this->generateError(
$resultingType,
$node,
sprintf('%s of return value for call to \'%s\'', $action, $node->name->toString())
);
} elseif ($node instanceof Node\Expr\Variable && is_string($node->name)) {
$hasVariable = $scope->hasVariableType($node->name);
if ($hasVariable->no()) {
return $error ?? RuleErrorBuilder::message(
sprintf('%s of undefined variable $%s.', $action, $node->name)
)->line($node->getLine())->build();
}
$variableType = $scope->getVariableType($node->name);
if ($hasVariable->maybe()) {
return null;
}
if ($hasVariable->yes()) {
return $error ?? $this->generateError(
$variableType,
$node,
sprintf('%s of variable $%s', $action, $node->name)
);
}
} elseif ($node instanceof Node\Expr\ArrayDimFetch && $node->dim !== null) {
$type = $scope->getType($node->var);
$dimType = $scope->getType($node->dim);
$hasOffsetValue = $type->hasOffsetValueType($dimType);
if ($hasOffsetValue->no() || $type->isOffsetAccessible()->no()) {
return $error ?? RuleErrorBuilder::message(
sprintf(
'%s of invalid offset %s on %s.',
$action,
$dimType->describe(VerbosityLevel::value()),
$type->describe(VerbosityLevel::value())
)
)->line($node->getLine())->build();
}
if ($hasOffsetValue->maybe()) {
return null;
}
// If offset is 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()) {
$error = $error ?? $this->generateError($type->getOffsetValueType($dimType), $node, sprintf(
'%s of offset %s on %s',
$action,
$dimType->describe(VerbosityLevel::value()),
$type->describe(VerbosityLevel::value())
));
if ($error !== null) {
return $this->canBeCoalesced($node->var, $scope, $action, $error);
}
}
// Has offset, it is nullable
return null;
} elseif ($node instanceof Node\Expr\PropertyFetch || $node instanceof Node\Expr\StaticPropertyFetch) {
$propertyReflection = $this->propertyReflectionFinder->findPropertyReflectionFromNode($node, $scope);
if ($propertyReflection === null) {
return null;
}
$propertyDescription = $this->propertyDescriptor->describeProperty($propertyReflection, $node);
$propertyType = $propertyReflection->getWritableType();
$error = $error ?? $this->generateError(
$propertyReflection->getWritableType(),
$node,
sprintf('%s of %s (%s)', $action, $propertyDescription, $propertyType->describe(VerbosityLevel::typeOnly()))
);
if ($error !== null && property_exists($node, 'var')) {
return $this->canBeCoalesced($node->var, $scope, $action, $error);
}
}
return $error;
}
private function generateError(Type $type, Node $node, string $message): ?RuleError
{
$nullType = new NullType();
if ($type->equals($nullType)) {
return $this->generateAlwaysNullError($node, $message);
}
if ($type->isSuperTypeOf($nullType)->no()) {
return $this->generateNeverNullError($node, $message);
}
return null;
}
private function generateAlwaysNullError(Node $node, string $message): RuleError
{
return RuleErrorBuilder::message(
sprintf('%s, which is always null.', $message)
)->line($node->getLine())->build();
}
private function generateNeverNullError(Node $node, string $message): RuleError
{
return RuleErrorBuilder::message(
sprintf('%s, which cannot be null.', $message)
)->line($node->getLine())->build();
}
}