Skip to content

Expose ClassMethod and Function_ on MethodReturnStatementsNode and FunctionReturnStatementsNode - #6958

Merged
ondrejmirtes merged 1 commit into
phpstan:2.3.xfrom
janedbal:jn-return-statements-node-getters
Nov 5, 2026
Merged

ondrejmirtes merged 1 commit into
phpstan:2.3.xfrom
janedbal:jn-return-statements-node-getters

Conversation

@janedbal

@janedbal janedbal commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

This PR adds MethodReturnStatementsNode::getClassMethod() and FunctionReturnStatementsNode::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:

Node Getter
ClosureReturnStatementsNode getClosureExpr(): Closure
PropertyHookReturnStatementsNode getPropertyHookNode(): PropertyHook
InClassMethodNode getOriginalNode(): ClassMethod
InFunctionNode getOriginalNode(): Function_
MethodReturnStatementsNode none (this PR adds getClassMethod(): ClassMethod)
FunctionReturnStatementsNode none (this PR adds getFunction(): Function_)

Fixable rules. In PHPStan 2.3.5, RuleErrorBuilder::fixNode() and removeNode() find the node to fix by its class and file position (see ReplacingNodeVisitor). Thus a rule must give a real parser node. A rule on ReturnStatementsNode can find that a native return type is too wide, for example because of a null that the method never returns. To fix it, the rule must change ClassMethod::$returnType. Today the rule cannot get that node. shipmonk-rnd/phpstan-rules needs a custom parser visitor for this (shipmonk-rnd/phpstan-rules#371, see ReturnTypeNodeVisitor). That workaround works only because PHPStan copies the attributes of the method or function to its ReturnStatementsNode. With the new getters, the visitor is not necessary.

Behaviour and test

There is no behaviour change. The getters return the existing private properties.

ReturnStatementsNodeParserNodeRuleTest uses a test rule on ReturnStatementsNode. The rule reads the parser node through the new getters and reports its name, lines and return type node.

Co-Authored-By: Claude Code

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
@ondrejmirtes
ondrejmirtes merged commit f3f76b2 into phpstan:2.3.x Nov 5, 2026
611 of 661 checks passed
@ondrejmirtes

Copy link
Copy Markdown
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants