Skip to content

Commit 6c78056

Browse files
Fix InvalidKeyInArrayDimFetchRule
1 parent 5e15e2e commit 6c78056

9 files changed

+63
-17
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 [];
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[
2+
{
3+
"message": "Invalid array key type object.",
4+
"line": 20,
5+
"ignorable": true
6+
}
7+
]
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[
2+
{
3+
"message": "Possibly invalid array key type mixed.",
4+
"line": 22,
5+
"ignorable": true
6+
}
7+
]

tests/PHPStan/Levels/data/arrayOffsetAccess-7.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"ignorable": true
66
},
77
{
8-
"message": "Possibly invalid array key type object|null.",
8+
"message": "Invalid array key type object.",
99
"line": 20,
1010
"ignorable": true
1111
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[
2+
{
3+
"message": "Invalid array key type object.",
4+
"line": 20,
5+
"ignorable": true
6+
}
7+
]
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[
2+
{
3+
"message": "Possibly invalid array key type object|null.",
4+
"line": 20,
5+
"ignorable": true
6+
}
7+
]
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[
2+
{
3+
"message": "Invalid array key type object.",
4+
"line": 20,
5+
"ignorable": true
6+
}
7+
]
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[
2+
{
3+
"message": "Possibly invalid array key type mixed.",
4+
"line": 21,
5+
"ignorable": true
6+
}
7+
]

tests/PHPStan/Levels/data/arrayOffsetAccess.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
namespace Levels\ArrayOffsetAccess;
44

55
class Foo {
6-
/** @return void */
7-
public function test(
8-
array $a,
9-
int|null $intOrNull,
10-
object|int $objectOrInt,
11-
object|null $objectOrNull,
12-
mixed $explicitlyMixed,
13-
$implicitlyMixed,
14-
) {
6+
/**
7+
* @param int|null $intOrNull
8+
* @param object|int $objectOrInt
9+
* @param object|null $objectOrNull
10+
* @param mixed $explicitlyMixed
11+
* @return void
12+
*/
13+
public function test(array $a, $intOrNull, $objectOrInt, $objectOrNull, $explicitlyMixed, $implicitlyMixed)
14+
{
1515
$a[42];
1616
$a[null];
1717
$a[new \DateTimeImmutable()];

0 commit comments

Comments
 (0)