-
Notifications
You must be signed in to change notification settings - Fork 540
Fix incorrect analyzing of array_shift with non-empty-list property #4294
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
Show all changes
12 commits
Select commit
Hold shift + click to select a range
aa8d779
Fix incorrect analyzing of array_shift with non-empty-list property
staabm 9d2f659
fix array_push()/array_pop()
staabm 4998c74
fix more functions
staabm f37c06c
added regression test
staabm 4fe77cf
fix php 7.4 lint
staabm cf9bf2c
use MutatingScope->processAssignVar() instead
staabm b872a73
Added regression test
staabm 15bb2f7
Added regression test
staabm de0b94b
Added regression test
staabm 15e7f3a
fix php 7.4 build
staabm 6d77e92
Create bug-11846.php
staabm 4702c2b
use newType() as expr
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
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,28 @@ | ||
<?php | ||
|
||
namespace Bug11846; | ||
|
||
use function PHPStan\Testing\assertType; | ||
|
||
function demo(): void | ||
{ | ||
$outerList = []; | ||
$idList = [1, 2]; | ||
|
||
foreach ($idList as $id) { | ||
$outerList[$id] = []; | ||
array_push($outerList[$id], []); | ||
} | ||
assertType('non-empty-array<1|2, array{}|array{array{}}>', $outerList); | ||
|
||
foreach ($outerList as $key => $outerElement) { | ||
$result = false; | ||
|
||
assertType('array{}|array{array{}}', $outerElement); | ||
foreach ($outerElement as $innerElement) { | ||
$result = true; | ||
} | ||
assertType('bool', $result); // could be 'true' | ||
|
||
} | ||
} |
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
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,29 @@ | ||
<?php | ||
|
||
namespace Bug2457; | ||
|
||
class HelloWorld | ||
{ | ||
public function sayHello(array $x, array $y): void | ||
{ | ||
$a = []; | ||
$o = []; | ||
|
||
foreach ($x as $t) { | ||
if (!isset($a[$t])) { | ||
$a[$t] = []; | ||
} | ||
$o[] = 'x'; | ||
array_unshift($a[$t], count($o) - 1); | ||
} | ||
|
||
foreach ($y as $t) { | ||
if (!isset($a[$t])) { | ||
continue; | ||
} | ||
foreach ($a[$t] as $b) { | ||
// this will be reached | ||
} | ||
} | ||
} | ||
} |
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,20 @@ | ||
<?php | ||
|
||
namespace Bug2560; | ||
|
||
class HelloWorld | ||
{ | ||
public function test(): void | ||
{ | ||
$arr = []; | ||
foreach ([0,1] as $i) { | ||
$arr[$i] = []; | ||
array_push($arr[$i], "foo"); | ||
} | ||
foreach (array_values($arr) as $vec) { | ||
foreach ($vec as $value) { | ||
print_r("$value"); | ||
} | ||
} | ||
} | ||
} |
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,33 @@ | ||
<?php | ||
|
||
namespace Bug3387; | ||
|
||
function (array $items, string $key) { | ||
$array = [$key => []]; | ||
foreach ($items as $item) { | ||
$array[$key][] = $item; | ||
if (count($array[$key]) > 1) { | ||
throw new RuntimeException(); | ||
} | ||
} | ||
}; | ||
|
||
function (array $items, string $key) { | ||
$array = [$key => []]; | ||
foreach ($items as $item) { | ||
array_unshift($array[$key], $item); | ||
if (count($array[$key]) > 1) { | ||
throw new RuntimeException(); | ||
} | ||
} | ||
}; | ||
|
||
function (array $items, string $key) { | ||
$array = [$key => []]; | ||
foreach ($items as $item) { | ||
array_push($array[$key], $item); | ||
if (count($array[$key]) > 1) { | ||
throw new RuntimeException(); | ||
} | ||
} | ||
}; |
Oops, something went wrong.
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.
I have also tried removing aboves
$scope->invalidateExpression($arrayArg)->assignExpression($arrayArg, ...)
but this leads to different native vs. phpdoc type resultsThere 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.
I'm fine with that as a temporary step.
Maybe we can experiment with a new
TypeExpr
-like class in another PR that would carry$type
and$nativeType
- two constructor arguments. A bunch of places that currently donew TypeExpr
might get better behaviour from that.Uh oh!
There was an error while loading. Please reload this page.
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.
after doing #4294 (comment) we get the same type changes, as I saw with the above suggestion.
so I went ahead and did this simplification in a53b7a9
if you are not fine with it, I can revert the changes (but it does not affect results of tests)
agree 👍
Uh oh!
There was an error while loading. Please reload this page.
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.
I don't want this simplification. We'd have to put the code back anyway after we do the "TypeExpr with native type" again. Please revert it.