-
Notifications
You must be signed in to change notification settings - Fork 504
/
Copy pathAccessPropertiesCheck.php
211 lines (185 loc) · 6.22 KB
/
AccessPropertiesCheck.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
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Properties;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Identifier;
use PHPStan\Analyser\NullsafeOperatorHelper;
use PHPStan\Analyser\Scope;
use PHPStan\Internal\SprintfHelper;
use PHPStan\Php\PhpVersion;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\IdentifierRuleError;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Rules\RuleLevelHelper;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\ErrorType;
use PHPStan\Type\StaticType;
use PHPStan\Type\Type;
use PHPStan\Type\VerbosityLevel;
use function array_map;
use function array_merge;
use function count;
use function sprintf;
final class AccessPropertiesCheck
{
public function __construct(
private ReflectionProvider $reflectionProvider,
private RuleLevelHelper $ruleLevelHelper,
private PhpVersion $phpVersion,
private bool $reportMagicProperties,
private bool $checkDynamicProperties,
)
{
}
/**
* @return list<IdentifierRuleError>
*/
public function check(PropertyFetch $node, Scope $scope, bool $write): array
{
if ($node->name instanceof Identifier) {
$names = [$node->name->name];
} else {
$names = array_map(static fn (ConstantStringType $type): string => $type->getValue(), $scope->getType($node->name)->getConstantStrings());
}
$errors = [];
foreach ($names as $name) {
$errors = array_merge($errors, $this->processSingleProperty($scope, $node, $name, $write));
}
return $errors;
}
/**
* @return list<IdentifierRuleError>
*/
private function processSingleProperty(Scope $scope, PropertyFetch $node, string $name, bool $write): array
{
$typeResult = $this->ruleLevelHelper->findTypeToCheck(
$scope,
NullsafeOperatorHelper::getNullsafeShortcircuitedExprRespectingScope($scope, $node->var),
sprintf('Access to property $%s on an unknown class %%s.', SprintfHelper::escapeFormatString($name)),
static fn (Type $type): bool => $type->canAccessProperties()->yes() && $type->hasProperty($name)->yes(),
);
$type = $typeResult->getType();
if ($type instanceof ErrorType) {
return $typeResult->getUnknownClassErrors();
}
if ($scope->isInExpressionAssign($node)) {
return [];
}
$typeForDescribe = $type;
if ($type instanceof StaticType) {
$typeForDescribe = $type->getStaticObjectType();
}
if ($type->canAccessProperties()->no() || $type->canAccessProperties()->maybe() && !$scope->isUndefinedExpressionAllowed($node)) {
return [
RuleErrorBuilder::message(sprintf(
'Cannot access property $%s on %s.',
$name,
$typeForDescribe->describe(VerbosityLevel::typeOnly()),
))->identifier('property.nonObject')->build(),
];
}
$has = $type->hasProperty($name);
if (!$has->no() && $this->canAccessUndefinedProperties($scope, $node)) {
return [];
}
if (!$has->yes()) {
if ($scope->hasExpressionType($node)->yes()) {
return [];
}
$classNames = $type->getObjectClassNames();
if (!$this->reportMagicProperties) {
foreach ($classNames as $className) {
if (!$this->reflectionProvider->hasClass($className)) {
continue;
}
$classReflection = $this->reflectionProvider->getClass($className);
if (
$classReflection->hasNativeMethod('__get')
|| $classReflection->hasNativeMethod('__set')
) {
return [];
}
}
}
if (count($classNames) === 1) {
$propertyClassReflection = $this->reflectionProvider->getClass($classNames[0]);
$parentClassReflection = $propertyClassReflection->getParentClass();
while ($parentClassReflection !== null) {
if ($parentClassReflection->hasProperty($name)) {
if ($write) {
if ($scope->canWriteProperty($parentClassReflection->getProperty($name, $scope))) {
return [];
}
} elseif ($scope->canReadProperty($parentClassReflection->getProperty($name, $scope))) {
return [];
}
return [
RuleErrorBuilder::message(sprintf(
'Access to private property $%s of parent class %s.',
$name,
$parentClassReflection->getDisplayName(),
))->identifier('property.private')->build(),
];
}
$parentClassReflection = $parentClassReflection->getParentClass();
}
}
$ruleErrorBuilder = RuleErrorBuilder::message(sprintf(
'Access to an undefined property %s::$%s.',
$typeForDescribe->describe(VerbosityLevel::typeOnly()),
$name,
))->identifier('property.notFound');
if ($typeResult->getTip() !== null) {
$ruleErrorBuilder->tip($typeResult->getTip());
} else {
$ruleErrorBuilder->tip('Learn more: <fg=cyan>https://phpstan.org/blog/solving-phpstan-access-to-undefined-property</>');
}
return [
$ruleErrorBuilder->build(),
];
}
$propertyReflection = $type->getProperty($name, $scope);
if ($propertyReflection->isStatic()) {
return [
RuleErrorBuilder::message(sprintf(
'Non-static access to static property %s::$%s.',
$propertyReflection->getDeclaringClass()->getDisplayName(),
$name,
))->identifier('staticProperty.nonStaticAccess')->build(),
];
}
if ($write) {
if ($scope->canWriteProperty($propertyReflection)) {
return [];
}
} elseif ($scope->canReadProperty($propertyReflection)) {
return [];
}
if (
!$this->phpVersion->supportsAsymmetricVisibility()
|| !$write
|| (!$propertyReflection->isPrivateSet() && !$propertyReflection->isProtectedSet())
) {
return [
RuleErrorBuilder::message(sprintf(
'Access to %s property %s::$%s.',
$propertyReflection->isPrivate() ? 'private' : 'protected',
$type->describe(VerbosityLevel::typeOnly()),
$name,
))->identifier(sprintf('property.%s', $propertyReflection->isPrivate() ? 'private' : 'protected'))->build(),
];
}
return [
RuleErrorBuilder::message(sprintf(
'Assign to %s property %s::$%s.',
$propertyReflection->isPrivateSet() ? 'private(set)' : 'protected(set)',
$type->describe(VerbosityLevel::typeOnly()),
$name,
))->identifier(sprintf('assign.property%s', $propertyReflection->isPrivateSet() ? 'PrivateSet' : 'ProtectedSet'))->build(),
];
}
private function canAccessUndefinedProperties(Scope $scope, Expr $node): bool
{
return $scope->isUndefinedExpressionAllowed($node) && !$this->checkDynamicProperties;
}
}