Skip to content
This repository has been archived by the owner on Sep 24, 2020. It is now read-only.

Commit

Permalink
[CFG] Initial for Foreach
Browse files Browse the repository at this point in the history
  • Loading branch information
ovr committed Feb 26, 2019
1 parent c3478ec commit 8419d75
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/ControlFlow/ControlFlowGraph.php
Expand Up @@ -83,6 +83,9 @@ protected function passNodes(array $nodes, Block $block)
case \PhpParser\Node\Stmt\Return_::class:
$block = $this->passReturn($stmt, $block);
break;
case \PhpParser\Node\Stmt\Foreach_::class:
$block = $this->passForeach($stmt, $block);
break;
case \PhpParser\Node\Stmt\For_::class:
$block = $this->passFor($stmt, $block);
break;
Expand Down Expand Up @@ -218,6 +221,32 @@ protected function passIf(\PhpParser\Node\Stmt\If_ $if, Block $block)
return $exitBlock;
}

/**
* @param \PhpParser\Node\Stmt\Foreach_ $foreach
* @param Block $block
* @return Block
*/
protected function passForeach(\PhpParser\Node\Stmt\Foreach_ $foreach, Block $block)
{
$block->setExit(
$next = new Block($this->lastBlockId++)
);

$next->setExit(
$loop = new Block($this->lastBlockId++)
);

$this->passNodes($foreach->stmts, $loop);

$loop->addChildren(new Node\JumpNode($next));

$loop->setExit(
$after = new Block($this->lastBlockId++)
);

return $after;
}

/**
* @param \PhpParser\Node\Stmt\For_ $for
* @param Block $block
Expand Down

0 comments on commit 8419d75

Please sign in to comment.