From 9d97d4950cd293648e51d843ca0721e6ff6aba1f Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sun, 22 Dec 2024 22:22:19 +0700 Subject: [PATCH] Fix crash on MethodCallableNode on do while --- src/Node/Printer/ExprPrinter.php | 4 ++++ .../Rules/Methods/CallMethodsRuleTest.php | 10 ++++++++++ .../data/first_class_callable_do_while.php | 18 ++++++++++++++++++ 3 files changed, 32 insertions(+) create mode 100644 tests/PHPStan/Rules/Methods/data/first_class_callable_do_while.php diff --git a/src/Node/Printer/ExprPrinter.php b/src/Node/Printer/ExprPrinter.php index 32505ef568..3edfd91d4d 100644 --- a/src/Node/Printer/ExprPrinter.php +++ b/src/Node/Printer/ExprPrinter.php @@ -3,6 +3,7 @@ namespace PHPStan\Node\Printer; use PhpParser\Node\Expr; +use PHPStan\Node\MethodCallableNode; /** * @api @@ -19,6 +20,9 @@ public function printExpr(Expr $expr): string /** @var string|null $exprString */ $exprString = $expr->getAttribute('phpstan_cache_printer'); if ($exprString === null) { + if ($expr instanceof MethodCallableNode) { + $expr = $expr->getOriginalNode(); + } $exprString = $this->printer->prettyPrintExpr($expr); $expr->setAttribute('phpstan_cache_printer', $exprString); } diff --git a/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php b/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php index 0da6911a43..b5630ec34c 100644 --- a/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php +++ b/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php @@ -3491,4 +3491,14 @@ public function testBug4801(): void $this->analyse([__DIR__ . '/data/bug-4801.php'], []); } + public function testBug12246(): void + { + $this->checkThisOnly = false; + $this->checkNullables = false; + $this->checkUnionTypes = false; + $this->checkExplicitMixed = false; + + $this->analyse([ __DIR__ . '/data/first_class_callable_do_while.php'], []); + } + } diff --git a/tests/PHPStan/Rules/Methods/data/first_class_callable_do_while.php b/tests/PHPStan/Rules/Methods/data/first_class_callable_do_while.php new file mode 100644 index 0000000000..6ec58ff347 --- /dev/null +++ b/tests/PHPStan/Rules/Methods/data/first_class_callable_do_while.php @@ -0,0 +1,18 @@ +textElement(...)); + } + + public function textElement() + { + return 1; + } +}