Skip to content

Commit 150a265

Browse files
Respect phpstan level in InvalidKeyInArrayDimFetchRule
1 parent d81cb77 commit 150a265

File tree

2 files changed

+114
-7
lines changed

2 files changed

+114
-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: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,23 @@
1212
*/
1313
class InvalidKeyInArrayDimFetchRuleTest extends RuleTestCase
1414
{
15+
private bool $checkUnion = true;
16+
private bool $checkExplicitMixed = false;
17+
private bool $checkImplicitMixed = false;
1518

1619
protected function getRule(): Rule
1720
{
18-
$ruleLevelHelper = new RuleLevelHelper(self::createReflectionProvider(), true, false, true, false, false, false, true);
21+
$ruleLevelHelper = new RuleLevelHelper(
22+
self::createReflectionProvider(),
23+
true,
24+
false,
25+
$this->checkUnion,
26+
$this->checkExplicitMixed,
27+
$this->checkImplicitMixed,
28+
false,
29+
true,
30+
);
31+
1932
return new InvalidKeyInArrayDimFetchRule($ruleLevelHelper, true);
2033
}
2134

@@ -61,6 +74,95 @@ public function testInvalidKey(): void
6174
]);
6275
}
6376

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

0 commit comments

Comments
 (0)