forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInvalidKeyInArrayDimFetchRuleTest.php
More file actions
191 lines (175 loc) · 3.53 KB
/
InvalidKeyInArrayDimFetchRuleTest.php
File metadata and controls
191 lines (175 loc) · 3.53 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
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Arrays;
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<InvalidKeyInArrayDimFetchRule>
*/
class InvalidKeyInArrayDimFetchRuleTest extends RuleTestCase
{
private bool $reportNonIntStringArrayKey = false;
protected function getRule(): Rule
{
$ruleLevelHelper = new RuleLevelHelper(self::createReflectionProvider(), true, false, true, true, true, false, true);
return new InvalidKeyInArrayDimFetchRule(
$ruleLevelHelper,
self::getContainer()->getByType(PhpVersion::class),
true,
$this->reportNonIntStringArrayKey,
);
}
public function testInvalidKey(): void
{
$errors = [
[
'Invalid array key type DateTimeImmutable.',
7,
],
[
'Invalid array key type array.',
8,
],
[
'Possibly invalid array key type stdClass|string.',
24,
],
[
'Invalid array key type DateTimeImmutable.',
31,
],
[
'Possibly invalid array key type mixed.',
41,
],
[
'Invalid array key type DateTimeImmutable.',
45,
],
[
'Invalid array key type DateTimeImmutable.',
46,
],
[
'Invalid array key type DateTimeImmutable.',
47,
],
[
'Invalid array key type stdClass.',
47,
],
[
'Invalid array key type DateTimeImmutable.',
48,
],
];
if (PHP_VERSION_ID >= 80100) {
$errors[] = [
'Invalid array key type float.',
51,
];
}
if (PHP_VERSION_ID >= 80500) {
$errors[] = [
'Invalid array key type null.',
52,
];
$errors[] = [
'Possibly invalid array key type string|null.',
56,
];
}
$this->analyse([__DIR__ . '/data/invalid-key-array-dim-fetch.php'], $errors);
}
#[RequiresPhp('>= 8.1')]
public function testBug6315(): void
{
$this->analyse([__DIR__ . '/data/bug-6315.php'], [
[
'Invalid array key type Bug6315\FooEnum::A.',
18,
],
[
'Invalid array key type Bug6315\FooEnum::A.',
19,
],
[
'Invalid array key type Bug6315\FooEnum::A.',
20,
],
[
'Invalid array key type Bug6315\FooEnum::B.',
21,
],
[
'Invalid array key type Bug6315\FooEnum::A.',
21,
],
[
'Invalid array key type Bug6315\FooEnum::A.',
22,
],
]);
}
public function testBug13135(): void
{
$this->analyse([__DIR__ . '/data/bug-13135.php'], [
[
'Possibly invalid array key type Tk of mixed.',
15,
],
]);
}
public function testBug12273(): void
{
$this->analyse([__DIR__ . '/data/bug-12273.php'], [
[
'Possibly invalid array key type mixed.',
16,
],
]);
}
#[RequiresPhp('>= 8.1')]
public function testBug12798(): void
{
$this->analyse([__DIR__ . '/../Comparison/data/bug-12798.php'], []);
}
public function testBug12981(): void
{
$this->analyse([__DIR__ . '/data/bug-12981.php'], [
[
'Invalid array key type array<int, int|string>.',
31,
],
[
'Invalid array key type array<int, int|string>.',
33,
],
[
'Possibly invalid array key type array<int, int|string>|int|string.',
39,
],
[
'Possibly invalid array key type array<int, int|string>|int|string.',
41,
],
]);
}
public function testUnsetFalseKey(): void
{
$this->reportNonIntStringArrayKey = true;
$this->analyse([__DIR__ . '/data/unset-false-key.php'], [
[
'Invalid array key type false.',
6,
],
[
'Invalid array key type false.',
13,
],
]);
}
}