Skip to content

Commit c525e0e

Browse files
Add fix
1 parent 106905c commit c525e0e

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

src/Rules/Arrays/NonexistentOffsetInArrayDimFetchCheck.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use PHPStan\Rules\RuleLevelHelper;
1313
use PHPStan\Type\BenevolentUnionType;
1414
use PHPStan\Type\ErrorType;
15+
use PHPStan\Type\MixedType;
1516
use PHPStan\Type\NeverType;
1617
use PHPStan\Type\Type;
1718
use PHPStan\Type\TypeCombinator;
@@ -86,7 +87,9 @@ public function check(
8687
$flattenedTypes = TypeUtils::flattenTypes($type);
8788
}
8889

89-
$validArrayDimType = TypeCombinator::intersect(AllowedArrayKeysTypes::getType(), $dimType);
90+
$validArrayDimType = $dimType instanceof MixedType
91+
? $dimType
92+
: TypeCombinator::intersect(AllowedArrayKeysTypes::getType(), $dimType);
9093

9194
foreach ($flattenedTypes as $innerType) {
9295
$dimTypeToCheck = $innerType->isArray()->yes() ? $validArrayDimType : $dimType;

tests/PHPStan/Rules/Arrays/NonexistentOffsetInArrayDimFetchRuleTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,6 +1094,11 @@ public function testPR4385(): void
10941094
]);
10951095
}
10961096

1097+
public function testPR4385Bis(): void
1098+
{
1099+
$this->analyse([__DIR__ . '/data/pr-4385-bis.php'], []);
1100+
}
1101+
10971102
public function testBug12805(): void
10981103
{
10991104
$this->reportPossiblyNonexistentGeneralArrayOffset = true;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Pr4385Bis;
4+
5+
class HelloWorld
6+
{
7+
/** @param array<string, int> $a */
8+
public function sayHello(array $a, mixed $m): void
9+
{
10+
echo $a[$m];
11+
}
12+
}

0 commit comments

Comments
 (0)