Skip to content

Commit c6e0827

Browse files
committed
more tests
1 parent 1425cc2 commit c6e0827

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
@@ -1174,9 +1174,17 @@ public function testBug13770(): void
11741174
'Offset int might not exist on list<int>.',
11751175
66,
11761176
],
1177+
[
1178+
'Offset int might not exist on list<int>.',
1179+
91,
1180+
],
11771181
[
11781182
'Offset int might not exist on array.',
1179-
75,
1183+
100,
1184+
],
1185+
[
1186+
'Offset -1|3|6|10 might not exist on list<int>.',
1187+
126,
11801188
],
11811189
]);
11821190
}

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)