-
Notifications
You must be signed in to change notification settings - Fork 554
Fix phpstan/phpstan#12244: $this(Type~Subtracted) does not work on conditional return type as Type~Subtracted
#5104
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| <?php // lint >= 8.1 | ||
|
|
||
| declare(strict_types = 1); | ||
|
|
||
| namespace Bug12244; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| enum X: string { | ||
| case A = 'a'; | ||
| case B = 'b'; | ||
| case C = 'c'; | ||
|
|
||
| /** @return ($this is self::A ? int : null) */ | ||
| public function get(): ?int { | ||
| return ($this === self::A) ? 123 : null; | ||
| } | ||
|
|
||
| public function doSomething(): void { | ||
| if ($this !== self::A) { | ||
|
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. would it make sense to test a
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 |
||
| assertType('$this(Bug12244\X~Bug12244\X::A)', $this); | ||
| assertType('null', $this->get()); | ||
| } else { | ||
| assertType('int', $this->get()); | ||
| } | ||
|
|
||
| if ($this !== self::B && $this !== self::C) { | ||
| assertType('int', $this->get()); | ||
| } | ||
|
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. add else branch with assertions for all the expressions tested in the if-branch?
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 |
||
| } | ||
|
|
||
| public static function doSomethingFor(X $x): void { | ||
| if ($x !== self::A) { | ||
| assertType('Bug12244\X~Bug12244\X::A', $x); | ||
| assertType('null', $x->get()); | ||
| } else { | ||
| assertType('int', $x->get()); | ||
| } | ||
|
|
||
| if ($x !== self::B && $x !== self::C) { | ||
| assertType('int', $x->get()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| enum Y: string { | ||
| case A = 'a'; | ||
| case B = 'b'; | ||
| case C = 'c'; | ||
|
|
||
| /** @param-out ($this is self::A ? int : null) $i */ | ||
| public function get(?int &$i): void { | ||
| ($this === self::A) ? $i=null : $i=123; | ||
| } | ||
|
|
||
| public function doSomething(): void { | ||
| $i = 0; | ||
| if ($this !== self::A) { | ||
| $this->get($i); | ||
| assertType('null', $i); // null | ||
| } else { | ||
| $this->get($i); | ||
| assertType('int', $i); // int | ||
| } | ||
| } | ||
|
|
||
| public static function doSomethingFor(Y $x): void { | ||
| $i = 0; | ||
| if ($x !== self::A) { | ||
| $x->get($i); | ||
| assertType('null', $i); // null | ||
| } else { | ||
| $x->get($i); | ||
| assertType('int', $i); // int | ||
| } | ||
| } | ||
| } | ||
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.
will this also make a difference for param-out?
https://phpstan.org/r/e53128e5-21a6-4b35-88ba-868a23a28dca
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.
Yes
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.
Cool - please add a test
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.
Added