Repository navigation
Expand file tree
/
Copy pathUnsetRule.php
More file actions
184 lines (163 loc) · 5.13 KB
/
Copy pathUnsetRule.php
File metadata and controls
184 lines (163 loc) · 5.13 KB
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
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Variables;
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\AutowiredParameter;
use PHPStan\DependencyInjection\RegisteredRule;
use PHPStan\Php\PhpVersion;
use PHPStan\Rules\IdentifierRuleError;
use PHPStan\Rules\Properties\PropertyReflectionFinder;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\Type;
use PHPStan\Type\TypeUtils;
use PHPStan\Type\VerbosityLevel;
use function is_string;
use function sprintf;
/**
* @implements Rule<Node\Stmt\Unset_>
*/
#[RegisteredRule(level: 0)]
final class UnsetRule implements Rule
{
public function __construct(
private PropertyReflectionFinder $propertyReflectionFinder,
private PhpVersion $phpVersion,
#[AutowiredParameter]
private bool $reportMaybes,
#[AutowiredParameter(ref: '%featureToggles.unsetOffsetOnMaybeAccessible%')]
private bool $unsetOffsetOnMaybeAccessible,
)
{
}
public function getNodeType(): string
{
return Node\Stmt\Unset_::class;
}
public function processNode(Node $node, Scope $scope): array
{
$functionArguments = $node->vars;
$errors = [];
foreach ($functionArguments as $argument) {
if (
$argument instanceof Node\Expr\PropertyFetch
&& $argument->name instanceof Node\Identifier
) {
$foundPropertyReflection = $this->propertyReflectionFinder->findPropertyReflectionFromNode($argument, $scope);
if ($foundPropertyReflection === null) {
continue;
}
$propertyReflection = $foundPropertyReflection->getNativeReflection();
if ($propertyReflection === null) {
continue;
}
if ($propertyReflection->isReadOnly() || $propertyReflection->isReadOnlyByPhpDoc()) {
$errors[] = RuleErrorBuilder::message(
sprintf(
'Cannot unset %s %s::$%s property.',
$propertyReflection->isReadOnly() ? 'readonly' : '@readonly',
$propertyReflection->getDeclaringClass()->getDisplayName(),
$foundPropertyReflection->getName(),
),
)
->line($argument->getStartLine())
->identifier($propertyReflection->isReadOnly() ? 'unset.readOnlyProperty' : 'unset.readOnlyPropertyByPhpDoc')
->build();
continue;
}
if ($propertyReflection->isHooked()) {
$errors[] = RuleErrorBuilder::message(
sprintf(
'Cannot unset hooked %s::$%s property.',
$propertyReflection->getDeclaringClass()->getDisplayName(),
$foundPropertyReflection->getName(),
),
)
->line($argument->getStartLine())
->identifier('unset.hookedProperty')
->build();
continue;
} elseif ($this->phpVersion->supportsPropertyHooks()) {
if (
!$propertyReflection->isPrivate()
&& !$propertyReflection->isFinal()->yes()
&& !$propertyReflection->getDeclaringClass()->isFinal()
) {
$errors[] = RuleErrorBuilder::message(
sprintf(
'Cannot unset property %s::$%s because it might have hooks in a subclass.',
$propertyReflection->getDeclaringClass()->getDisplayName(),
$foundPropertyReflection->getName(),
),
)
->line($argument->getStartLine())
->identifier('unset.possiblyHookedProperty')
->build();
continue;
}
}
}
$error = $this->canBeUnset($argument, $scope);
if ($error === null) {
continue;
}
$errors[] = $error;
}
return $errors;
}
private function canBeUnset(Node $node, Scope $scope): ?IdentifierRuleError
{
if ($node instanceof Node\Expr\Variable && is_string($node->name)) {
$hasVariable = $scope->hasVariableType($node->name);
if ($hasVariable->no()) {
return RuleErrorBuilder::message(
sprintf('Call to function unset() contains undefined variable $%s.', $node->name),
)
->line($node->getStartLine())
->identifier('unset.variable')
->build();
}
} elseif ($node instanceof Node\Expr\ArrayDimFetch && $node->dim !== null) {
$type = $scope->getType($node->var);
$dimType = $scope->getType($node->dim);
if (
$type->isOffsetAccessible()->no()
|| $type->hasOffsetValueType($dimType)->no()
|| $this->isOffsetMaybeNotUnsettable($type)
) {
return RuleErrorBuilder::message(
sprintf(
'Cannot unset offset %s on %s.',
$dimType->describe(VerbosityLevel::value()),
$type->describe(VerbosityLevel::value()),
),
)
->line($node->getStartLine())
->identifier('unset.offset')
->build();
}
return $this->canBeUnset($node->var, $scope);
}
return null;
}
/**
* unset() of an offset deprecates on false and throws an Error on true, an int, a float or a string,
* so a union like array|false, array|int or array|string can fail at runtime.
* A type whose offset access is not even legal (an object without ArrayAccess) is reported by NonexistentOffsetInArrayDimFetchRule.
*/
private function isOffsetMaybeNotUnsettable(Type $type): bool
{
if (!$this->unsetOffsetOnMaybeAccessible || !$this->reportMaybes) {
return false;
}
if (!$type->isOffsetAccessLegal()->yes()) {
return false;
}
foreach (TypeUtils::flattenTypes($type) as $innerType) {
if ($innerType->isScalar()->yes()) {
return true;
}
}
return false;
}
}