Skip to content

Commit fa337a9

Browse files
committed
add more test cases
1 parent 679d274 commit fa337a9

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

tests/PHPStan/Rules/Arrays/NonexistentOffsetInArrayDimFetchRuleTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,14 @@ public static function dataReportPossiblyNonexistentArrayOffset(): iterable
704704
'Offset int might not exist on list<int>.',
705705
77
706706
],
707+
[
708+
'Offset int<0, max> might not exist on list<int>.',
709+
88
710+
],
711+
[
712+
'Offset int<0, max> might not exist on array<int, int>.',
713+
100
714+
],
707715
]];
708716
yield [true, true, [
709717
[
@@ -718,6 +726,14 @@ public static function dataReportPossiblyNonexistentArrayOffset(): iterable
718726
'Offset int might not exist on list<int>.',
719727
77
720728
],
729+
[
730+
'Offset int<0, max> might not exist on list<int>.',
731+
88
732+
],
733+
[
734+
'Offset int<0, max> might not exist on array<int, int>.',
735+
100
736+
],
721737
]];
722738
}
723739

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

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function nonEmpty(array $a): void
5959

6060
/**
6161
* @param list<int> $array
62-
* @param positive-int $index
62+
* @param non-negative-int $index
6363
*/
6464
public function guard(array $array, int $index) {
6565
if ($index < count($array)) {
@@ -72,10 +72,33 @@ public function guard(array $array, int $index) {
7272
/**
7373
* @param list<int> $array
7474
*/
75-
public function guardNotSafe(array $array, int $index) {
75+
public function guardNotSafeLowerBound(array $array, int $index) {
7676
if ($index < count($array)) {
7777
return $array[$index];
7878
}
7979
return null;
8080
}
81+
82+
/**
83+
* @param list<int> $array
84+
* @param non-negative-int $index
85+
*/
86+
public function guardNotSafeUpperBound(array $array, int $index) {
87+
if ($index <= count($array)) {
88+
return $array[$index];
89+
}
90+
return null;
91+
}
92+
93+
94+
/**
95+
* @param array<int, int> $array
96+
* @param non-negative-int $index
97+
*/
98+
public function guardNotSafeArray(array $array, int $index) {
99+
if ($index <= count($array)) {
100+
return $array[$index];
101+
}
102+
return null;
103+
}
81104
}

0 commit comments

Comments
 (0)