Skip to content

Commit 2caa239

Browse files
Respect phpstan level in InvalidKeyInArrayDimFetchRule
1 parent d81cb77 commit 2caa239

File tree

2 files changed

+117
-8
lines changed

2 files changed

+117
-8
lines changed

src/Rules/Arrays/InvalidKeyInArrayDimFetchRule.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use PHPStan\Rules\RuleErrorBuilder;
1111
use PHPStan\Rules\RuleLevelHelper;
1212
use PHPStan\Type\ErrorType;
13-
use PHPStan\Type\MixedType;
1413
use PHPStan\Type\Type;
1514
use PHPStan\Type\VerbosityLevel;
1615
use function sprintf;
@@ -41,22 +40,27 @@ public function processNode(Node $node, Scope $scope): array
4140
return [];
4241
}
4342

44-
$dimensionType = $scope->getType($node->dim);
45-
if ($dimensionType instanceof MixedType) {
46-
return [];
47-
}
48-
4943
$varType = $this->ruleLevelHelper->findTypeToCheck(
5044
$scope,
5145
$node->var,
5246
'',
53-
static fn (Type $varType): bool => $varType->isArray()->no() || AllowedArrayKeysTypes::getType()->isSuperTypeOf($dimensionType)->yes(),
47+
static fn (Type $varType): bool => $varType->isArray()->no(),
5448
)->getType();
5549

5650
if ($varType instanceof ErrorType || $varType->isArray()->no()) {
5751
return [];
5852
}
5953

54+
$dimensionType = $this->ruleLevelHelper->findTypeToCheck(
55+
$scope,
56+
$node->dim,
57+
'',
58+
static fn (Type $dimType): bool => AllowedArrayKeysTypes::getType()->isSuperTypeOf($dimType)->yes(),
59+
)->getType();
60+
if ($dimensionType instanceof ErrorType) {
61+
return [];
62+
}
63+
6064
$isSuperType = AllowedArrayKeysTypes::getType()->isSuperTypeOf($dimensionType);
6165
if ($isSuperType->yes() || ($isSuperType->maybe() && !$this->reportMaybes)) {
6266
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)