Skip to content

Commit b32f7fe

Browse files
Respect phpstan level in InvalidKeyInArrayDimFetchRule
1 parent d81cb77 commit b32f7fe

File tree

2 files changed

+117
-7
lines changed

2 files changed

+117
-7
lines changed

src/Rules/Arrays/InvalidKeyInArrayDimFetchRule.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,27 @@ public function processNode(Node $node, Scope $scope): array
4141
return [];
4242
}
4343

44-
$dimensionType = $scope->getType($node->dim);
45-
if ($dimensionType instanceof MixedType) {
46-
return [];
47-
}
48-
4944
$varType = $this->ruleLevelHelper->findTypeToCheck(
5045
$scope,
5146
$node->var,
5247
'',
53-
static fn (Type $varType): bool => $varType->isArray()->no() || AllowedArrayKeysTypes::getType()->isSuperTypeOf($dimensionType)->yes(),
48+
static fn (Type $varType): bool => $varType->isArray()->no(),
5449
)->getType();
5550

5651
if ($varType instanceof ErrorType || $varType->isArray()->no()) {
5752
return [];
5853
}
5954

55+
$dimensionType = $this->ruleLevelHelper->findTypeToCheck(
56+
$scope,
57+
$node->dim,
58+
'',
59+
static fn (Type $dimType): bool => AllowedArrayKeysTypes::getType()->isSuperTypeOf($dimType)->yes(),
60+
)->getType();
61+
if ($dimensionType instanceof ErrorType) {
62+
return [];
63+
}
64+
6065
$isSuperType = AllowedArrayKeysTypes::getType()->isSuperTypeOf($dimensionType);
6166
if ($isSuperType->yes() || ($isSuperType->maybe() && !$this->reportMaybes)) {
6267
return [];

tests/PHPStan/Rules/Arrays/InvalidKeyInArrayDimFetchRuleTest.php

Lines changed: 106 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,25 @@
1313
class InvalidKeyInArrayDimFetchRuleTest extends RuleTestCase
1414
{
1515

16+
private bool $checkUnion = true;
17+
18+
private bool $checkExplicitMixed = false;
19+
20+
private bool $checkImplicitMixed = false;
21+
1622
protected function getRule(): Rule
1723
{
18-
$ruleLevelHelper = new RuleLevelHelper(self::createReflectionProvider(), true, false, true, false, false, false, true);
24+
$ruleLevelHelper = new RuleLevelHelper(
25+
self::createReflectionProvider(),
26+
true,
27+
false,
28+
$this->checkUnion,
29+
$this->checkExplicitMixed,
30+
$this->checkImplicitMixed,
31+
false,
32+
true,
33+
);
34+
1935
return new InvalidKeyInArrayDimFetchRule($ruleLevelHelper, true);
2036
}
2137

@@ -61,6 +77,95 @@ public function testInvalidKey(): void
6177
]);
6278
}
6379

80+
public function testInvalidKeyOnLevel6(): void
81+
{
82+
$this->checkUnion = false;
83+
84+
$this->analyse([__DIR__ . '/data/invalid-key-array-dim-fetch.php'], [
85+
[
86+
'Invalid array key type DateTimeImmutable.',
87+
7,
88+
],
89+
[
90+
'Invalid array key type array.',
91+
8,
92+
],
93+
[
94+
'Invalid array key type DateTimeImmutable.',
95+
31,
96+
],
97+
[
98+
'Invalid array key type DateTimeImmutable.',
99+
45,
100+
],
101+
[
102+
'Invalid array key type DateTimeImmutable.',
103+
46,
104+
],
105+
[
106+
'Invalid array key type DateTimeImmutable.',
107+
47,
108+
],
109+
[
110+
'Invalid array key type stdClass.',
111+
47,
112+
],
113+
[
114+
'Invalid array key type DateTimeImmutable.',
115+
48,
116+
],
117+
]);
118+
}
119+
120+
public function testInvalidKeyOnLevel10(): void
121+
{
122+
$this->checkExplicitMixed = true;
123+
$this->checkImplicitMixed = true;
124+
125+
$this->analyse([__DIR__ . '/data/invalid-key-array-dim-fetch.php'], [
126+
[
127+
'Invalid array key type DateTimeImmutable.',
128+
7,
129+
],
130+
[
131+
'Invalid array key type array.',
132+
8,
133+
],
134+
[
135+
'Possibly invalid array key type stdClass|string.',
136+
24,
137+
],
138+
[
139+
'Invalid array key type DateTimeImmutable.',
140+
31,
141+
],
142+
[
143+
'Possibly invalid array key type mixed.',
144+
41,
145+
],
146+
[
147+
'Invalid array key type DateTimeImmutable.',
148+
45,
149+
],
150+
[
151+
'Invalid array key type DateTimeImmutable.',
152+
46,
153+
],
154+
[
155+
'Invalid array key type DateTimeImmutable.',
156+
47,
157+
],
158+
[
159+
'Invalid array key type stdClass.',
160+
47,
161+
],
162+
[
163+
'Invalid array key type DateTimeImmutable.',
164+
48,
165+
],
166+
]);
167+
}
168+
64169
#[RequiresPhp('>= 8.1')]
65170
public function testBug6315(): void
66171
{

0 commit comments

Comments
 (0)