/
UnusedPrivatePropertyRuleTest.php
255 lines (230 loc) · 6.37 KB
/
UnusedPrivatePropertyRuleTest.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
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
<?php declare(strict_types = 1);
namespace PHPStan\Rules\DeadCode;
use PHPStan\Reflection\PropertyReflection;
use PHPStan\Rules\Properties\DirectReadWritePropertiesExtensionProvider;
use PHPStan\Rules\Properties\ReadWritePropertiesExtension;
use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
use function in_array;
use const PHP_VERSION_ID;
/**
* @extends RuleTestCase<UnusedPrivatePropertyRule>
*/
class UnusedPrivatePropertyRuleTest extends RuleTestCase
{
/** @var string[] */
private array $alwaysWrittenTags;
/** @var string[] */
private array $alwaysReadTags;
protected function getRule(): Rule
{
return new UnusedPrivatePropertyRule(
new DirectReadWritePropertiesExtensionProvider([
new class() implements ReadWritePropertiesExtension {
public function isAlwaysRead(PropertyReflection $property, string $propertyName): bool
{
return $property->getDeclaringClass()->getName() === 'UnusedPrivateProperty\\TestExtension'
&& in_array($propertyName, [
'read',
'used',
], true);
}
public function isAlwaysWritten(PropertyReflection $property, string $propertyName): bool
{
return $property->getDeclaringClass()->getName() === 'UnusedPrivateProperty\\TestExtension'
&& in_array($propertyName, [
'written',
'used',
], true);
}
public function isInitialized(PropertyReflection $property, string $propertyName): bool
{
return false;
}
},
]),
$this->alwaysWrittenTags,
$this->alwaysReadTags,
true,
);
}
public function testRule(): void
{
if (PHP_VERSION_ID < 70400 && !self::$useStaticReflectionProvider) {
$this->markTestSkipped('Test requires PHP 7.4 or static reflection.');
}
$this->alwaysWrittenTags = [];
$this->alwaysReadTags = [];
$tip = 'See: https://phpstan.org/developing-extensions/always-read-written-properties';
$this->analyse([__DIR__ . '/data/unused-private-property.php'], [
[
'Property UnusedPrivateProperty\Foo::$bar is never read, only written.',
10,
$tip,
],
[
'Property UnusedPrivateProperty\Foo::$baz is unused.',
12,
$tip,
],
[
'Property UnusedPrivateProperty\Foo::$lorem is never written, only read.',
14,
$tip,
],
[
'Property UnusedPrivateProperty\Bar::$baz is never written, only read.',
57,
$tip,
],
[
'Static property UnusedPrivateProperty\Baz::$bar is never read, only written.',
86,
$tip,
],
[
'Static property UnusedPrivateProperty\Baz::$baz is unused.',
88,
$tip,
],
[
'Static property UnusedPrivateProperty\Baz::$lorem is never written, only read.',
90,
$tip,
],
[
'Property UnusedPrivateProperty\Lorem::$baz is never read, only written.',
117,
$tip,
],
[
'Property class@anonymous/tests/PHPStan/Rules/DeadCode/data/unused-private-property.php:152::$bar is unused.',
153,
$tip,
],
[
'Property UnusedPrivateProperty\DolorWithAnonymous::$foo is unused.',
148,
$tip,
],
[
'Property UnusedPrivateProperty\ArrayAssign::$foo is never read, only written.',
162,
$tip,
],
[
'Property UnusedPrivateProperty\ListAssign::$foo is never read, only written.',
191,
$tip,
],
]);
$this->analyse([__DIR__ . '/data/TestExtension.php'], [
[
'Property UnusedPrivateProperty\TestExtension::$unused is unused.',
8,
$tip,
],
[
'Property UnusedPrivateProperty\TestExtension::$read is never written, only read.',
10,
$tip,
],
[
'Property UnusedPrivateProperty\TestExtension::$written is never read, only written.',
12,
$tip,
],
]);
}
public function testAlwaysUsedTags(): void
{
$this->alwaysWrittenTags = ['@ORM\Column'];
$this->alwaysReadTags = ['@get'];
$tip = 'See: https://phpstan.org/developing-extensions/always-read-written-properties';
$this->analyse([__DIR__ . '/data/private-property-with-tags.php'], [
[
'Property PrivatePropertyWithTags\Foo::$title is never read, only written.',
13,
$tip,
],
[
'Property PrivatePropertyWithTags\Foo::$text is never written, only read.',
18,
$tip,
],
]);
}
public function testTrait(): void
{
$this->alwaysWrittenTags = [];
$this->alwaysReadTags = [];
$this->analyse([__DIR__ . '/data/private-property-trait.php'], []);
}
public function testBug3636(): void
{
if (!self::$useStaticReflectionProvider && PHP_VERSION_ID < 70400) {
$this->markTestSkipped('Test requires PHP 7.4.');
}
$this->alwaysWrittenTags = [];
$this->alwaysReadTags = [];
$tip = 'See: https://phpstan.org/developing-extensions/always-read-written-properties';
$this->analyse([__DIR__ . '/data/bug-3636.php'], [
[
'Property Bug3636\Bar::$date is never written, only read.',
22,
$tip,
],
]);
}
public function testPromotedProperties(): void
{
if (!self::$useStaticReflectionProvider && PHP_VERSION_ID < 80000) {
$this->markTestSkipped('Test requires PHP 8.0.');
}
$this->alwaysWrittenTags = [];
$this->alwaysReadTags = ['@get'];
$tip = 'See: https://phpstan.org/developing-extensions/always-read-written-properties';
$this->analyse([__DIR__ . '/data/unused-private-promoted-property.php'], [
[
'Property UnusedPrivatePromotedProperty\Foo::$lorem is never read, only written.',
12,
$tip,
],
]);
}
public function testNullsafe(): void
{
if (PHP_VERSION_ID < 80000 && !self::$useStaticReflectionProvider) {
$this->markTestSkipped('Test requires PHP 8.0.');
}
$this->alwaysWrittenTags = [];
$this->alwaysReadTags = [];
$this->analyse([__DIR__ . '/data/nullsafe-unused-private-property.php'], []);
}
public function testBug5935(): void
{
$this->alwaysWrittenTags = [];
$this->alwaysReadTags = [];
$this->analyse([__DIR__ . '/data/bug-5935.php'], []);
}
public function testBug5337(): void
{
if (PHP_VERSION_ID < 70400 && !self::$useStaticReflectionProvider) {
$this->markTestSkipped('Test requires PHP 7.4.');
}
$this->alwaysWrittenTags = [];
$this->alwaysReadTags = [];
$this->analyse([__DIR__ . '/data/bug-5337.php'], [
[
'Property Bug5337\Clazz::$prefix is never read, only written.',
7,
'See: https://phpstan.org/developing-extensions/always-read-written-properties',
],
[
'Property Bug5337\Foo::$field is unused.',
20,
'See: https://phpstan.org/developing-extensions/always-read-written-properties',
],
]);
}
}