Fix phpstan/phpstan#12246: crash on first class callable as conditional do while#5194
Merged
staabm merged 5 commits intophpstan:2.1.xfrom Mar 11, 2026
Merged
Conversation
- Added printer methods for MethodCallableNode, FunctionCallableNode, StaticMethodCallableNode, and InstantiationCallableNode to Printer - These nodes delegate to printing their original AST node - The crash occurred because filterByTruthyValue/filterByFalseyValue tried to print callable nodes that had no printer method - New regression test in tests/PHPStan/Analyser/data/bug-12246.php Closes phpstan/phpstan#12246
staabm
approved these changes
Mar 11, 2026
Contributor
|
I verified that all added methods are covered by a test which crashes without the methods beeing present |
Contributor
VincentLanglet
left a comment
There was a problem hiding this comment.
I dunno this class but all other methods use sprintf for the implementation, why not the new methods ?
1e706aa to
945bccf
Compare
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.
Summary
Using a first-class callable (e.g.
$this->textElement(...)) as ado...whileloop condition caused a crash:Call to undefined method PHPStan\Node\Printer\Printer::pPHPStan_Node_MethodCallableNode(). This fix adds the missing printer methods for all callable node types.Changes
pPHPStan_Node_MethodCallableNode()tosrc/Node/Printer/Printer.phppPHPStan_Node_FunctionCallableNode()tosrc/Node/Printer/Printer.phppPHPStan_Node_StaticMethodCallableNode()tosrc/Node/Printer/Printer.phppPHPStan_Node_InstantiationCallableNode()tosrc/Node/Printer/Printer.phpgetOriginalNode()tests/PHPStan/Analyser/data/bug-12246.phptestBug12246intests/PHPStan/Analyser/AnalyserIntegrationTest.phpRoot cause
When a first-class callable is used as a
do...whilecondition,NodeScopeResolverconverts theMethodCallto aMethodCallableNodeand processes it. At the end ofprocessExprNode, the truthy/falsey scope callbacks call$scope->filterByTruthyValue($expr)with theMethodCallableNode. This callsgetNodeKey()which usesExprPrinter::printExpr(), which delegates to thePrinterclass. ThePrinter(extendingPhpParser\PrettyPrinter\Standard) looks for a method namedpPHPStan_Node_MethodCallableNode()based on the node'sgetType()return value, but no such method existed.The fix adds printer methods for all four callable node types (
FunctionCallableNode,MethodCallableNode,StaticMethodCallableNode,InstantiationCallableNode), each delegating to printing the original call expression node.Test
The regression test uses a
do...whileloop with$this->textElement(...)as the condition. Without the fix, this crashes with the printer error. With the fix, PHPStan correctly reports that the do-while condition is always true (since a Closure is always truthy).Fixes phpstan/phpstan#12246