-
Notifications
You must be signed in to change notification settings - Fork 547
Understand always-overwritten arrays in foreach #4534
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
Changes from all commits
Commits
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
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
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,37 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Node\Expr; | ||
|
|
||
| use Override; | ||
| use PhpParser\Node\Expr; | ||
| use PHPStan\Node\VirtualNode; | ||
|
|
||
| final class OriginalForeachKeyExpr extends Expr implements VirtualNode | ||
| { | ||
|
|
||
| public function __construct(private string $variableName) | ||
| { | ||
| parent::__construct([]); | ||
| } | ||
|
|
||
| public function getVariableName(): string | ||
| { | ||
| return $this->variableName; | ||
| } | ||
|
|
||
| #[Override] | ||
| public function getType(): string | ||
| { | ||
| return 'PHPStan_Node_OriginalForeachKeyExpr'; | ||
| } | ||
|
|
||
| /** | ||
| * @return string[] | ||
| */ | ||
| #[Override] | ||
| public function getSubNodeNames(): array | ||
| { | ||
| return []; | ||
| } | ||
|
|
||
| } |
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
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
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,24 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace Bug13730; | ||
|
|
||
| use function PHPStan\dumpType; | ||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| class HelloWorld | ||
| { | ||
| /** | ||
| * @param array<string|null> $arr | ||
| * @return array<string> | ||
| */ | ||
| public function sayHello(array $arr): array | ||
| { | ||
| foreach($arr as $k => $v) { | ||
| $arr[$k] = $v ?? ''; | ||
| } | ||
|
|
||
| assertType('array<string>', $arr); | ||
|
|
||
| return $arr; | ||
| } | ||
| } |
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,22 @@ | ||
| <?php | ||
|
|
||
| namespace Bug2273; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| class Foo { | ||
| /** | ||
| * @param string[] $x | ||
| */ | ||
| public function doFoo(array $x): void | ||
| { | ||
| foreach ($x as $k => $v) { | ||
| $x[$k] = \realpath($v); | ||
| if ($x[$k] === false) { | ||
| throw new \Exception(); | ||
| } | ||
| } | ||
|
|
||
| assertType('array<non-empty-string>', $x); | ||
| } | ||
| } |
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,131 @@ | ||
| <?php | ||
|
|
||
| namespace OverwrittenArrays; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
| use function rad2deg; | ||
|
|
||
| class Foo | ||
| { | ||
|
|
||
| /** | ||
| * @param array<int, string> $a | ||
| */ | ||
| public function doFoo(array $a): void | ||
| { | ||
| foreach ($a as $k => $v) { | ||
| $a[$k] = 1; | ||
| } | ||
|
|
||
| assertType('array<int, 1>', $a); | ||
| } | ||
|
|
||
| /** | ||
| * @param array<int, string> $a | ||
| */ | ||
| public function doFoo2(array $a): void | ||
| { | ||
| foreach ($a as $k => $v) { | ||
| if (rand(0, 1)) { | ||
| $a[$k] = 2; | ||
| continue; | ||
| } | ||
| $a[$k] = 1; | ||
| } | ||
|
|
||
| assertType('array<int, 1|2>', $a); | ||
| } | ||
|
|
||
| /** | ||
| * @param array<int, string> $a | ||
| */ | ||
| public function doFoo3(array $a): void | ||
| { | ||
| foreach ($a as $k => $v) { | ||
| if (rand(0, 1)) { | ||
| break; | ||
| } | ||
| if (rand(0, 1)) { | ||
| $a[$k] = 2; | ||
| continue; | ||
| } | ||
| $a[$k] = 1; | ||
| } | ||
|
|
||
| assertType('array<int, 1|2|string>', $a); | ||
| } | ||
|
|
||
| /** | ||
| * @param array<int, string> $a | ||
| */ | ||
| public function doFoo4(array $a): void | ||
| { | ||
| foreach ($a as $k => $v) { | ||
| $k++; | ||
| $a[$k] = 1; | ||
| } | ||
|
|
||
| assertType('array<int, 1|string>', $a); | ||
| } | ||
|
|
||
| /** | ||
| * @param array<int, string> $a | ||
| */ | ||
| public function doFoo5(array $a): void | ||
| { | ||
| foreach ($a as $k => $v) { | ||
| if (rand(0, 1)) { | ||
| $k++; | ||
| $a[$k] = 2; | ||
| continue; | ||
| } | ||
| $a[$k] = 1; | ||
| } | ||
|
|
||
| assertType('array<int, 1|2|string>', $a); | ||
| } | ||
|
|
||
| /** | ||
| * @param array<int, string> $a | ||
| */ | ||
| public function doFoo6(array $a): void | ||
| { | ||
| foreach ($a as $k => $v) { | ||
| if (rand(0, 1)) { | ||
| $a[$k] = 2; | ||
| continue; | ||
| } | ||
| $k++; | ||
| $a[$k] = 1; | ||
| } | ||
|
|
||
| assertType('array<int, 1|2|string>', $a); | ||
| } | ||
|
|
||
| /** | ||
| * @param array<int, string> $a | ||
| */ | ||
| public function doFoo7(array $a): void | ||
| { | ||
| foreach ($a as &$v) { | ||
| $v = 1; | ||
| } | ||
|
|
||
| assertType('array<int, 1>', $a); | ||
| } | ||
|
|
||
| /** | ||
| * @param array<int, string> $a | ||
| */ | ||
| public function doFoo8(array $a): void | ||
| { | ||
| foreach ($a as &$v) { | ||
| if (rand(0, 1)) { | ||
| $v = 1; | ||
| } | ||
| } | ||
|
|
||
| assertType('array<int, 1|string>', $a); | ||
| } | ||
|
|
||
| } |
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.
tbh I did not yet understand why this PR works, but I wonder whether we could use the same logic to also infer a better result when the iteratee is a known constant array?
like in
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.
Why this works - I look at
$a[$k]type at the end of the loop and at eachcontinuepoint and if some other conditions are true (like there are zerobreakpoints), I rewrite the whole array value type with the$a[$k]type unioned from all the points I looked at.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.
When
$foreach->expris a constant array I feel like we need a different approach, possibly analysing the foreach separately for each item (but run$nodeCallbackfor the rules still only once).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.
Also I want to look at
foreach ($a as &$v)to understand it better that also needs a different approach.