Repository navigation
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
Closed
phpstan-bot
wants to merge
1
commit into
phpstan:2.2.x
from
phpstan-bot:create-pull-request/patch-3pnp41f
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
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.
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.