-
Notifications
You must be signed in to change notification settings - Fork 568
Fix phpstan/phpstan#8985: Expression result remembered on new() #5449
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
phpstan-bot
wants to merge
6
commits into
phpstan:2.1.x
Choose a base branch
from
phpstan-bot:create-pull-request/patch-r89jstk
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.
+136
−0
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5e2127c
Fix phpstan/phpstan#8985: Expression result remembered on new()
staabm d0d1966
Skip storing expression types with new in chain instead of skipping a…
phpstan-bot 36e3e69
more readable
staabm dbdb449
Fix expressionHasNewInChain crash on Name nodes, add tests for all ex…
phpstan-bot 654734a
Allow ClassConstFetch on new to be remembered, add tests for all expr…
phpstan-bot e7850f1
testClassConstFetchOnUnknownClass
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| <?php // lint >= 8.0 | ||
|
|
||
| declare(strict_types = 1); | ||
|
|
||
| namespace Bug8985; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| class Entity | ||
| { | ||
| public string $value; | ||
|
|
||
| public function __construct(string $value) | ||
| { | ||
| $this->value = $value; | ||
| } | ||
|
|
||
| public function getValue(): string | ||
| { | ||
| return $this->value; | ||
| } | ||
| } | ||
|
|
||
| class Repository | ||
| { | ||
| /** @return array<int, Entity> */ | ||
| public function getAll(): array | ||
| { | ||
| return [new Entity('test')]; | ||
| } | ||
|
|
||
| public string $name = 'default'; | ||
|
|
||
| /** @return array<int, Entity> */ | ||
| public static function staticGetAll(): array | ||
| { | ||
| return [new Entity('test')]; | ||
| } | ||
|
|
||
| public function getEntity(): Entity | ||
| { | ||
| return new Entity('test'); | ||
| } | ||
|
|
||
| public const MY_CONST = 'const_value'; | ||
| } | ||
|
|
||
| function testMethodCall(): void { | ||
| assert((new Repository())->getAll() === []); | ||
|
|
||
| $all = (new Repository())->getAll(); | ||
| assertType('array<int, Bug8985\Entity>', $all); | ||
| $value = $all[0]->getValue(); | ||
| } | ||
|
|
||
| function testNullsafeMethodCall(): void { | ||
| assert((new Repository())?->getEntity()?->getValue() === 'specific'); | ||
|
|
||
| assertType('string', (new Repository())?->getEntity()?->getValue()); | ||
| } | ||
|
|
||
| function testPropertyFetch(): void { | ||
| assert((new Repository())->name === 'foo'); | ||
|
|
||
| assertType('string', (new Repository())->name); | ||
| } | ||
|
|
||
| function testNullsafePropertyFetch(): void { | ||
| assert((new Repository())?->name === 'foo'); | ||
|
|
||
| assertType('string', (new Repository())?->name); | ||
| } | ||
|
|
||
| function testArrayDimFetch(): void { | ||
| assert((new Repository())->getAll()[0]->getValue() === 'specific'); | ||
|
|
||
| assertType('string', (new Repository())->getAll()[0]->getValue()); | ||
| } | ||
|
|
||
| function testStaticCall(): void { | ||
| assert((new Repository())::staticGetAll() === []); | ||
|
|
||
| assertType('array<int, Bug8985\Entity>', (new Repository())::staticGetAll()); | ||
| } | ||
|
|
||
| function testChainedMethodCalls(): void { | ||
| assert((new Repository())->getEntity()->getValue() === 'specific'); | ||
|
|
||
| assertType('string', (new Repository())->getEntity()->getValue()); | ||
| } | ||
|
|
||
| function testChainedPropertyOnMethodCall(): void { | ||
| assert((new Repository())->getEntity()->value === 'specific'); | ||
|
|
||
| assertType('string', (new Repository())->getEntity()->value); | ||
| } | ||
|
|
||
| function testClassConstFetch(): void { | ||
| assert((new Repository())::MY_CONST === 'const_value'); | ||
|
|
||
| assertType("'const_value'", (new Repository())::MY_CONST); | ||
| } | ||
|
|
||
| function testClassConstFetchOnUnknownClass(string $class, string $anotherClass): void { | ||
| assert((new $class())::MY_CONST === 'const_value'); | ||
|
|
||
| assertType("'const_value'", (new $class())::MY_CONST); | ||
|
|
||
| $class = $anotherClass; | ||
| assertType("*ERROR*", (new $class())::MY_CONST); | ||
| } |
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.
add more tests to cover the relevant expression types
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.
Done. Here's a summary of the changes:
Bug fix (
src/Analyser/MutatingScope.php):expressionHasNewInChain()would crash with aTypeErrorwhen$expr->classwas aNamenode (e.g.,Foo::bar()) instead of anExprnode. Added an$expr->class instanceof Exprguard before the recursive call on line 1007.Tests (
tests/PHPStan/Analyser/nsrt/bug-8985.php):Added test functions covering all expression types handled by
expressionHasNewInChain():testMethodCall-(new Foo())->method()(existing, reorganized)testNullsafeMethodCall-(new Foo())?->method()testPropertyFetch-(new Foo())->proptestNullsafePropertyFetch-(new Foo())?->proptestArrayDimFetch-(new Foo())->getArray()[0]->method()testStaticCall-(new Foo())::staticMethod()testChainedMethodCalls-(new Foo())->getEntity()->getValue()testChainedPropertyOnMethodCall-(new Foo())->getEntity()->valueAll 11853 tests pass, PHPStan reports no errors.