-
Notifications
You must be signed in to change notification settings - Fork 565
Expand file tree
/
Copy pathUninitializedPropertyRuleTest.php
More file actions
250 lines (221 loc) · 6.68 KB
/
UninitializedPropertyRuleTest.php
File metadata and controls
250 lines (221 loc) · 6.68 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
<?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 array_merge;
use function strpos;
/**
* @extends RuleTestCase<UninitializedPropertyRule>
*/
class UninitializedPropertyRuleTest extends RuleTestCase
{
protected function getRule(): Rule
{
return new UninitializedPropertyRule(
new ConstructorsHelper(
self::getContainer(),
[
'UninitializedProperty\\TestCase::setUp',
'Bug9619\\AdminPresenter::startup',
'Bug9619\\AdminPresenter2::startup',
'Bug9619\\AdminPresenter3::startup',
'Bug9619\\AdminPresenter3::startup2',
],
),
);
}
protected function getReadWritePropertiesExtensions(): array
{
return [
new class() implements ReadWritePropertiesExtension {
public function isAlwaysRead(PropertyReflection $property, string $propertyName): bool
{
return false;
}
public function isAlwaysWritten(PropertyReflection $property, string $propertyName): bool
{
return false;
}
public function isInitialized(PropertyReflection $property, string $propertyName): bool
{
return $property->getDeclaringClass()->getName() === 'UninitializedProperty\\TestExtension' && $propertyName === 'inited';
}
},
// bug-9619
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() ?? '', '@inject') !== false;
}
},
];
}
public static function getAdditionalConfigFiles(): array
{
return array_merge(
parent::getAdditionalConfigFiles(),
[
__DIR__ . '/uninitialized-property-rule.neon',
],
);
}
public function testRule(): void
{
$this->analyse([__DIR__ . '/data/uninitialized-property.php'], [
[
'Class UninitializedProperty\Foo has an uninitialized property $bar. Give it default value or assign it in the constructor.',
10,
],
[
'Class UninitializedProperty\Foo has an uninitialized property $baz. Give it default value or assign it in the constructor.',
12,
],
[
'Access to an uninitialized property UninitializedProperty\Bar::$foo.',
33,
],
[
'Class UninitializedProperty\Lorem has an uninitialized property $baz. Give it default value or assign it in the constructor.',
59,
],
[
'Class UninitializedProperty\TestExtension has an uninitialized property $uninited. Give it default value or assign it in the constructor.',
122,
],
[
'Class UninitializedProperty\FooTraitClass has an uninitialized property $bar. Give it default value or assign it in the constructor.',
157,
],
[
'Class UninitializedProperty\FooTraitClass has an uninitialized property $baz. Give it default value or assign it in the constructor.',
159,
],
/*[
'Access to an uninitialized property UninitializedProperty\InitializedInPublicSetterNonFinalClass::$foo.',
278,
],*/
[
'Class UninitializedProperty\SometimesInitializedInPrivateSetter has an uninitialized property $foo. Give it default value or assign it in the constructor.',
286,
],
[
'Access to an uninitialized property UninitializedProperty\SometimesInitializedInPrivateSetter::$foo.',
303,
],
[
'Class UninitializedProperty\EarlyReturn has an uninitialized property $foo. Give it default value or assign it in the constructor.',
372,
],
]);
}
public function testPromotedProperties(): void
{
$this->analyse([__DIR__ . '/data/uninitialized-property-promoted.php'], []);
}
public function testReadOnly(): void
{
// reported by a different rule
$this->analyse([__DIR__ . '/data/uninitialized-property-readonly.php'], []);
}
public function testReadOnlyPhpDoc(): void
{
// reported by a different rule
$this->analyse([__DIR__ . '/data/uninitialized-property-readonly-phpdoc.php'], []);
}
public function testBug7219(): void
{
$this->analyse([__DIR__ . '/data/bug-7219.php'], [
[
'Class Bug7219\Foo has an uninitialized property $email. Give it default value or assign it in the constructor.',
15,
],
[
'Class Bug7219\Foo has an uninitialized property $id. Give it default value or assign it in the constructor.',
8,
],
]);
}
public function testAdditionalConstructorsExtension(): void
{
$this->analyse([__DIR__ . '/data/uninitialized-property-additional-constructors.php'], [
[
'Class TestInitializedProperty\TestAdditionalConstructor has an uninitialized property $three. Give it default value or assign it in the constructor.',
11,
],
[
'Class TestInitializedProperty\TestAdditionalConstructor has an uninitialized property $one. Give it default value or assign it in the constructor.',
07,
],
]);
}
public function testEfabricaLatteBug(): void
{
$this->analyse([__DIR__ . '/data/efabrica-latte-bug.php'], []);
}
public function testBug9619(): void
{
$this->analyse([__DIR__ . '/data/bug-9619.php'], [
[
'Access to an uninitialized property Bug9619\AdminPresenter3::$user.',
55,
],
]);
}
public function testBug9831(): void
{
$this->analyse([__DIR__ . '/data/bug-9831.php'], [
[
'Access to an uninitialized property Bug9831\Foo::$bar.',
12,
],
]);
}
public function testRedeclareReadonlyProperties(): void
{
$this->analyse([__DIR__ . '/data/redeclare-readonly-property.php'], [
[
'Class RedeclareReadonlyProperty\B19 has an uninitialized property $prop2. Give it default value or assign it in the constructor.',
249,
],
[
'Access to an uninitialized property RedeclareReadonlyProperty\B19::$prop2.',
260,
],
]);
}
#[RequiresPhp('>= 8.4')]
public function testBug12336(): void
{
$this->analyse([__DIR__ . '/data/bug-12336.php'], []);
}
#[RequiresPhp('>= 8.4')]
public function testBug12547(): void
{
$this->analyse([__DIR__ . '/data/bug-12547.php'], []);
}
public function testBug13380(): void
{
$this->analyse([__DIR__ . '/data/bug-13380.php'], [
[
'Class Bug13380\Baz has an uninitialized property $prop. Give it default value or assign it in the constructor.',
33,
],
[
'Class Bug13380\Baz3 has an uninitialized property $prop. Give it default value or assign it in the constructor.',
57,
],
]);
}
}