Skip to content

Commit 9208a02

Browse files
committed
test more cases
1 parent 3517a55 commit 9208a02

3 files changed

Lines changed: 32 additions & 2 deletions

File tree

src/Analyser/TypeSpecifier.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,9 +352,9 @@ public function specifyTypesInCondition(
352352
}
353353

354354
// infer $list[$index] after $index < count($list) - N
355+
// infer $list[$index] after $index <= count($list) - N
355356
if (
356357
$context->true()
357-
&& !$orEqual
358358
&& $expr->right instanceof Expr\BinaryOp\Minus
359359
&& $expr->right->left instanceof FuncCall
360360
&& $expr->right->left->name instanceof Name
@@ -369,7 +369,7 @@ public function specifyTypesInCondition(
369369
$subtractedType = $scope->getType($expr->right->right);
370370
if (
371371
$countArgType->isList()->yes()
372-
&& IntegerRangeType::fromInterval(0, null)->isSuperTypeOf($subtractedType)->yes()
372+
&& IntegerRangeType::fromInterval(1, null)->isSuperTypeOf($subtractedType)->yes()
373373
) {
374374
$arrayArg = $expr->right->left->getArgs()[0]->value;
375375
$dimFetch = new ArrayDimFetch($arrayArg, $expr->left);

tests/PHPStan/Rules/Arrays/NonexistentOffsetInArrayDimFetchRuleTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,6 +1186,10 @@ public function testBug13770(): void
11861186
'Offset -1|3|6|10 might not exist on list<int>.',
11871187
126,
11881188
],
1189+
[
1190+
'Offset int<0, max> might not exist on list<int>.',
1191+
139,
1192+
],
11891193
]);
11901194
}
11911195

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,30 @@ public function constantMaybeNegativeIntLessThanCount(array $array, int $index):
128128

129129
return 0;
130130
}
131+
132+
/**
133+
* @param list<int> $array
134+
* @param 0|positive-int $index
135+
*/
136+
public function ZeroOrMoreIntLessThanOrEqualCount(array $array, int $index): int
137+
{
138+
if ($index <= count($array)) {
139+
return $array[$index]; // SHOULD still report - off by one
140+
}
141+
142+
return 0;
143+
}
144+
145+
/**
146+
* @param list<int> $array
147+
* @param 0|positive-int $index
148+
*/
149+
public function ZeroOrMoreMinusOneIntLessThanOrEqualCount(array $array, int $index): int
150+
{
151+
if ($index <= count($array) - 1) {
152+
return $array[$index];
153+
}
154+
155+
return 0;
156+
}
131157
}

0 commit comments

Comments
 (0)