-
Notifications
You must be signed in to change notification settings - Fork 426
/
EmptyRuleTest.php
180 lines (159 loc) · 4.33 KB
/
EmptyRuleTest.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
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Variables;
use PHPStan\Rules\IssetCheck;
use PHPStan\Rules\Properties\PropertyDescriptor;
use PHPStan\Rules\Properties\PropertyReflectionFinder;
use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
use const PHP_VERSION_ID;
/**
* @extends RuleTestCase<EmptyRule>
*/
class EmptyRuleTest extends RuleTestCase
{
private bool $treatPhpDocTypesAsCertain;
private bool $strictUnnecessaryNullsafePropertyFetch;
protected function getRule(): Rule
{
return new EmptyRule(new IssetCheck(
new PropertyDescriptor(),
new PropertyReflectionFinder(),
true,
$this->treatPhpDocTypesAsCertain,
$this->strictUnnecessaryNullsafePropertyFetch,
));
}
protected function shouldTreatPhpDocTypesAsCertain(): bool
{
return $this->treatPhpDocTypesAsCertain;
}
public function testRule(): void
{
$this->treatPhpDocTypesAsCertain = true;
$this->strictUnnecessaryNullsafePropertyFetch = false;
$this->analyse([__DIR__ . '/data/empty-rule.php'], [
[
'Offset \'nonexistent\' on array{0?: bool, 1?: false, 2: bool, 3: false, 4: true} in empty() does not exist.',
22,
],
[
'Offset 3 on array{0?: bool, 1?: false, 2: bool, 3: false, 4: true} in empty() always exists and is always falsy.',
24,
],
[
'Offset 4 on array{0?: bool, 1?: false, 2: bool, 3: false, 4: true} in empty() always exists and is not falsy.',
25,
],
[
'Offset 0 on array{\'\', \'0\', \'foo\', \'\'|\'foo\'} in empty() always exists and is always falsy.',
36,
],
[
'Offset 1 on array{\'\', \'0\', \'foo\', \'\'|\'foo\'} in empty() always exists and is always falsy.',
37,
],
[
'Offset 2 on array{\'\', \'0\', \'foo\', \'\'|\'foo\'} in empty() always exists and is not falsy.',
38,
],
[
'Variable $a in empty() is never defined.',
44,
],
[
'Variable $b in empty() always exists and is not falsy.',
47,
],
]);
}
public function testBug970(): void
{
$this->treatPhpDocTypesAsCertain = true;
$this->strictUnnecessaryNullsafePropertyFetch = false;
$this->analyse([__DIR__ . '/data/bug-970.php'], [
[
'Variable $ar in empty() is never defined.',
9,
],
]);
}
public function testBug6974(): void
{
$this->treatPhpDocTypesAsCertain = false;
$this->strictUnnecessaryNullsafePropertyFetch = false;
$this->analyse([__DIR__ . '/data/bug-6974.php'], [
[
'Variable $a in empty() always exists and is always falsy.',
12,
],
[
'Variable $a in empty() always exists and is not falsy.',
30,
],
]);
}
public function testBug7109(): void
{
if (PHP_VERSION_ID < 80000) {
$this->markTestSkipped('Test requires PHP 8.0.');
}
$this->treatPhpDocTypesAsCertain = true;
$this->strictUnnecessaryNullsafePropertyFetch = false;
$this->analyse([__DIR__ . '/../Properties/data/bug-7109.php'], [
[
'Expression in empty() is not falsy.',
59,
],
]);
}
public function testBug7109Strict(): void
{
if (PHP_VERSION_ID < 80000) {
$this->markTestSkipped('Test requires PHP 8.0.');
}
$this->treatPhpDocTypesAsCertain = true;
$this->strictUnnecessaryNullsafePropertyFetch = true;
$this->analyse([__DIR__ . '/../Properties/data/bug-7109.php'], [
[
'Using nullsafe property access "?->aaa" in empty() is unnecessary. Use -> instead.',
19,
],
[
'Using nullsafe property access "?->aaa" in empty() is unnecessary. Use -> instead.',
30,
],
[
'Using nullsafe property access "?->aaa" in empty() is unnecessary. Use -> instead.',
42,
],
[
'Using nullsafe property access "?->notFalsy" in empty() is unnecessary. Use -> instead.',
54,
],
[
'Expression in empty() is not falsy.',
59,
],
[
'Using nullsafe property access "?->aaa" in empty() is unnecessary. Use -> instead.',
68,
],
[
'Using nullsafe property access "?->(Expression)" in empty() is unnecessary. Use -> instead.',
75,
],
]);
}
public function testBug7318(): void
{
$this->treatPhpDocTypesAsCertain = true;
$this->strictUnnecessaryNullsafePropertyFetch = true;
$this->analyse([__DIR__ . '/../Properties/data/bug-7318.php'], []);
}
public function testBug7424(): void
{
$this->treatPhpDocTypesAsCertain = true;
$this->strictUnnecessaryNullsafePropertyFetch = false;
$this->analyse([__DIR__ . '/data/bug-7424.php'], []);
}
}