-
Notifications
You must be signed in to change notification settings - Fork 576
Treat first-class callable NullsafeMethodCall as a MethodCall instead of throwing
#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
9
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
9 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 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
73 changes: 73 additions & 0 deletions
73
src/Analyser/ExprHandler/Virtual/NullsafeFirstClassCallableNodeHandler.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,73 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Analyser\ExprHandler\Virtual; | ||
|
|
||
| use PhpParser\Node\Expr; | ||
| use PhpParser\Node\Stmt; | ||
| use PHPStan\Analyser\ExpressionContext; | ||
| use PHPStan\Analyser\ExpressionResult; | ||
| use PHPStan\Analyser\ExpressionResultStorage; | ||
| use PHPStan\Analyser\ExprHandler; | ||
| use PHPStan\Analyser\MutatingScope; | ||
| use PHPStan\Analyser\NodeScopeResolver; | ||
| use PHPStan\Analyser\Scope; | ||
| use PHPStan\Analyser\SpecifiedTypes; | ||
| use PHPStan\Analyser\TypeSpecifier; | ||
| use PHPStan\Analyser\TypeSpecifierContext; | ||
| use PHPStan\DependencyInjection\AutowiredService; | ||
| use PHPStan\Node\NullsafeFirstClassCallableNode; | ||
| use PHPStan\Type\MixedType; | ||
| use PHPStan\Type\Type; | ||
| use function array_merge; | ||
|
|
||
| /** | ||
| * @implements ExprHandler<NullsafeFirstClassCallableNode> | ||
| */ | ||
| #[AutowiredService] | ||
| final class NullsafeFirstClassCallableNodeHandler implements ExprHandler | ||
| { | ||
|
|
||
| public function supports(Expr $expr): bool | ||
| { | ||
| return $expr instanceof NullsafeFirstClassCallableNode; | ||
| } | ||
|
|
||
| public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult | ||
| { | ||
| $varResult = $nodeScopeResolver->processExprNode($stmt, $expr->getVar(), $scope, $storage, $nodeCallback, ExpressionContext::createDeep()); | ||
| $scope = $varResult->getScope(); | ||
| $hasYield = $varResult->hasYield(); | ||
| $throwPoints = $varResult->getThrowPoints(); | ||
| $impurePoints = $varResult->getImpurePoints(); | ||
| $isAlwaysTerminating = false; | ||
| if ($expr->getName() instanceof Expr) { | ||
| $nameResult = $nodeScopeResolver->processExprNode($stmt, $expr->getName(), $scope, $storage, $nodeCallback, ExpressionContext::createDeep()); | ||
| $scope = $nameResult->getScope(); | ||
| $hasYield = $hasYield || $nameResult->hasYield(); | ||
| $throwPoints = array_merge($throwPoints, $nameResult->getThrowPoints()); | ||
| $impurePoints = array_merge($impurePoints, $nameResult->getImpurePoints()); | ||
| $isAlwaysTerminating = $nameResult->isAlwaysTerminating(); | ||
| } | ||
|
|
||
| return new ExpressionResult( | ||
| $scope, | ||
| hasYield: $hasYield, | ||
| isAlwaysTerminating: $isAlwaysTerminating, | ||
| throwPoints: $throwPoints, | ||
| impurePoints: $impurePoints, | ||
| ); | ||
| } | ||
|
|
||
| public function resolveType(MutatingScope $scope, Expr $expr): Type | ||
| { | ||
| // in practice the type of the first-class callable is resolved | ||
| // by NullsafeMethodCallHandler / FirstClassCallableMethodCallHandler | ||
| return new MixedType(); | ||
| } | ||
|
|
||
| public function specifyTypes(TypeSpecifier $typeSpecifier, Scope $scope, Expr $expr, TypeSpecifierContext $context): SpecifiedTypes | ||
| { | ||
| return $typeSpecifier->specifyDefaultTypes($scope, $expr, $context); | ||
| } | ||
|
|
||
| } |
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
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 NullsafeFirstClassCallableNode 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
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\NullsafeFirstClassCallableNode; | ||
| use PHPStan\Rules\Rule; | ||
| use PHPStan\Rules\RuleErrorBuilder; | ||
|
|
||
| /** | ||
| * @implements Rule<NullsafeFirstClassCallableNode> | ||
| */ | ||
| #[RegisteredRule(level: 0)] | ||
| final class NullsafeFirstClassCallableRule implements Rule | ||
| { | ||
|
|
||
| public function getNodeType(): string | ||
| { | ||
| return NullsafeFirstClassCallableNode::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(...); | ||
| } | ||
| } | ||
31 changes: 31 additions & 0 deletions
31
tests/PHPStan/Rules/Methods/NullsafeFirstClassCallableRuleTest.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,31 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Rules\Methods; | ||
|
|
||
| use PHPStan\Rules\Rule; | ||
| use PHPStan\Testing\RuleTestCase; | ||
| use PHPUnit\Framework\Attributes\RequiresPhp; | ||
|
|
||
| /** | ||
| * @extends RuleTestCase<NullsafeFirstClassCallableRule> | ||
| */ | ||
| class NullsafeFirstClassCallableRuleTest extends RuleTestCase | ||
| { | ||
|
|
||
| protected function getRule(): Rule | ||
| { | ||
| return new NullsafeFirstClassCallableRule(); | ||
| } | ||
|
|
||
| #[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, | ||
| ], | ||
| ]); | ||
| } | ||
|
|
||
| } |
21 changes: 21 additions & 0 deletions
21
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,21 @@ | ||
| <?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(...); | ||
| } |
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.
we expect a
Cannot combine nullsafe operator with Closure creationerror on this lineThere 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.
I've addressed staabm's review. Here's what I did:
The review
staabm pointed out that for
$self?->sayHello(...)PHPStan should report aCannot combine nullsafe operator with Closure creationerror (matching PHP's fatal error), not just silently treat it as a valid method-call first-class callable.Changes
src/Node/MethodCallableNode.php— added anullsafeflag (defaultfalse) plus anisNullsafe()accessor, so the node can record that it originated from a$foo?->bar(...)expression.src/Analyser/NodeScopeResolver.php— theNullsafeMethodCallfirst-class-callable branch now passesnullsafe: truewhen building theMethodCallableNode.src/Rules/Methods/MethodCallableRule.php— when the node is nullsafe, it reports a non-ignorable errorCannot combine nullsafe operator with Closure creation.(identifiernullsafe.firstClassCallable) and skips the normal method-existence check.Tests:
tests/PHPStan/Rules/Methods/data/method-callable-nullsafe.php+testNullsafe()inMethodCallableRuleTest— direct rule-level coverage of the new error.testBug9746()inAnalyserIntegrationTest, which was inconsistent with its data file (it referenced aBug9746Integrationnamespace/file that no longer existed). It now asserts the actual two errors, including the nullsafe one.Verification
testNullsafefails without the source change (it instead reportsCannot call method doFoo() on Foo|null), confirming the test catches the regression; passes with the fix.make phpstan: No errors.Committed as
d31bdac35and pushed.