forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInvalidIncDecOperationRuleTest.php
More file actions
257 lines (239 loc) · 5.29 KB
/
InvalidIncDecOperationRuleTest.php
File metadata and controls
257 lines (239 loc) · 5.29 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
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Operators;
use PHPStan\Php\PhpVersion;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleLevelHelper;
use PHPStan\Testing\RuleTestCase;
use PHPUnit\Framework\Attributes\RequiresPhp;
use const PHP_VERSION_ID;
/**
* @extends RuleTestCase<InvalidIncDecOperationRule>
*/
class InvalidIncDecOperationRuleTest extends RuleTestCase
{
private bool $checkExplicitMixed = false;
private bool $checkImplicitMixed = false;
protected function getRule(): Rule
{
return new InvalidIncDecOperationRule(
new RuleLevelHelper(
self::createReflectionProvider(),
checkNullables: true,
checkThisOnly: false,
checkUnionTypes: true,
checkExplicitMixed: $this->checkExplicitMixed,
checkImplicitMixed: $this->checkImplicitMixed,
checkBenevolentUnionTypes: false,
discoveringSymbolsTip: true,
),
new PhpVersion(PHP_VERSION_ID),
);
}
public function testRule(): void
{
$errors = [
[
'Cannot use ++ on a non-variable.',
11,
],
[
'Cannot use -- on a non-variable.',
12,
],
[
'Cannot use ++ on stdClass.',
17,
],
[
'Cannot use ++ on InvalidIncDec\\ClassWithToString.',
19,
],
[
'Cannot use -- on InvalidIncDec\\ClassWithToString.',
21,
],
[
'Cannot use ++ on array{}.',
23,
],
[
'Cannot use -- on array{}.',
25,
],
[
'Cannot use ++ on resource.',
28,
],
[
'Cannot use -- on resource.',
32,
],
];
if (PHP_VERSION_ID >= 80300) {
$errors[] = [
'Cannot use ++ on true.',
36,
'Operator ++ is deprecated for true.',
];
$errors[] = [
'Cannot use -- on false.',
38,
'Operator -- is deprecated for false.',
];
$errors[] = [
'Cannot use -- on null.',
42,
'Operator -- is deprecated for null.',
];
}
$this->analyse([__DIR__ . '/data/invalid-inc-dec.php'], $errors);
}
#[RequiresPhp('>= 8.0.0')]
public function testMixed(): void
{
$this->checkExplicitMixed = true;
$this->checkImplicitMixed = true;
$this->analyse([__DIR__ . '/data/invalid-inc-dec-mixed.php'], [
[
'Cannot use ++ on T of mixed.',
12,
],
[
'Cannot use ++ on T of mixed.',
14,
],
[
'Cannot use -- on T of mixed.',
16,
],
[
'Cannot use -- on T of mixed.',
18,
],
[
'Cannot use ++ on mixed.',
24,
],
[
'Cannot use ++ on mixed.',
26,
],
[
'Cannot use -- on mixed.',
28,
],
[
'Cannot use -- on mixed.',
30,
],
[
'Cannot use ++ on mixed.',
36,
],
[
'Cannot use ++ on mixed.',
38,
],
[
'Cannot use -- on mixed.',
40,
],
[
'Cannot use -- on mixed.',
42,
],
]);
}
public function testUnion(): void
{
$this->analyse([__DIR__ . '/data/invalid-inc-dec-union.php'], [
[
'Cannot use ++ on array|bool|float|int|object|string|null.',
24,
PHP_VERSION_ID >= 80500 ?
'• Operator ++ is deprecated for non-numeric-strings. Either narrow the type to numeric-string, or use str_increment().' . "\n" .
'• Operator ++ is deprecated for bool.'
: (PHP_VERSION_ID >= 80300 ? 'Operator ++ is deprecated for bool.' : null),
],
[
'Cannot use -- on array|bool|float|int|object|string|null.',
26,
PHP_VERSION_ID >= 80300 ?
'• Operator -- is deprecated for non-numeric-strings. Either narrow the type to numeric-string, or use str_decrement().' . "\n" .
'• Operator -- is deprecated for null.' . "\n" .
'• Operator -- is deprecated for bool.'
: null,
],
[
'Cannot use ++ on (array|object).',
29,
],
[
'Cannot use -- on (array|object).',
31,
],
]);
}
public function testDecNonNumericString(): void
{
$errors = [];
if (PHP_VERSION_ID >= 80300) {
$errors = [
[
'Cannot use -- on \'a\'.',
21,
'Operator -- is deprecated for non-numeric-strings. Either narrow the type to numeric-string, or use str_decrement().',
],
[
'Cannot use -- on string.',
23,
'Operator -- is deprecated for non-numeric-strings. Either narrow the type to numeric-string, or use str_decrement().',
],
[
'Cannot use -- on null.',
30,
'Operator -- is deprecated for null.',
],
[
'Cannot use ++ on bool.',
32,
'Operator ++ is deprecated for bool.',
],
[
'Cannot use -- on bool.',
40,
'Operator -- is deprecated for bool.',
],
];
}
$this->analyse([__DIR__ . '/data/dec-non-numeric-string.php'], $errors);
}
public function testIncNonNumericString(): void
{
$errors = [];
if (PHP_VERSION_ID >= 80500) {
$errors = [
[
'Cannot use ++ on \'a\'.',
21,
'Operator ++ is deprecated for non-numeric-strings. Either narrow the type to numeric-string, or use str_increment().',
],
[
'Cannot use ++ on string.',
23,
'Operator ++ is deprecated for non-numeric-strings. Either narrow the type to numeric-string, or use str_increment().',
],
];
}
$this->analyse([__DIR__ . '/data/inc-non-numeric-string.php'], $errors);
}
#[RequiresPhp('>= 8.4.0')]
public function testBcMathNumber(): void
{
$this->analyse([__DIR__ . '/data/inc-dec-bcmath-number.php'], []);
}
public function testGmp(): void
{
$this->analyse([__DIR__ . '/data/inc-dec-gmp.php'], []);
}
}