Skip to content

Commit d4bf4f5

Browse files
committed
Fix reportPossiblyNonexistentConstantArrayOffset
1 parent 326b985 commit d4bf4f5

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/Rules/Arrays/NonexistentOffsetInArrayDimFetchCheck.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,15 @@ public function check(
7575
$this->reportPossiblyNonexistentGeneralArrayOffset
7676
&& $innerType->isArray()->yes()
7777
&& !$innerType->isConstantArray()->yes()
78+
&& !$innerType->hasOffsetValueType($dimType)->yes()
7879
) {
7980
$report = true;
8081
break;
8182
}
8283
if (
8384
$this->reportPossiblyNonexistentConstantArrayOffset
8485
&& $innerType->isConstantArray()->yes()
86+
&& !$innerType->hasOffsetValueType($dimType)->yes()
8587
) {
8688
$report = true;
8789
break;

tests/PHPStan/Rules/Arrays/data/report-possibly-nonexistent-array-offset.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,24 @@ public function constantArrayWithPossiblyUndefinedArrayOffset(string $s): void
1919
$a = ['foo' => 1];
2020
echo $a[$s];
2121
}
22+
23+
/**
24+
* @param array{bool|float|int|string|null} $a
25+
* @return void
26+
*/
27+
public function testConstantArray(array $a): void
28+
{
29+
echo $a[0];
30+
}
31+
32+
/**
33+
* @param array<int, bool> $a
34+
* @return void
35+
*/
36+
public function testConstantArray2(array $a): void
37+
{
38+
if (isset($a[0])) {
39+
echo $a[0];
40+
}
41+
}
2242
}

0 commit comments

Comments
 (0)