-
Notifications
You must be signed in to change notification settings - Fork 576
Fix internal error when using first class callable with NullsafeMethodCall #5888
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
15
commits into
phpstan:2.2.x
Choose a base branch
from
phpstan-bot:create-pull-request/patch-3pnp41f
base: 2.2.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
15 commits
Select commit
Hold shift + click to select a range
da17bb9
Treat first-class callable `NullsafeMethodCall` as a `MethodCall` ins…
staabm a2149b4
Add AnalyserIntegrationTest for first-class callable NullsafeMethodCa…
phpstan-bot 3c8c16a
Update bug-9746.php
staabm 35812c7
Delete tests/PHPStan/Analyser/nsrt/bug-9746.php
staabm d31bdac
Report "Cannot combine nullsafe operator with Closure creation" for f…
phpstan-bot acc8721
Carry first-class callable nullsafe info in MethodCallableNode's orig…
phpstan-bot b3ee78e
assert based on original-node
staabm d2288b9
Introduce NullsafeFirstClassCallableNode for $foo?->bar(...)
phpstan-bot daa1ab4
Test NullsafeFirstClassCallableRule
phpstan-bot 8f7ddea
simplify
staabm 3e6e7d4
Delete NullsafeFirstClassCallableNodeHandler.php
staabm 419910b
Update DependencyResolver.php
staabm ef3a400
Update DependencyResolver.php
staabm 6564c7b
more precise name
staabm 3bf98fb
Test dynamic first-class callable nullsafe method call
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Node; | ||
|
|
||
| use Override; | ||
| use PhpParser\Node\Expr; | ||
| use PhpParser\Node\Identifier; | ||
|
|
||
| /** | ||
| * Represents `$foo?->bar(...)` - combining the nullsafe operator with the | ||
| * first-class callable syntax. This is a fatal error in PHP ("Cannot combine | ||
| * nullsafe operator with Closure creation"), reported by NullsafeFirstClassCallableRule. | ||
| * | ||
| * @api | ||
| */ | ||
| final class NullsafeMethodCallOnFirstClassCallableNode extends Expr implements VirtualNode | ||
| { | ||
|
|
||
| public function __construct( | ||
| private Expr $var, | ||
| private Identifier|Expr $name, | ||
| private Expr\NullsafeMethodCall $originalNode, | ||
| ) | ||
| { | ||
| parent::__construct($originalNode->getAttributes()); | ||
| } | ||
|
|
||
| public function getVar(): Expr | ||
| { | ||
| return $this->var; | ||
| } | ||
|
|
||
| /** | ||
| * @return Expr|Identifier | ||
| */ | ||
| public function getName() | ||
| { | ||
| return $this->name; | ||
| } | ||
|
|
||
| public function getOriginalNode(): Expr\NullsafeMethodCall | ||
| { | ||
| return $this->originalNode; | ||
| } | ||
|
|
||
| #[Override] | ||
| public function getType(): string | ||
| { | ||
| return 'PHPStan_Node_NullsafeFirstClassCallableNode'; | ||
| } | ||
|
|
||
| /** | ||
| * @return string[] | ||
| */ | ||
| #[Override] | ||
| public function getSubNodeNames(): array | ||
| { | ||
| return []; | ||
| } | ||
|
|
||
| } |
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
34 changes: 34 additions & 0 deletions
34
src/Rules/Methods/NullsafeMethodCallOnFirstClassCallableRule.php
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,34 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Rules\Methods; | ||
|
|
||
| use PhpParser\Node; | ||
| use PHPStan\Analyser\Scope; | ||
| use PHPStan\DependencyInjection\RegisteredRule; | ||
| use PHPStan\Node\NullsafeMethodCallOnFirstClassCallableNode; | ||
| use PHPStan\Rules\Rule; | ||
| use PHPStan\Rules\RuleErrorBuilder; | ||
|
|
||
| /** | ||
| * @implements Rule<NullsafeMethodCallOnFirstClassCallableNode> | ||
| */ | ||
| #[RegisteredRule(level: 0)] | ||
| final class NullsafeMethodCallOnFirstClassCallableRule implements Rule | ||
| { | ||
|
|
||
| public function getNodeType(): string | ||
| { | ||
| return NullsafeMethodCallOnFirstClassCallableNode::class; | ||
| } | ||
|
|
||
| public function processNode(Node $node, Scope $scope): array | ||
| { | ||
| return [ | ||
| RuleErrorBuilder::message('Cannot combine nullsafe operator with Closure creation.') | ||
| ->nonIgnorable() | ||
| ->identifier('nullsafe.firstClassCallable') | ||
| ->build(), | ||
| ]; | ||
| } | ||
|
|
||
| } |
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,13 @@ | ||
| <?php // lint >= 8.1 | ||
|
|
||
| declare(strict_types = 1); | ||
|
|
||
| namespace Bug9746; | ||
|
|
||
| class HelloWorld | ||
| { | ||
| public function sayHello(?self $self): void | ||
| { | ||
| $self?->sayHello(...); | ||
| } | ||
| } | ||
39 changes: 39 additions & 0 deletions
39
tests/PHPStan/Rules/Methods/NullsafeMethodCallOnFirstClassCallableRuleTest.php
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 PHPStan\Rules\Methods; | ||
|
|
||
| use PHPStan\Rules\Rule; | ||
| use PHPStan\Testing\RuleTestCase; | ||
| use PHPUnit\Framework\Attributes\RequiresPhp; | ||
|
|
||
| /** | ||
| * @extends RuleTestCase<NullsafeMethodCallOnFirstClassCallableRule> | ||
| */ | ||
| class NullsafeMethodCallOnFirstClassCallableRuleTest extends RuleTestCase | ||
| { | ||
|
|
||
| protected function getRule(): Rule | ||
| { | ||
| return new NullsafeMethodCallOnFirstClassCallableRule(); | ||
| } | ||
|
|
||
| #[RequiresPhp('>= 8.1.0')] | ||
| public function testRule(): void | ||
| { | ||
| $this->analyse([__DIR__ . '/data/nullsafe-first-class-callable.php'], [ | ||
| [ | ||
| 'Cannot combine nullsafe operator with Closure creation.', | ||
| 20, | ||
| ], | ||
| [ | ||
| 'Cannot combine nullsafe operator with Closure creation.', | ||
| 26, | ||
| ], | ||
| [ | ||
| 'Cannot combine nullsafe operator with Closure creation.', | ||
| 34, | ||
| ], | ||
| ]); | ||
| } | ||
|
|
||
| } |
36 changes: 36 additions & 0 deletions
36
tests/PHPStan/Rules/Methods/data/nullsafe-first-class-callable.php
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,36 @@ | ||
| <?php // lint >= 8.1 | ||
|
|
||
| declare(strict_types = 1); | ||
|
|
||
| namespace MethodCallableNullsafe; | ||
|
|
||
| class Foo | ||
| { | ||
|
|
||
| public function doFoo(): int | ||
| { | ||
| return 1; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| function test(?Foo $foo): void | ||
| { | ||
| // fatal error in PHP: "Cannot combine nullsafe operator with Closure creation" | ||
| $foo?->doFoo(...); | ||
| } | ||
|
|
||
| function testDynamic(?Foo $foo, string $method): void | ||
| { | ||
| // dynamic method name - also a fatal error in PHP | ||
| $foo?->{$method}(...); | ||
| } | ||
|
|
||
|
|
||
| class HelloWorld | ||
| { | ||
| public function sayHello(?self $self): void | ||
| { | ||
| $self?->sayHello(...); | ||
|
staabm marked this conversation as resolved.
|
||
| } | ||
| } | ||
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.