-
Notifications
You must be signed in to change notification settings - Fork 529
Fix incorrect type inference for @phpstan-assert-if-true on $this with union types #4246
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
Open
yt-catpaw
wants to merge
5
commits into
phpstan:2.1.x
Choose a base branch
from
yt-catpaw:bugfix/13358-assert-if-true-this
base: 2.1.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ab74f12
test: reproduce #13358 (assert-if-true on $this lost via union)
potet 1f1e916
fix(reflection): merge assertions in UnionTypeMethodReflection::getAs…
potet 31b55ac
Move bug-13358 test to nsrt/ and remove obsolete test class
potet 6ca93cf
Add regression test for #11441 (assert on UnionType)
yt-catpaw 65ac9e0
Consistent variable name in getAsserts()
yt-catpaw 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,39 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Bug11441; | ||
|
||
use function PHPStan\Testing\assertType; | ||
|
||
class Foo { | ||
public function getParam(): ?string { | ||
return 'foo'; | ||
} | ||
|
||
/** @phpstan-assert !null $this->getParam() */ | ||
public function checkNotNull(): void { | ||
if ($this->getParam() === null) { | ||
throw new \Exception(); | ||
} | ||
} | ||
} | ||
|
||
class Bar { | ||
public function getParam(): ?int { | ||
return 1; | ||
} | ||
|
||
/** @phpstan-assert !null $this->getParam() */ | ||
public function checkNotNull(): void { | ||
if ($this->getParam() === null) { | ||
throw new \Exception(); | ||
} | ||
} | ||
} | ||
|
||
function test(Foo|Bar $obj): void { | ||
assertType('int|string|null', $obj->getParam()); | ||
|
||
$obj->checkNotNull(); | ||
|
||
assertType('int|string', $obj->getParam()); | ||
} |
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,40 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Bug13358; | ||
|
||
use function PHPStan\Testing\assertType; | ||
|
||
/** | ||
* @phpstan-sealed SystemActor|AnonymousVisitorActor | ||
*/ | ||
abstract class Actor | ||
{ | ||
/** | ||
* @phpstan-assert-if-true SystemActor $this | ||
*/ | ||
public function isSystem(): bool | ||
{ | ||
return $this instanceof SystemActor; | ||
} | ||
|
||
/** | ||
* @phpstan-assert-if-true AnonymousVisitorActor $this | ||
*/ | ||
public function isAnonymousVisitor(): bool | ||
{ | ||
return $this instanceof AnonymousVisitorActor; | ||
} | ||
} | ||
|
||
class SystemActor extends Actor {} | ||
class AnonymousVisitorActor extends Actor {} | ||
|
||
function test(AnonymousVisitorActor|SystemActor $actor): void { | ||
assertType('Bug13358\AnonymousVisitorActor|Bug13358\SystemActor', $actor); | ||
|
||
if ($actor->isSystem()) { | ||
assertType('Bug13358\SystemActor', $actor); | ||
} else { | ||
assertType('Bug13358\AnonymousVisitorActor', $actor); | ||
} | ||
} |
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.
This will merge assertions from both types. Which is a wrong operation. (The method has a misleading name but it's used in IntersectionTypeMethodReflection where it's appropriate.)
In a union type what needs to be done is we need to look at assertions from all places and do some kind of intersection, figure out the common assertions.
Let's say we have
Foo|Bar
and we call a method that's on both types.One method does some kind of assertion and the other method does a different assertion. The correct performed assertion on the union is none because they have nothing in common.
So we need to look at on which expression the assertion is performed (in this case
$this->getParam()
) and if all involved methods on the union are doing an assertion on the same expression.Then we need to take a look at the asserted type and also take it into account only if it's the same. (It's possible that they don't have to be the same but we'd have to figure out the right operation that we can be sure about.)