Skip to content

Commit 2c7e290

Browse files
committed
Fix test because of more explicit type
1 parent fa337a9 commit 2c7e290

File tree

4 files changed

+25
-3
lines changed

4 files changed

+25
-3
lines changed

src/Analyser/TypeSpecifier.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,8 @@ public function specifyTypesInCondition(
273273
&& in_array(strtolower((string) $expr->right->name), ['count', 'sizeof'], true)
274274
&& $leftType->isInteger()->yes()
275275
) {
276-
$argType = $scope->getType($expr->right->getArgs()[0]->value);
276+
$argExpr = $expr->right->getArgs()[0]->value;
277+
$argType = $scope->getType($argExpr);
277278

278279
if ($leftType instanceof ConstantIntegerType) {
279280
if ($orEqual) {
@@ -295,10 +296,11 @@ public function specifyTypesInCondition(
295296
$context->true()
296297
&& $expr instanceof Node\Expr\BinaryOp\Smaller
297298
&& $argType->isList()->yes()
299+
&& $argExpr instanceof Expr\Variable
298300
&& IntegerRangeType::fromInterval(0, null)->isSuperTypeOf($leftType)->yes()
299301
) {
300302
$dimFetch = new ArrayDimFetch(
301-
$expr->right->getArgs()[0]->value,
303+
$argExpr,
302304
$expr->left,
303305
);
304306
$result = $result->unionWith(

tests/PHPStan/Analyser/nsrt/bug-10264.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function sayHello(array $c): void
3333
assertType('list<int>', $c);
3434
if (count($c) > 0) {
3535
$c = array_map(fn() => new stdClass(), $c);
36-
assertType('non-empty-list<stdClass>', $c);
36+
assertType('non-empty-list<stdClass>&hasOffsetValue(0, stdClass)', $c);
3737
} else {
3838
assertType('array{}', $c);
3939
}

tests/PHPStan/Rules/Arrays/NonexistentOffsetInArrayDimFetchRuleTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,10 @@ public static function dataReportPossiblyNonexistentArrayOffset(): iterable
712712
'Offset int<0, max> might not exist on array<int, int>.',
713713
100
714714
],
715+
[
716+
'Offset int<0, max> might not exist on array<int<min, 4>|int<6, max>, int>.',
717+
112
718+
],
715719
]];
716720
yield [true, true, [
717721
[
@@ -734,6 +738,10 @@ public static function dataReportPossiblyNonexistentArrayOffset(): iterable
734738
'Offset int<0, max> might not exist on array<int, int>.',
735739
100
736740
],
741+
[
742+
'Offset int<0, max> might not exist on array<int<min, 4>|int<6, max>, int>.',
743+
112
744+
],
737745
]];
738746
}
739747

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,16 @@ public function guardNotSafeArray(array $array, int $index) {
101101
}
102102
return null;
103103
}
104+
105+
/**
106+
* @param array<int, int> $array
107+
* @param non-negative-int $index
108+
*/
109+
public function guardNotSafeBecauseRewrite(array $array, int $index) {
110+
if ($index <= count($array)) {
111+
unset($array[5]);
112+
return $array[$index];
113+
}
114+
return null;
115+
}
104116
}

0 commit comments

Comments
 (0)