-
Notifications
You must be signed in to change notification settings - Fork 574
Fix phpstan/phpstan#14245: list lost after overwriting last element #5169
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
staabm
merged 9 commits into
phpstan:2.1.x
from
phpstan-bot:create-pull-request/patch-nva5u2r
Mar 10, 2026
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
cc9deae
Preserve list type when overwriting existing elements via count()-1, …
phpstan-bot 31f0c2a
improve tests
staabm 8ee03f7
more tests
staabm 140d391
extract method
staabm 7126910
remove ai leftover
staabm 9a5f88d
same for sizeof
staabm 7c97da4
array_search() keeps list
staabm 39d803d
test huge list
staabm 5c070ed
test array_key_exists() keeps list
staabm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -53,6 +53,7 @@ | |
| use PHPStan\Type\Constant\ConstantStringType; | ||
| use PHPStan\Type\ConstantTypeHelper; | ||
| use PHPStan\Type\ErrorType; | ||
| use PHPStan\Type\IntegerRangeType; | ||
| use PHPStan\Type\MixedType; | ||
| use PHPStan\Type\ObjectType; | ||
| use PHPStan\Type\StaticTypeFactory; | ||
|
|
@@ -987,25 +988,11 @@ private function produceArrayDimFetchAssignValueToWrite(array $dimFetchStack, ar | |
| continue; | ||
| } | ||
|
|
||
| if (!$arrayDimFetch->dim instanceof Expr\BinaryOp\Plus) { | ||
| if (!$this->shouldKeepList($arrayDimFetch, $scope, $offsetValueType)) { | ||
| continue; | ||
| } | ||
|
|
||
| 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()); | ||
| } | ||
| $valueToWrite = TypeCombinator::intersect($valueToWrite, new AccessoryArrayListType()); | ||
| } | ||
|
|
||
| $additionalExpressions = []; | ||
|
|
@@ -1029,4 +1016,64 @@ private function produceArrayDimFetchAssignValueToWrite(array $dimFetchStack, ar | |
| return [$valueToWrite, $additionalExpressions]; | ||
| } | ||
|
|
||
| private function shouldKeepList(ArrayDimFetch $arrayDimFetch, Scope $scope, Type $offsetValueType): bool | ||
| { | ||
| if ($arrayDimFetch->dim instanceof Expr\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() | ||
| ) { | ||
| return true; | ||
| } 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() | ||
| ) { | ||
| return true; | ||
| } | ||
| } elseif ( // keep list for $list[count($list) - n] assignments | ||
| $arrayDimFetch->dim instanceof Expr\BinaryOp\Minus | ||
| && $arrayDimFetch->dim->right instanceof Node\Scalar\Int_ | ||
| && $arrayDimFetch->dim->left instanceof Expr\FuncCall | ||
| && $arrayDimFetch->dim->left->name instanceof Name | ||
| && in_array($arrayDimFetch->dim->left->name->toLowerString(), ['count', 'sizeof'], true) | ||
| && count($arrayDimFetch->dim->left->getArgs()) === 1 // could support COUNT_RECURSIVE, COUNT_NORMAL | ||
| && $this->isSameVariable($arrayDimFetch->var, $arrayDimFetch->dim->left->getArgs()[0]->value) | ||
| && IntegerRangeType::fromInterval(0, null)->isSuperTypeOf($scope->getType($arrayDimFetch->dim))->yes() | ||
| && $offsetValueType->isIterableAtLeastOnce()->yes() | ||
| ) { | ||
| return true; | ||
| } elseif ( // keep list for $list[array_key_last($list)] and $list[array_key_first($list)] assignments | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Array_search works too
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added 👍 |
||
| $arrayDimFetch->dim instanceof Expr\FuncCall | ||
| && $arrayDimFetch->dim->name instanceof Name | ||
| && in_array($arrayDimFetch->dim->name->toLowerString(), ['array_key_last', 'array_key_first'], true) | ||
| && count($arrayDimFetch->dim->getArgs()) >= 1 | ||
| && $this->isSameVariable($arrayDimFetch->var, $arrayDimFetch->dim->getArgs()[0]->value) | ||
| ) { | ||
| return true; | ||
| } elseif ( // keep list for $list[array_search($needle, $list)] assignments | ||
| $arrayDimFetch->dim instanceof Expr\FuncCall | ||
| && $arrayDimFetch->dim->name instanceof Name | ||
| && $arrayDimFetch->dim->name->toLowerString() === 'array_search' | ||
| && count($arrayDimFetch->dim->getArgs()) >= 1 | ||
| && $this->isSameVariable($arrayDimFetch->var, $arrayDimFetch->dim->getArgs()[1]->value) | ||
| ) { | ||
| return true; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| private function isSameVariable(Expr $a, Expr $b): bool | ||
| { | ||
| if ($a instanceof Variable && $b instanceof Variable && is_string($a->name) && is_string($b->name)) { | ||
| return $a->name === $b->name; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace Bug14245; | ||
|
|
||
| use function array_key_first; | ||
| use function array_key_last; | ||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| /** | ||
| * @return list<int> | ||
| */ | ||
| function foo(): array { | ||
| return []; | ||
| } | ||
|
|
||
| function doFoo(): void { | ||
| $list = foo(); | ||
| $count = count($list); | ||
| assertType('list<int>', $list); | ||
| if ($count > 0) { | ||
| assertType('non-empty-list<int>', $list); | ||
| $list[count($list) - 1] = 37; | ||
| assertType('non-empty-list<int>', $list); | ||
| } | ||
|
|
||
| assertType('list<int>', $list); | ||
| } | ||
|
|
||
| function doFoo2(): void { | ||
| $list = foo(); | ||
| $count = count($list); | ||
| assertType('list<int>', $list); | ||
| if ($count > 0) { | ||
| assertType('non-empty-list<int>', $list); | ||
| // we don't know the $list length, | ||
| // therefore count() - N might be before the first element -> degrade to array | ||
| $list[count($list) - 5] = 37; | ||
| assertType('non-empty-array<int<-4, max>, int>', $list); | ||
| } | ||
|
|
||
| assertType('array<int<-4, max>, int>', $list); | ||
| } | ||
|
|
||
| function listKnownSize(): void { | ||
| $list = foo(); | ||
| assertType('list<int>', $list); | ||
| if (count($list) === 5) { | ||
| assertType('array{int, int, int, int, int}', $list); | ||
| $list[count($list) - 3] = 37; | ||
| assertType('array{int, int, 37, int, int}', $list); | ||
| } | ||
|
|
||
| assertType('list<int>', $list); | ||
| } | ||
|
|
||
| function listKnownHugeSize(): void { | ||
| $list = foo(); | ||
| assertType('list<int>', $list); | ||
| if (count($list) === 50000) { | ||
| assertType('non-empty-list<int>', $list); | ||
| $list[count($list) - 3000] = 37; | ||
| assertType('non-empty-array<int<-2999, max>, int>', $list); | ||
| } | ||
|
|
||
| assertType('array<int<-2999, max>, int>', $list); | ||
| } | ||
|
|
||
| function overwriteKeyLast(): void { | ||
| $list = foo(); | ||
| $count = count($list); | ||
| assertType('list<int>', $list); | ||
| if ($count > 0) { | ||
| assertType('non-empty-list<int>', $list); | ||
| $list[array_key_last($list)] = 37; | ||
| assertType('non-empty-list<int>', $list); | ||
| } | ||
|
|
||
| assertType('list<int>', $list); | ||
| } | ||
|
|
||
| function overwriteKeyFirst(): void { | ||
| $list = foo(); | ||
| $count = count($list); | ||
| assertType('list<int>', $list); | ||
| if ($count > 0) { | ||
| assertType('non-empty-list<int>', $list); | ||
| $list[array_key_first($list)] = 37; | ||
| assertType('non-empty-list<int>', $list); | ||
| } | ||
|
|
||
| assertType('list<int>', $list); | ||
| } | ||
|
|
||
| function overwriteKeyFirstMaybeEmptyArray(): void { | ||
| $list = foo(); | ||
| assertType('list<int>', $list); | ||
| // empty list might return NULL for array_key_first() | ||
| $list[array_key_first($list)] = 37; | ||
| assertType('non-empty-list<int>', $list); | ||
| } | ||
|
|
||
| function keyDifferentArray(array $arr): void { | ||
| $list = foo(); | ||
| assertType('list<int>', $list); | ||
| $list[array_key_first($arr)] = 37; | ||
| assertType('non-empty-array<int|string, int>', $list); | ||
| } | ||
|
|
||
| function overwriteArraySearch($needle): void { | ||
| $list = foo(); | ||
|
|
||
| assertType('list<int>', $list); | ||
| // search in empty-array, or with a non-existent key will return false, | ||
| // which gets auto-casted to 0, so we still have a list | ||
| // https://3v4l.org/RZbOK | ||
| $list[array_search($needle, $list)] = 37; | ||
| assertType('non-empty-list<int>', $list); | ||
| } | ||
|
|
||
| function overwriteArraySearchStrict($needle): void { | ||
| $list = foo(); | ||
|
|
||
| assertType('list<int>', $list); | ||
| // search in empty-array, or with a non-existent key will return false, | ||
| // which gets auto-casted to 0, so we still have a list | ||
| // https://3v4l.org/RZbOK | ||
| $list[array_search($needle, $list, true)] = 37; | ||
| assertType('non-empty-list<int>', $list); | ||
| } | ||
|
|
||
| function ArraySearchWithDifferentArray($array2, $needle): void { | ||
| $list = foo(); | ||
|
|
||
| assertType('list<int>', $list); | ||
| $list[array_search($needle, $array2, true)] = 37; | ||
| assertType('non-empty-array<int|string, int>', $list); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cant we risk having n too big and set the offset -1 ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since the logic here will only add non-empty-list to a pre-existing maybe already degraded because too huge array, I don't see how a too big "n" value can be a problem here.
added a test with a huge array and a big offset