Skip to content

Commit

Permalink
CleaningVisitor - keep closures and arrow functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Aug 17, 2023
1 parent a8fbf1a commit 0501aaf
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/Parser/CleaningVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
10 changes: 10 additions & 0 deletions tests/PHPStan/Parser/data/cleaning-1-after.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,13 @@ public function doFoo()
\func_get_args();
}
}
class ContainsClosure
{
public function doFoo()
{
static function () {
yield;
};
yield;
}
}
16 changes: 16 additions & 0 deletions tests/PHPStan/Parser/data/cleaning-1-before.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,19 @@ public function doFoo()
}
}
}

class ContainsClosure
{

public function doFoo()
{
return static function () {
if (doFoo()) {
echo 'foo';
}

yield;
};
}

}

0 comments on commit 0501aaf

Please sign in to comment.