diff --git a/src/Parser/CleaningVisitor.php b/src/Parser/CleaningVisitor.php index b5d406cf17..0a2c9aecff 100644 --- a/src/Parser/CleaningVisitor.php +++ b/src/Parser/CleaningVisitor.php @@ -52,11 +52,20 @@ private function keepVariadicsAndYields(array $stmts): array return in_array($node->name->toLowerString(), ParametersAcceptor::VARIADIC_FUNCTIONS, true); } + if ($node instanceof Node\Expr\Closure || $node instanceof Node\Expr\ArrowFunction) { + return true; + } + return false; }); $newStmts = []; foreach ($results as $result) { - if ($result instanceof Node\Expr\Yield_ || $result instanceof Node\Expr\YieldFrom) { + if ( + $result instanceof Node\Expr\Yield_ + || $result instanceof Node\Expr\YieldFrom + || $result instanceof Node\Expr\Closure + || $result instanceof Node\Expr\ArrowFunction + ) { $newStmts[] = new Node\Stmt\Expression($result); continue; } diff --git a/tests/PHPStan/Parser/data/cleaning-1-after.php b/tests/PHPStan/Parser/data/cleaning-1-after.php index e13a195e7d..1d22a6ac92 100644 --- a/tests/PHPStan/Parser/data/cleaning-1-after.php +++ b/tests/PHPStan/Parser/data/cleaning-1-after.php @@ -39,3 +39,13 @@ public function doFoo() \func_get_args(); } } +class ContainsClosure +{ + public function doFoo() + { + static function () { + yield; + }; + yield; + } +} diff --git a/tests/PHPStan/Parser/data/cleaning-1-before.php b/tests/PHPStan/Parser/data/cleaning-1-before.php index 468db05176..ae93a81b77 100644 --- a/tests/PHPStan/Parser/data/cleaning-1-before.php +++ b/tests/PHPStan/Parser/data/cleaning-1-before.php @@ -67,3 +67,19 @@ public function doFoo() } } } + +class ContainsClosure +{ + + public function doFoo() + { + return static function () { + if (doFoo()) { + echo 'foo'; + } + + yield; + }; + } + +}