-
Notifications
You must be signed in to change notification settings - Fork 568
Fix isset() falsey branch not narrowing array type for dim fetches #4983
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
7 commits
Select commit
Hold shift + click to select a range
3b14a35
Fix isset() falsey branch not narrowing array type for dim fetches
phpstan-bot fed2702
More tests
VincentLanglet 432d18a
Simplify
VincentLanglet 24c603c
Add regression test for #10544
phpstan-bot cf3bf63
Add regression test for #8724
phpstan-bot c5a4140
Add regression test for #5128
phpstan-bot 06ae0bd
Add regression test for #12401
phpstan-bot 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,19 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace Bug10544; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| class Boo { | ||
| /** | ||
| * @param array{check1:bool,boo:string}|array{check2:bool,foo:string} $param | ||
| */ | ||
| public function foo(array $param): string { | ||
| if (isset($param['check1'])) { | ||
| assertType('array{check1: bool, boo: string}', $param); | ||
| return $param['boo']; | ||
| } | ||
| assertType('array{check2: bool, foo: string}', $param); | ||
| return $param['foo']; | ||
| } | ||
| } |
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 Bug12401; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| /** | ||
| * @param array{a: string, b: string}|array{c: string} $data | ||
| */ | ||
| function test(array $data): void { | ||
| if (isset($data['a'])) { | ||
| assertType('array{a: string, b: string}', $data); | ||
| } else { | ||
| assertType('array{c: string}', $data); | ||
| } | ||
|
|
||
| if (isset($data['a'])) { | ||
| assertType('array{a: string, b: string}', $data); | ||
| } elseif (isset($data['c'])) { | ||
| assertType('array{c: string}', $data); | ||
| } else { | ||
| assertType('*NEVER*', $data); | ||
| } | ||
| } |
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,18 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace Bug5128; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| /** | ||
| * @param array{a: string}|array{b: string} $array | ||
| */ | ||
| function a(array $array): string { | ||
| if (isset($array['a'])) { | ||
| assertType('array{a: string}', $array); | ||
| return $array['a']; | ||
| } | ||
|
|
||
| assertType('array{b: string}', $array); | ||
| return $array['b']; | ||
| } |
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 declare(strict_types = 1); | ||
|
|
||
| namespace Bug8724; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| /** | ||
| * @phpstan-type NumberFilterStructure array{ | ||
| * type: 'equals'|'notEqual'|'lessThan'|'lessThanOrEqual'|'greaterThan'|'greaterThanOrEqual'|'inRange'|'blank'|'notBlank', | ||
| * } | ||
| * @phpstan-type CombinedNumberFilterStructure array{ | ||
| * operator: 'AND'|'OR' | ||
| * } | ||
| */ | ||
| class HelloWorld | ||
| { | ||
| /** @param NumberFilterStructure|CombinedNumberFilterStructure $filter */ | ||
| public function test(array $filter): void | ||
| { | ||
| if (isset($filter['operator'])) { | ||
| assertType("array{operator: 'AND'|'OR'}", $filter); | ||
| return; | ||
| } | ||
|
|
||
| assertType("array{type: 'blank'|'equals'|'greaterThan'|'greaterThanOrEqual'|'inRange'|'lessThan'|'lessThanOrEqual'|'notBlank'|'notEqual'}", $filter); | ||
| echo $filter['type']; | ||
| } | ||
| } |
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,57 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace Bug9908; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| class HelloWorld | ||
| { | ||
| public function test(): void | ||
| { | ||
| $a = []; | ||
| if (rand() % 2) { | ||
| $a = ['bar' => 'string']; | ||
| } | ||
|
|
||
| if (isset($a['bar'])) { | ||
| $a['bar'] = 1; | ||
| } | ||
|
|
||
| assertType("array{}|array{bar: 1}", $a); | ||
| } | ||
|
|
||
| /** | ||
| * @param array{bar?: int} $foo | ||
| */ | ||
| public function sayHello(array $foo): void | ||
| { | ||
| echo 'Hello' . print_r($foo, true); | ||
| } | ||
|
|
||
| public function test2(): void | ||
| { | ||
| $a = []; | ||
| if (rand() % 2) { | ||
| $a = ['bar' => 'string']; | ||
| } | ||
|
|
||
| if (isset($a['bar'])) { | ||
| $a['bar'] = 1; | ||
| } | ||
|
|
||
| $this->sayHello($a); | ||
| } | ||
|
|
||
| /** | ||
| * @param array{a: string, b: string}|array{a: string|null, c: string}|array{a?: string, d: string} $a | ||
| */ | ||
| public function moreTests($a): void | ||
| { | ||
| if (isset($a['a'])) { | ||
| assertType("array{a: string, b: string}|array{a: string, c: string}|array{a: string, d: string}", $a); | ||
| } else { | ||
| // Could be "array{a: null, c: string}|array{d: string}" | ||
| assertType("array{a: string|null, c: string}|array{a?: string, d: string}", $a); | ||
| } | ||
| } | ||
| } |
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
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.
Uh oh!
There was an error while loading. Please reload this page.