Skip to content

Commit aef2632

Browse files
committed
more tests
1 parent 9278b3b commit aef2632

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

tests/PHPStan/Rules/Arrays/NonexistentOffsetInArrayDimFetchRuleTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1168,9 +1168,17 @@ public function testBug13770(): void
11681168
'Offset int might not exist on list<int>.',
11691169
66,
11701170
],
1171+
[
1172+
'Offset int might not exist on list<int>.',
1173+
91,
1174+
],
11711175
[
11721176
'Offset int might not exist on array.',
1173-
75,
1177+
100,
1178+
],
1179+
[
1180+
'Offset -1|3|6|10 might not exist on list<int>.',
1181+
126,
11741182
],
11751183
]);
11761184
}

tests/PHPStan/Rules/Arrays/data/bug-13770.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,31 @@ public function anyIntLessThanCount(array $array, int $index): int
6969
return 0;
7070
}
7171

72+
/**
73+
* @param list<int> $array
74+
* @param int<0, max> $index
75+
*/
76+
public function positiveIntOnNormalCountMode(array $array, int $index): int
77+
{
78+
if ($index < count($array, COUNT_NORMAL)) {
79+
return $array[$index]; // should not error
80+
}
81+
82+
return 0;
83+
}
84+
85+
/**
86+
* @param list<int> $array
87+
*/
88+
public function anyIntOnUnknownCountMode(array $array, int $index, $countMode): int
89+
{
90+
if ($index < count($array, $countMode)) {
91+
return $array[$index]; // SHOULD still report - could be negative
92+
}
93+
94+
return 0;
95+
}
96+
7297
public function anyIntOnRecursiveCount(array $array, int $index): int
7398
{
7499
if ($index < count($array, COUNT_RECURSIVE)) {
@@ -77,4 +102,30 @@ public function anyIntOnRecursiveCount(array $array, int $index): int
77102

78103
return 0;
79104
}
105+
106+
/**
107+
* @param list<int> $array
108+
* @param 3|6|10 $index
109+
*/
110+
public function constantPositiveIntLessThanCount(array $array, int $index): int
111+
{
112+
if ($index < count($array)) {
113+
return $array[$index]; // should not report
114+
}
115+
116+
return 0;
117+
}
118+
119+
/**
120+
* @param list<int> $array
121+
* @param -1|3|6|10 $index
122+
*/
123+
public function constantMaybeNegativeIntLessThanCount(array $array, int $index): int
124+
{
125+
if ($index < count($array)) {
126+
return $array[$index]; // SHOULD still report - could be negative
127+
}
128+
129+
return 0;
130+
}
80131
}

0 commit comments

Comments
 (0)