Skip to content

Commit

Permalink
handle by check Return in ArrowFunction
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik committed Oct 22, 2022
1 parent 2f19923 commit f7125f3
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions packages/PhpDocParser/NodeTraverser/SimpleCallableNodeTraverser.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
namespace Rector\PhpDocParser\NodeTraverser;

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ArrowFunction;
use PhpParser\Node\Stmt\Return_;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor\ParentConnectingVisitor;
use Rector\NodeTypeResolver\Node\AttributeKey;
Expand Down Expand Up @@ -36,27 +39,34 @@ public function traverseNodesWithCallable(Node | array | null $nodes, callable $
$nodeTraverser = new NodeTraverser();
$callableNodeVisitor = new CallableNodeVisitor($callable);
$nodeTraverser->addVisitor($callableNodeVisitor);

if ($this->shouldConnectParent($nodes)) {
$nodeTraverser->addVisitor(new ParentConnectingVisitor());
}

$this->mirrorParent($nodes);
$nodeTraverser->addVisitor(new ParentConnectingVisitor());
$nodeTraverser->traverse($nodes);
}

/**
* @param Node[] $nodes
*/
private function shouldConnectParent(array $nodes): bool
private function mirrorParent(array $nodes): void
{
foreach ($nodes as $node) {
if (! $node instanceof Return_) {
continue;
}

if (! $node->expr instanceof Expr) {
continue;
}

$parentNode = $node->getAttribute(AttributeKey::PARENT_NODE);
if ($parentNode instanceof Node) {
continue;
}

if (! $parentNode instanceof Node) {
return false;
$exprParent = $node->expr->getAttribute(AttributeKey::PARENT_NODE);
if ($exprParent instanceof ArrowFunction) {
$node->setAttribute(AttributeKey::PARENT_NODE, $exprParent);
}
}

return true;
}
}

0 comments on commit f7125f3

Please sign in to comment.