-
Notifications
You must be signed in to change notification settings - Fork 433
/
UninitializedPropertyRuleTest.php
187 lines (163 loc) · 5.14 KB
/
UninitializedPropertyRuleTest.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
<?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 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',
],
),
);
}
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 [
__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 $id. Give it default value or assign it in the constructor.',
8,
],
[
'Class Bug7219\Foo has an uninitialized property $email. Give it default value or assign it in the constructor.',
15,
],
]);
}
public function testAdditionalConstructorsExtension(): void
{
$this->analyse([__DIR__ . '/data/uninitialized-property-additional-constructors.php'], [
[
'Class TestInitializedProperty\TestAdditionalConstructor has an uninitialized property $one. Give it default value or assign it in the constructor.',
07,
],
[
'Class TestInitializedProperty\TestAdditionalConstructor has an uninitialized property $three. Give it default value or assign it in the constructor.',
11,
],
]);
}
public function testEfabricaLatteBug(): void
{
$this->analyse([__DIR__ . '/data/efabrica-latte-bug.php'], []);
}
public function testBug9619(): void
{
$this->analyse([__DIR__ . '/data/bug-9619.php'], []);
}
}