Repository navigation
Expose ClassMethod and Function_ on MethodReturnStatementsNode and FunctionReturnStatementsNode - #6958
Merged
ondrejmirtes merged 1 commit intoNov 5, 2026
Conversation
MethodReturnStatementsNode::getClassMethod() and FunctionReturnStatementsNode::getFunction() return the parser node of the analysed method or function. ClosureReturnStatementsNode and PropertyHookReturnStatementsNode already expose their parser nodes. A fixable rule on ReturnStatementsNode can now change the signature, for example the native return type. Co-Authored-By: Claude Code
Member
|
Thank you! |
janedbal
added a commit
to shipmonk-rnd/phpstan-rules
that referenced
this pull request
Nov 5, 2026
phpstan/phpstan-src#6958 adds MethodReturnStatementsNode::getClassMethod() and FunctionReturnStatementsNode::getFunction(). forbidUselessNullableReturn now gets the native return type node from them, and ReturnTypeNodeVisitor is removed. The getters are not in a tagged release yet, so composer.json requires ^2.3.6@dev. Change it to ^2.3.6 after the tag. Co-Authored-By: Claude Code
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This PR adds
MethodReturnStatementsNode::getClassMethod()andFunctionReturnStatementsNode::getFunction(). Each getter returns the parser node of the analysed method or function. Both classes are@api, so the getters are part of the public API.Why
Consistency with the sibling nodes. The other nodes already expose their parser node:
ClosureReturnStatementsNodegetClosureExpr(): ClosurePropertyHookReturnStatementsNodegetPropertyHookNode(): PropertyHookInClassMethodNodegetOriginalNode(): ClassMethodInFunctionNodegetOriginalNode(): Function_MethodReturnStatementsNodegetClassMethod(): ClassMethod)FunctionReturnStatementsNodegetFunction(): Function_)Fixable rules. In PHPStan 2.3.5,
RuleErrorBuilder::fixNode()andremoveNode()find the node to fix by its class and file position (seeReplacingNodeVisitor). Thus a rule must give a real parser node. A rule onReturnStatementsNodecan find that a native return type is too wide, for example because of anullthat the method never returns. To fix it, the rule must changeClassMethod::$returnType. Today the rule cannot get that node. shipmonk-rnd/phpstan-rules needs a custom parser visitor for this (shipmonk-rnd/phpstan-rules#371, seeReturnTypeNodeVisitor). That workaround works only because PHPStan copies the attributes of the method or function to itsReturnStatementsNode. With the new getters, the visitor is not necessary.Behaviour and test
There is no behaviour change. The getters return the existing private properties.
ReturnStatementsNodeParserNodeRuleTestuses a test rule onReturnStatementsNode. The rule reads the parser node through the new getters and reports its name, lines and return type node.Co-Authored-By: Claude Code