-
Notifications
You must be signed in to change notification settings - Fork 545
array_key_exist inference is lost when adding a false condition
#4473
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 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8c85a32
array_key_exist inference is lost when adding a false condition
staabm 6cf3962
add more tests
staabm 74f27ca
fix
staabm 6cdeb45
fix php7
staabm d02bcc3
fix build
staabm fbb0018
support truethy/falsey
staabm 9d112f2
Update BooleanOrConstantConditionRuleTest.php
staabm 602fc71
give infection more time to run the tests
staabm 2080517
Update tests.yml
staabm d63bcba
Discard changes to .github/workflows/tests.yml
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
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,43 @@ | ||
| <?php | ||
|
|
||
| namespace Bug11276; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| function doFoo(int $i, int $j, $arr): void | ||
| { | ||
| if (false || array_key_exists($i, $arr)) { | ||
| assertType('non-empty-array', $arr); | ||
| } | ||
|
|
||
| if (array_key_exists($i, $arr) || false) { | ||
| assertType('non-empty-array', $arr); | ||
| } | ||
|
|
||
| if (true || array_key_exists($i, $arr)) { | ||
| assertType('mixed', $arr); | ||
| } | ||
|
|
||
| if (array_key_exists($i, $arr) || true) { | ||
| assertType('mixed', $arr); | ||
| } | ||
|
|
||
| if (array_key_exists($i, $arr) || array_key_exists($j, $arr)) { | ||
| assertType('non-empty-array', $arr); | ||
| } | ||
|
|
||
| if (!array_key_exists($j, $arr)) { | ||
| if (array_key_exists($i, $arr) || array_key_exists($j, $arr)) { | ||
| assertType('non-empty-array', $arr); | ||
| } | ||
| } | ||
| if (!array_key_exists($i, $arr)) { | ||
| if (array_key_exists($i, $arr) || array_key_exists($j, $arr)) { | ||
| assertType('non-empty-array', $arr); | ||
| } | ||
| } | ||
|
|
||
| if (array_key_exists($i, $arr)) { | ||
| assertType('non-empty-array', $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,20 @@ | ||
| <?php // lint >= 8.0 | ||
|
|
||
| namespace Bug11276b; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| function doBar($j, array $arr) { | ||
| $i = 1; | ||
| if (!array_key_exists($i, $arr)) { | ||
| if (array_key_exists($i, $arr) || array_key_exists($j, $arr)) { | ||
| assertType('non-empty-array<mixed~1, mixed>', $arr); | ||
| } | ||
| } | ||
|
|
||
| if (!array_key_exists($i, $arr)) { | ||
| if (array_key_exists($j, $arr) || array_key_exists($i, $arr)) { | ||
| assertType('non-empty-array<mixed~1, mixed>', $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
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,26 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace Bug11276; | ||
|
|
||
| class HelloWorld | ||
| { | ||
| /** | ||
| * @param array{from: string, to: string} $expected | ||
| */ | ||
| public function testLanguagesMatchingRegex(string $url, ?array $expected): void | ||
| { | ||
| preg_match('#\/(?<from>[a-z]{2})-(?<to>[a-z]{1}[a-z0-9]{1})\/#', $url, $matches); | ||
|
|
||
| foreach ($expected as $key => $value) { | ||
| if ($matches instanceof ArrayAccess || \array_key_exists($key, $matches)) { | ||
| $matches[$key]; | ||
| } | ||
| } | ||
|
|
||
| foreach ($expected as $key => $value) { | ||
| if (\array_key_exists($key, $matches) || $matches instanceof ArrayAccess) { | ||
| $matches[$key]; | ||
| } | ||
| } | ||
| } | ||
| } |
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.
My head hurts 😅
I think all the
isTrue/isFalsecalls should dotoBoolean()beforehand.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.
great catch. fixed.
thank you.
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.
hmm maybe we should have a
$type->isFalse()->yes()/$type->isTrue()->yes()to$type->toBoolean()->isFalse()->yes()/$type->toBoolean()->isTrue()->yes()mutator.it would make us aware of false/falsey coverage