Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,8 @@ public function specifyTypesInCondition(
&& in_array(strtolower((string) $expr->right->name), ['count', 'sizeof'], true)
&& $leftType->isInteger()->yes()
) {
$argType = $scope->getType($expr->right->getArgs()[0]->value);
$argExpr = $expr->right->getArgs()[0]->value;
$argType = $scope->getType($argExpr);

if ($leftType instanceof ConstantIntegerType) {
if ($orEqual) {
Expand All @@ -291,6 +292,26 @@ public function specifyTypesInCondition(
if ($specifiedTypes !== null) {
$result = $result->unionWith($specifiedTypes);
}
if (
$context->true()
&& $expr instanceof Node\Expr\BinaryOp\Smaller
&& $argType->isList()->yes()
&& $argExpr instanceof Expr\Variable
Copy link
Contributor

@staabm staabm Nov 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instanceof Expr\Variable is usually a bug as most logic works for other expressions (e.g. a pure function call can also be remembered).

most of the time this works just by dropping this line

&& IntegerRangeType::fromInterval(0, null)->isSuperTypeOf($leftType)->yes()
) {
$dimFetch = new ArrayDimFetch(
$argExpr,
$expr->left,
);
$result = $result->unionWith(
$this->create(
$dimFetch,
$argType->getIterableValueType(),
TypeSpecifierContext::createTrue(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

usually we pass thru the $context, instead of creating a new one.

$scope,
)
);
}

if (
$context->true() && (IntegerRangeType::createAllGreaterThanOrEqualTo(1 - $offset)->isSuperTypeOf($leftType)->yes())
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/nsrt/bug-10264.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function sayHello(array $c): void
assertType('list<int>', $c);
if (count($c) > 0) {
$c = array_map(fn() => new stdClass(), $c);
assertType('non-empty-list<stdClass>', $c);
assertType('non-empty-list<stdClass>&hasOffsetValue(0, stdClass)', $c);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this type does not make sense, because non-empty-list<stdClass> already implies that the list has a offset 0 with value stdClass

} else {
assertType('array{}', $c);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,22 @@ public static function dataReportPossiblyNonexistentArrayOffset(): iterable
"Offset 'foo' might not exist on array.",
9,
],
[
'Offset int might not exist on list<int>.',
77
],
[
'Offset int<0, max> might not exist on list<int>.',
88
],
[
'Offset int<0, max> might not exist on array<int, int>.',
100
],
[
'Offset int<0, max> might not exist on array<int<min, 4>|int<6, max>, int>.',
112
],
]];
yield [true, true, [
[
Expand All @@ -710,6 +726,22 @@ public static function dataReportPossiblyNonexistentArrayOffset(): iterable
'Offset string might not exist on array{foo: 1}.',
20,
],
[
'Offset int might not exist on list<int>.',
77
],
[
'Offset int<0, max> might not exist on list<int>.',
88
],
[
'Offset int<0, max> might not exist on array<int, int>.',
100
],
[
'Offset int<0, max> might not exist on array<int<min, 4>|int<6, max>, int>.',
112
],
]];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,60 @@ public function nonEmpty(array $a): void
echo $a[0];
}

/**
* @param list<int> $array
* @param non-negative-int $index
*/
public function guard(array $array, int $index) {
if ($index < count($array)) {
return $array[$index];
}
return null;
}


/**
* @param list<int> $array
*/
public function guardNotSafeLowerBound(array $array, int $index) {
if ($index < count($array)) {
return $array[$index];
}
return null;
}

/**
* @param list<int> $array
* @param non-negative-int $index
*/
public function guardNotSafeUpperBound(array $array, int $index) {
if ($index <= count($array)) {
return $array[$index];
}
return null;
}


/**
* @param array<int, int> $array
* @param non-negative-int $index
*/
public function guardNotSafeArray(array $array, int $index) {
if ($index <= count($array)) {
return $array[$index];
}
return null;
}

/**
* @param array<int, int> $array
* @param non-negative-int $index
*/
public function guardNotSafeBecauseRewrite(array $array, int $index) {
if ($index <= count($array)) {
unset($array[5]);
return $array[$index];
}
return null;
}
}
Loading