private function removeTrivialPhi(Block $block)
{
$toReplace = new \SplObjectStorage();
$replaced = new \SplObjectStorage();
$toReplace->attach($block);
while ($toReplace->count() > 0) {
foreach ($toReplace as $block) {
$toReplace->detach($block);
$replaced->attach($block);
foreach ($block->phi as $key => $phi) {
if ($this->tryRemoveTrivialPhi($phi, $block)) {
unset($block->phi[$key]);
}
}
foreach ($block->children as $child) {
foreach ($child->getSubBlocks() as $name) {
$subBlocks = $child->{$name};
if (!is_array($child->{$name})) {
if ($child->{$name} === null) {
continue;
}
$subBlocks = [$subBlocks];
}
foreach ($subBlocks as $subBlock) {
if (!$replaced->contains($subBlock)) {
$toReplace->attach($subBlock);
}
}
}
}
}
}
while ($this->trivialPhiCandidates->count() > 0) {
foreach ($this->trivialPhiCandidates as $phi) {
$block = $this->trivialPhiCandidates[$phi];
$this->trivialPhiCandidates->detach($phi);
if ($this->tryRemoveTrivialPhi($phi, $block)) {
$key = array_search($phi, $block->phi, true);
if ($key !== false) {
unset($block->phi[$key]);
}
}
}
}
}