-
Notifications
You must be signed in to change notification settings - Fork 529
Fix chaining with static
/self
and $this
#3363
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
Closed
Closed
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
namespace StaticWithThisChained; | ||
|
||
use function PHPStan\Testing\assertType; | ||
|
||
class Foo | ||
{ | ||
/** @var $this */ | ||
public $propThis; | ||
|
||
/** @var static */ | ||
public $propStatic; | ||
|
||
/** @var static */ | ||
public static $propStaticStatic; | ||
|
||
/** | ||
* @return $this | ||
*/ | ||
public function getThis() | ||
{ | ||
return $this; | ||
} | ||
|
||
/** | ||
* @return static | ||
*/ | ||
public function newStatic() | ||
{ | ||
return new static(); | ||
} | ||
|
||
/** | ||
* @return static | ||
*/ | ||
public static function newStaticStatic() | ||
{ | ||
return new static(); | ||
} | ||
|
||
public function testMethodChained(): void | ||
{ | ||
assertType('$this(StaticWithThisChained\Foo)', $this->getThis()); | ||
assertType('static(StaticWithThisChained\Foo)', $this->newStatic()); | ||
assertType('static(StaticWithThisChained\Foo)', static::newStaticStatic()); | ||
assertType('StaticWithThisChained\Foo', self::newStaticStatic()); | ||
assertType('StaticWithThisChained\Foo', Foo::newStaticStatic()); | ||
|
||
assertType('$this(StaticWithThisChained\Foo)', $this->getThis()->getThis()); | ||
assertType('static(StaticWithThisChained\Foo)', $this->newStatic()->getThis()); | ||
assertType('static(StaticWithThisChained\Foo)', static::newStaticStatic()->getThis()); | ||
assertType('StaticWithThisChained\Foo', self::newStaticStatic()->getThis()); | ||
assertType('StaticWithThisChained\Foo', Foo::newStaticStatic()->getThis()); | ||
} | ||
|
||
public function testPropertyChained(): void | ||
{ | ||
/* | ||
assertType('$this(StaticWithThisChained\Foo)', $this->propThis); | ||
assertType('static(StaticWithThisChained\Foo)', $this->propStatic); | ||
assertType('static(StaticWithThisChained\Foo)', static::$propStaticStatic); | ||
assertType('StaticWithThisChained\Foo', self::$propStaticStatic); | ||
assertType('StaticWithThisChained\Foo', Foo::$propStaticStatic); | ||
|
||
assertType('$this(StaticWithThisChained\Foo)', $this->propThis->propThis); | ||
assertType('static(StaticWithThisChained\Foo)', $this->propStatic->propThis); | ||
assertType('static(StaticWithThisChained\Foo)', static::$propStaticStatic->propThis); | ||
assertType('StaticWithThisChained\Foo', self::$propStaticStatic->propThis); | ||
assertType('StaticWithThisChained\Foo', Foo::$propStaticStatic->propThis); | ||
*/ | ||
} | ||
} |
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.
Help needed - how can I mutate the scope to be static when
static
return type is analysed?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.
@ondrejmirtes please see https://phpstan.org/r/29fcfdb5-f19e-4df2-9177-61ad88490c64 repro - do you know where the issue comes from and how it can be fixed? (
static
must never be converted to$this
)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.
@staabm might I ask you for a help? I have looked into the fix several times, but I am not familiar with mutating the scope logic and I do not know how to fix this myself.