Skip to content

Narrow offsets for lists with HasOffsetType/HasOffsetValueType #3905

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 28, 2025
Merged
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
18 changes: 18 additions & 0 deletions src/Type/IntersectionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
use PHPStan\Type\Accessory\AccessoryNumericStringType;
use PHPStan\Type\Accessory\AccessoryType;
use PHPStan\Type\Accessory\AccessoryUppercaseStringType;
use PHPStan\Type\Accessory\HasOffsetType;
use PHPStan\Type\Accessory\HasOffsetValueType;
use PHPStan\Type\Accessory\NonEmptyArrayType;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantIntegerType;
Expand All @@ -45,6 +47,7 @@
use function count;
use function implode;
use function in_array;
use function is_int;
use function ksort;
use function md5;
use function sprintf;
Expand Down Expand Up @@ -738,6 +741,21 @@ public function hasOffsetValueType(Type $offsetType): TrinaryLogic
if ((new ConstantIntegerType(0))->isSuperTypeOf($arrayKeyOffsetType)->yes()) {
return TrinaryLogic::createYes();
}

foreach ($this->types as $type) {
if (!$type instanceof HasOffsetValueType && !$type instanceof HasOffsetType) {
continue;
}

foreach ($type->getOffsetType()->getConstantScalarValues() as $constantScalarValue) {
if (!is_int($constantScalarValue)) {
continue;
}
if (IntegerRangeType::fromInterval(0, $constantScalarValue)->isSuperTypeOf($arrayKeyOffsetType)->yes()) {
return TrinaryLogic::createYes();
}
}
}
}

return $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->hasOffsetValueType($offsetType));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,22 @@ public function testNarrowSuperglobals(): void
$this->analyse([__DIR__ . '/data/narrow-superglobal.php'], []);
}

public function testBug12605(): void
{
$this->reportPossiblyNonexistentGeneralArrayOffset = true;

$this->analyse([__DIR__ . '/data/bug-12605.php'], [
[
'Offset 1 might not exist on list<int>.',
19,
],
[
'Offset 10 might not exist on non-empty-list<int>.',
26,
],
]);
}

public function testBug11602(): void
{
$this->reportPossiblyNonexistentGeneralArrayOffset = true;
Expand Down
37 changes: 37 additions & 0 deletions tests/PHPStan/Rules/Arrays/data/bug-12605.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Bug12605;

/**
* @return list<int>
*/
function test(): array
{
return [];
}

function doFoo(): void {
$test = test();

if (isset($test[3])) {
echo $test[1];
}
echo $test[1];
}

function doFooBar(): void {
$test = test();

if (isset($test[4])) {
echo $test[10];
}
}

function doBaz(): void {
$test = test();

if (array_key_exists(5, $test) && is_int($test[5])) {
echo $test[3];
}
}

Loading