forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMissingReadOnlyPropertyAssignRuleTest.php
More file actions
352 lines (316 loc) · 9.37 KB
/
MissingReadOnlyPropertyAssignRuleTest.php
File metadata and controls
352 lines (316 loc) · 9.37 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
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Properties;
use PHPStan\Reflection\ConstructorsHelper;
use PHPStan\Reflection\PropertyReflection;
use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
use PHPUnit\Framework\Attributes\RequiresPhp;
use function in_array;
use function strpos;
/**
* @extends RuleTestCase<MissingReadOnlyPropertyAssignRule>
*/
class MissingReadOnlyPropertyAssignRuleTest extends RuleTestCase
{
protected function getRule(): Rule
{
return new MissingReadOnlyPropertyAssignRule(
new ConstructorsHelper(
self::getContainer(),
[
'MissingReadOnlyPropertyAssign\\TestCase::setUp',
'Bug10523\\Controller::init',
'Bug10523\\MultipleWrites::init',
'Bug10523\\SingleWriteInConstructorCalledMethod::init',
'Bug12253\\PayloadWithAdditionalConstructor::setUp',
],
),
);
}
protected function getReadWritePropertiesExtensions(): array
{
return [
new class() implements ReadWritePropertiesExtension {
public function isAlwaysRead(PropertyReflection $property, string $propertyName): bool
{
return $this->isEntityId($property, $propertyName);
}
public function isAlwaysWritten(PropertyReflection $property, string $propertyName): bool
{
return $this->isEntityId($property, $propertyName);
}
public function isInitialized(PropertyReflection $property, string $propertyName): bool
{
return $this->isEntityId($property, $propertyName);
}
private function isEntityId(PropertyReflection $property, string $propertyName): bool
{
return $property->getDeclaringClass()->getName() === 'MissingReadOnlyPropertyAssign\\Entity'
&& in_array($propertyName, ['id'], true);
}
},
new class() implements ReadWritePropertiesExtension {
public function isAlwaysRead(PropertyReflection $property, string $propertyName): bool
{
return false;
}
public function isAlwaysWritten(PropertyReflection $property, string $propertyName): bool
{
return $this->isInitialized($property, $propertyName);
}
public function isInitialized(PropertyReflection $property, string $propertyName): bool
{
return $property->isPublic() &&
strpos($property->getDocComment() ?? '', '@init') !== false;
}
},
];
}
#[RequiresPhp('>= 8.1')]
public function testRule(): void
{
$this->analyse([__DIR__ . '/data/missing-readonly-property-assign.php'], [
[
'Class MissingReadOnlyPropertyAssign\Foo has an uninitialized readonly property $unassigned. Assign it in the constructor.',
14,
],
[
'Class MissingReadOnlyPropertyAssign\Foo has an uninitialized readonly property $unassigned2. Assign it in the constructor.',
16,
],
[
'Access to an uninitialized readonly property MissingReadOnlyPropertyAssign\Foo::$readBeforeAssigned.',
33,
],
[
'Readonly property MissingReadOnlyPropertyAssign\Foo::$doubleAssigned is already assigned.',
37,
],
[
'Class MissingReadOnlyPropertyAssign\BarDoubleAssignInSetter has an uninitialized readonly property $foo. Assign it in the constructor.',
53,
],
[
'Class MissingReadOnlyPropertyAssign\AssignOp has an uninitialized readonly property $foo. Assign it in the constructor.',
79,
],
[
'Access to an uninitialized readonly property MissingReadOnlyPropertyAssign\AssignOp::$foo.',
85,
],
[
'Access to an uninitialized readonly property MissingReadOnlyPropertyAssign\AssignOp::$bar.',
87,
],
[
'Class MissingReadOnlyPropertyAssign\FooTraitClass has an uninitialized readonly property $unassigned. Assign it in the constructor.',
114,
],
[
'Class MissingReadOnlyPropertyAssign\FooTraitClass has an uninitialized readonly property $unassigned2. Assign it in the constructor.',
116,
],
[
'Access to an uninitialized readonly property MissingReadOnlyPropertyAssign\FooTraitClass::$readBeforeAssigned.',
145,
],
[
'Readonly property MissingReadOnlyPropertyAssign\FooTraitClass::$doubleAssigned is already assigned.',
149,
],
[
'Readonly property MissingReadOnlyPropertyAssign\AdditionalAssignOfReadonlyPromotedProperty::$x is already assigned.',
188,
],
[
'Access to an uninitialized readonly property MissingReadOnlyPropertyAssign\MethodCalledFromConstructorBeforeAssign::$foo.',
226,
],
[
'Access to an uninitialized readonly property MissingReadOnlyPropertyAssign\MethodCalledTwice::$foo.',
244,
],
[
'Class MissingReadOnlyPropertyAssign\PropertyAssignedOnDifferentObjectUninitialized has an uninitialized readonly property $foo. Assign it in the constructor.',
264,
],
]);
}
#[RequiresPhp('>= 8.1')]
public function testBug7119(): void
{
$this->analyse([__DIR__ . '/data/bug-7119.php'], []);
}
#[RequiresPhp('>= 8.1')]
public function testBug7314(): void
{
$this->analyse([__DIR__ . '/data/bug-7314.php'], []);
}
#[RequiresPhp('>= 8.1')]
public function testBug8412(): void
{
$this->analyse([__DIR__ . '/data/bug-8412.php'], []);
}
#[RequiresPhp('>= 8.1')]
public function testBug8958(): void
{
$this->analyse([__DIR__ . '/data/bug-8958.php'], []);
}
#[RequiresPhp('>= 8.1')]
public function testBug8563(): void
{
$this->analyse([__DIR__ . '/data/bug-8563.php'], []);
}
#[RequiresPhp('>= 8.1')]
public function testBug6402(): void
{
$this->analyse([__DIR__ . '/data/bug-6402.php'], [
[
'Access to an uninitialized readonly property Bug6402\SomeModel2::$views.',
28,
],
]);
}
#[RequiresPhp('>= 8.1')]
public function testBug7198(): void
{
$this->analyse([__DIR__ . '/data/bug-7198.php'], []);
}
#[RequiresPhp('>= 8.1')]
public function testBug7649(): void
{
$this->analyse([__DIR__ . '/data/bug-7649.php'], [
[
'Class Bug7649\Foo has an uninitialized readonly property $bar. Assign it in the constructor.',
7,
],
]);
}
#[RequiresPhp('>= 8.1')]
public function testBug9577(): void
{
$this->analyse([__DIR__ . '/../Classes/data/bug-9577.php'], [
[
'Class Bug9577\SpecializedException2 has an uninitialized readonly property $message. Assign it in the constructor.',
8,
],
]);
}
#[RequiresPhp('>= 8.3')]
public function testAnonymousReadonlyClass(): void
{
$this->analyse([__DIR__ . '/data/missing-readonly-anonymous-class-property-assign.php'], [
[
'Class class@anonymous/tests/PHPStan/Rules/Properties/data/missing-readonly-anonymous-class-property-assign.php:10 has an uninitialized readonly property $foo. Assign it in the constructor.',
11,
],
]);
}
#[RequiresPhp('>= 8.1')]
public function testBug10523(): void
{
$this->analyse([__DIR__ . '/data/bug-10523.php'], [
[
'Readonly property Bug10523\MultipleWrites::$userAccount is already assigned.',
55,
],
]);
}
#[RequiresPhp('>= 8.1')]
public function testBug10822(): void
{
$this->analyse([__DIR__ . '/data/bug-10822.php'], []);
}
#[RequiresPhp('>= 8.1')]
public function testRedeclaredReadonlyProperties(): void
{
$this->analyse([__DIR__ . '/data/redeclare-readonly-property.php'], [
[
'Readonly property RedeclareReadonlyProperty\B1::$myProp is already assigned.',
16,
],
[
'Readonly property RedeclareReadonlyProperty\B5::$myProp is already assigned.',
50,
],
[
'Readonly property RedeclareReadonlyProperty\B7::$myProp is already assigned.',
70,
],
[
'Readonly property RedeclareReadonlyProperty\A@anonymous/tests/PHPStan/Rules/Properties/data/redeclare-readonly-property.php:117::$myProp is already assigned.',
121,
],
[
'Class RedeclareReadonlyProperty\B16 has an uninitialized readonly property $myProp. Assign it in the constructor.',
195,
],
[
'Class RedeclareReadonlyProperty\C17 has an uninitialized readonly property $aProp. Assign it in the constructor.',
218,
],
[
'Class RedeclareReadonlyProperty\B18 has an uninitialized readonly property $aProp. Assign it in the constructor.',
233,
],
]);
}
#[RequiresPhp('>= 8.2')]
public function testRedeclaredPropertiesOfReadonlyClass(): void
{
$this->analyse([__DIR__ . '/data/redeclare-property-of-readonly-class.php'], [
[
'Readonly property RedeclarePropertyOfReadonlyClass\B1::$promotedProp is already assigned.',
15,
],
]);
}
#[RequiresPhp('>= 8.1')]
public function testBug8101(): void
{
$this->analyse([__DIR__ . '/data/bug-8101.php'], [
[
'Readonly property Bug8101\B::$myProp is already assigned.',
12,
],
]);
}
#[RequiresPhp('>= 8.1')]
public function testBug9863(): void
{
$this->analyse([__DIR__ . '/data/bug-9863.php'], [
[
'Readonly property Bug9863\ReadonlyChildWithoutIsset::$foo is already assigned.',
17,
],
[
'Class Bug9863\ReadonlyParentWithIsset has an uninitialized readonly property $foo. Assign it in the constructor.',
23,
],
[
'Access to an uninitialized readonly property Bug9863\ReadonlyParentWithIsset::$foo.',
28,
],
]);
}
#[RequiresPhp('>= 8.1')]
public function testBug9864(): void
{
$this->analyse([__DIR__ . '/data/bug-9864.php'], []);
}
#[RequiresPhp('>= 8.1')]
public function testBug10048(): void
{
$this->analyse([__DIR__ . '/data/bug-10048.php'], []);
}
#[RequiresPhp('>= 8.1')]
public function testBug11828(): void
{
$this->analyse([__DIR__ . '/data/bug-11828.php'], []);
}
#[RequiresPhp('>= 8.4')]
public function testBug12253(): void
{
$this->analyse([__DIR__ . '/data/bug-12253.php'], []);
}
}