Skip to content
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

Fix lost list-type if substituted a element via loop #3908

Merged
merged 4 commits into from
Mar 30, 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
35 changes: 31 additions & 4 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -5450,14 +5450,15 @@ private function processAssignVar(
$offsetValueType = $varType;
$offsetNativeValueType = $varNativeType;

$valueToWrite = $this->produceArrayDimFetchAssignValueToWrite($offsetTypes, $offsetValueType, $valueToWrite);
$valueToWrite = $this->produceArrayDimFetchAssignValueToWrite($dimFetchStack, $offsetTypes, $offsetValueType, $valueToWrite, $scope);

if (!$offsetValueType->equals($offsetNativeValueType) || !$valueToWrite->equals($nativeValueToWrite)) {
$nativeValueToWrite = $this->produceArrayDimFetchAssignValueToWrite($offsetNativeTypes, $offsetNativeValueType, $nativeValueToWrite);
$nativeValueToWrite = $this->produceArrayDimFetchAssignValueToWrite($dimFetchStack, $offsetNativeTypes, $offsetNativeValueType, $nativeValueToWrite, $scope);
} else {
$rewritten = false;
foreach ($offsetTypes as $i => $offsetType) {
$offsetNativeType = $offsetNativeTypes[$i];

if ($offsetType === null) {
if ($offsetNativeType !== null) {
throw new ShouldNotHappenException();
Expand All @@ -5471,7 +5472,7 @@ private function processAssignVar(
continue;
}

$nativeValueToWrite = $this->produceArrayDimFetchAssignValueToWrite($offsetNativeTypes, $offsetNativeValueType, $nativeValueToWrite);
$nativeValueToWrite = $this->produceArrayDimFetchAssignValueToWrite($dimFetchStack, $offsetNativeTypes, $offsetNativeValueType, $nativeValueToWrite, $scope);
$rewritten = true;
break;
}
Expand Down Expand Up @@ -5784,9 +5785,10 @@ static function (): void {
}

/**
* @param list<ArrayDimFetch> $dimFetchStack
* @param list<Type|null> $offsetTypes
*/
private function produceArrayDimFetchAssignValueToWrite(array $offsetTypes, Type $offsetValueType, Type $valueToWrite): Type
private function produceArrayDimFetchAssignValueToWrite(array $dimFetchStack, array $offsetTypes, Type $offsetValueType, Type $valueToWrite, Scope $scope): Type
{
$offsetValueTypeStack = [$offsetValueType];
foreach (array_slice($offsetTypes, 0, -1) as $offsetType) {
Expand Down Expand Up @@ -5821,6 +5823,31 @@ private function produceArrayDimFetchAssignValueToWrite(array $offsetTypes, Type
$offsetValueType = TypeCombinator::intersect($offsetValueType, TypeCombinator::union(...$types));
}
$valueToWrite = $offsetValueType->setOffsetValueType($offsetType, $valueToWrite, $i === 0);

$arrayDimFetch = $dimFetchStack[$i] ?? null;
if ($arrayDimFetch === null || !$offsetValueType->isList()->yes()) {
continue;
}

if ($scope->hasExpressionType($arrayDimFetch)->yes()) { // keep list for $list[$index] assignments
$valueToWrite = TypeCombinator::intersect($valueToWrite, new AccessoryArrayListType());
} elseif ($arrayDimFetch->dim instanceof BinaryOp\Plus) {
if ( // keep list for $list[$index + 1] assignments
$arrayDimFetch->dim->right instanceof Variable
&& $arrayDimFetch->dim->left instanceof Node\Scalar\Int_
&& $arrayDimFetch->dim->left->value === 1
&& $scope->hasExpressionType(new ArrayDimFetch($arrayDimFetch->var, $arrayDimFetch->dim->right))->yes()
) {
$valueToWrite = TypeCombinator::intersect($valueToWrite, new AccessoryArrayListType());
} elseif ( // keep list for $list[1 + $index] assignments
$arrayDimFetch->dim->left instanceof Variable
&& $arrayDimFetch->dim->right instanceof Node\Scalar\Int_
&& $arrayDimFetch->dim->right->value === 1
&& $scope->hasExpressionType(new ArrayDimFetch($arrayDimFetch->var, $arrayDimFetch->dim->left))->yes()
) {
$valueToWrite = TypeCombinator::intersect($valueToWrite, new AccessoryArrayListType());
}
}
}

return $valueToWrite;
Expand Down
109 changes: 109 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-12274.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php declare(strict_types=1);

namespace Bug12274;

use function PHPStan\Testing\assertType;

/**
* @param non-empty-list<int> $items
*
* @return non-empty-list<int>
*/
function getItems(array $items): array
{
foreach ($items as $index => $item) {
$items[$index] = 1;
}

assertType('non-empty-list<int>', $items);
return $items;
}

/**
* @param non-empty-list<int> $items
*
* @return non-empty-list<int>
*/
function getItemsByModifiedIndex(array $items): array
{
foreach ($items as $index => $item) {
$index++;

$items[$index] = 1;
}

assertType('non-empty-array<int<0, max>, int>', $items);
return $items;
}

/** @param list<int> $list */
function testKeepListAfterIssetIndex(array $list, int $i): void
{
if (isset($list[$i])) {
assertType('list<int>', $list);
$list[$i] = 21;
assertType('non-empty-list<int>', $list);
$list[$i+1] = 21;
assertType('non-empty-list<int>', $list);
}
assertType('list<int>', $list);
}

/** @param list<list<int>> $nestedList */
function testKeepNestedListAfterIssetIndex(array $nestedList, int $i, int $j): void
{
if (isset($nestedList[$i][$j])) {
assertType('list<list<int>>', $nestedList);
assertType('list<int>', $nestedList[$i]);
$nestedList[$i][$j] = 21;
assertType('non-empty-list<non-empty-list<int>>', $nestedList);
assertType('non-empty-list<int>', $nestedList[$i]);
}
assertType('list<list<int>>', $nestedList);
}

/** @param list<int> $list */
function testKeepListAfterIssetIndexPlusOne(array $list, int $i): void
{
if (isset($list[$i])) {
assertType('list<int>', $list);
$list[$i+1] = 21;
assertType('non-empty-list<int>', $list);
}
assertType('list<int>', $list);
}

/** @param list<int> $list */
function testKeepListAfterIssetIndexOnePlus(array $list, int $i): void
{
if (isset($list[$i])) {
assertType('list<int>', $list);
$list[1+$i] = 21;
assertType('non-empty-list<int>', $list);
}
assertType('list<int>', $list);
}

/** @param list<int> $list */
function testShouldLooseListbyAst(array $list, int $i): void
{
if (isset($list[$i])) {
$i++;

assertType('list<int>', $list);
$list[1+$i] = 21;
assertType('non-empty-array<int, int>', $list);
}
assertType('array<int, int>', $list);
}

/** @param list<int> $list */
function testShouldLooseListbyAst2(array $list, int $i): void
{
if (isset($list[$i])) {
assertType('list<int>', $list);
$list[2+$i] = 21;
assertType('non-empty-array<int, int>', $list);
}
assertType('array<int, int>', $list);
}
14 changes: 14 additions & 0 deletions tests/PHPStan/Rules/Functions/ReturnTypeRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -345,4 +345,18 @@ public function testBug11301(): void
]);
}

public function testBug12274(): void
{
$this->checkExplicitMixed = true;
$this->checkNullables = true;

$this->analyse([__DIR__ . '/../../Analyser/nsrt/bug-12274.php'], [
[
'Function Bug12274\getItemsByModifiedIndex() should return non-empty-list<int> but returns non-empty-array<int<0, max>, int>.',
36,
'non-empty-array<int<0, max>, int> might not be a list.',
],
]);
}

}
Loading