-
Notifications
You must be signed in to change notification settings - Fork 568
Expand file tree
/
Copy pathAccessPropertiesInAssignRuleTest.php
More file actions
260 lines (241 loc) · 6.56 KB
/
AccessPropertiesInAssignRuleTest.php
File metadata and controls
260 lines (241 loc) · 6.56 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
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
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Properties;
use PHPStan\Php\PhpVersion;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleLevelHelper;
use PHPStan\Testing\RuleTestCase;
use PHPUnit\Framework\Attributes\RequiresPhp;
use const PHP_VERSION_ID;
/**
* @extends RuleTestCase<AccessPropertiesInAssignRule>
*/
class AccessPropertiesInAssignRuleTest extends RuleTestCase
{
protected function getRule(): Rule
{
$reflectionProvider = self::createReflectionProvider();
return new AccessPropertiesInAssignRule(
new AccessPropertiesCheck(
$reflectionProvider,
new RuleLevelHelper(
$reflectionProvider,
checkNullables: true,
checkThisOnly: false,
checkUnionTypes: true,
checkExplicitMixed: false,
checkImplicitMixed: false,
checkBenevolentUnionTypes: false,
discoveringSymbolsTip: true,
),
new PhpVersion(PHP_VERSION_ID),
reportMagicProperties: true,
checkDynamicProperties: true,
checkNonStringableDynamicAccess: true,
),
);
}
public function testRule(): void
{
$tipText = 'Learn more: <fg=cyan>https://phpstan.org/blog/solving-phpstan-access-to-undefined-property</>';
$this->analyse([__DIR__ . '/data/access-properties-assign.php'], [
[
'Access to an undefined property TestAccessPropertiesAssign\AccessPropertyWithDimFetch::$foo.',
10,
$tipText,
],
[
'Access to an undefined property TestAccessPropertiesAssign\AccessPropertyWithDimFetch::$foo.',
15,
$tipText,
],
]);
}
public function testRuleAssignOp(): void
{
$tipText = 'Learn more: <fg=cyan>https://phpstan.org/blog/solving-phpstan-access-to-undefined-property</>';
$this->analyse([__DIR__ . '/data/access-properties-assign-op.php'], [
[
'Access to an undefined property TestAccessProperties\AssignOpNonexistentProperty::$flags.',
15,
$tipText,
],
]);
}
public function testRuleExpressionNames(): void
{
$tipText = 'Learn more: <fg=cyan>https://phpstan.org/blog/solving-phpstan-access-to-undefined-property</>';
$this->analyse([__DIR__ . '/data/properties-from-variable-into-object.php'], [
[
'Access to an undefined property PropertiesFromVariableIntoObject\Foo::$noop.',
26,
$tipText,
],
]);
}
public function testRuleExpressionNames2(): void
{
$tipText = 'Learn more: <fg=cyan>https://phpstan.org/blog/solving-phpstan-access-to-undefined-property</>';
$this->analyse([__DIR__ . '/data/properties-from-array-into-object.php'], [
[
'Access to an undefined property PropertiesFromArrayIntoObject\Foo::$noop.',
42,
$tipText,
],
[
'Access to an undefined property PropertiesFromArrayIntoObject\Foo::$noop.',
54,
$tipText,
],
[
'Access to an undefined property PropertiesFromArrayIntoObject\Foo::$noop.',
69,
$tipText,
],
[
'Access to an undefined property PropertiesFromArrayIntoObject\Foo::$noop.',
110,
$tipText,
],
]);
}
public function testBug4492(): void
{
$this->analyse([__DIR__ . '/data/bug-4492.php'], []);
}
public function testDynamicStringableAccess(): void
{
// All warnings are reported by the AccessPropertiesRule.
// The AccessPropertiesInAssignRule does not report any warnings.
$this->analyse([__DIR__ . '/data/dynamic-stringable-access.php'], []);
}
#[RequiresPhp('>= 8.0')]
public function testDynamicStringableNullsafeAccess(): void
{
// All warnings are reported by the AccessPropertiesRule.
// The AccessPropertiesInAssignRule does not report any warnings.
$this->analyse([__DIR__ . '/data/dynamic-stringable-nullsafe-access.php'], []);
}
public function testObjectShapes(): void
{
$tipText = 'Learn more: <fg=cyan>https://phpstan.org/blog/solving-phpstan-access-to-undefined-property</>';
$this->analyse([__DIR__ . '/data/properties-object-shapes.php'], [
[
'Access to an undefined property object{foo: int, bar?: string}::$bar.',
19,
$tipText,
],
[
'Access to an undefined property object{foo: int, bar?: string}::$baz.',
20,
$tipText,
],
]);
}
public function testConflictingAnnotationProperty(): void
{
$errors = [];
if (PHP_VERSION_ID >= 80200) {
$errors = [
[
'Access to private property ConflictingAnnotationProperty\PropertyWithAnnotation::$test.',
27,
],
];
}
$this->analyse([__DIR__ . '/data/conflicting-annotation-property.php'], $errors);
}
public function testBug10477(): void
{
$this->analyse([__DIR__ . '/../../Analyser/nsrt/bug-10477.php'], []);
}
#[RequiresPhp('>= 8.4')]
public function testAsymmetricVisibility(): void
{
$this->analyse([__DIR__ . '/data/write-asymmetric-visibility.php'], [
[
'Assign to private(set) property $this(WriteAsymmetricVisibility\Bar)::$a.',
26,
],
[
'Assign to private(set) property WriteAsymmetricVisibility\Foo::$a.',
34,
],
[
'Assign to protected(set) property WriteAsymmetricVisibility\Foo::$b.',
35,
],
[
'Access to private property $c of parent class WriteAsymmetricVisibility\ReadonlyProps.',
64,
],
[
'Assign to protected(set) property WriteAsymmetricVisibility\ReadonlyProps::$a.',
70,
],
[
'Access to protected property WriteAsymmetricVisibility\ReadonlyProps::$b.',
71,
],
[
'Access to private property WriteAsymmetricVisibility\ReadonlyProps::$c.',
72,
],
[
'Assign to private(set) property WriteAsymmetricVisibility\ArrayProp::$a.',
83,
],
]);
}
public function testBug13123(): void
{
$this->analyse([__DIR__ . '/data/bug-13123.php'], []);
}
#[RequiresPhp('>= 8.5')]
public function testCloneWith(): void
{
$this->analyse([__DIR__ . '/data/clone-with.php'], [
[
'Access to private property AccessPropertiesInAssignCloneWith\Foo::$priv.',
26,
],
[
'Access to protected property AccessPropertiesInAssignCloneWith\Foo::$prot.',
26,
],
[
'Access to private property AccessPropertiesInAssignCloneWith\FooReadonly::$priv.',
56,
],
[
'Access to protected property AccessPropertiesInAssignCloneWith\FooReadonly::$prot.',
56,
],
[
'Assign to protected(set) property AccessPropertiesInAssignCloneWith\FooReadonly::$pub.',
56,
],
]);
}
#[RequiresPhp('>= 8.5')]
public function testBug14063(): void
{
$this->analyse([__DIR__ . '/data/bug-14063.php'], [
[
'Assign to protected(set) property Bug14063\Qux::$value.',
65,
],
[
'Assign to protected(set) property Bug14063\Bar::$value.',
68,
],
[
'Access to protected property Bug14063\Baz::$prot.',
71,
],
[
'Access to private property Bug14063\Baz::$priv.',
71,
],
]);
}
}