public function serialize(ControlFlowGraph $graph) { $dot = "digraph G {\n"; $this->ids = new \SplObjectStorage(); $this->idCount = 0; $dot .= " " . $this->getId($graph->getImplicitReturn()) . " [shape=box,label=\"implicit return\",style=filled]\n"; $entryPoint = $graph->getEntryPoint(); $nodes = $graph->getNodes(); foreach ($nodes as $astNode) { $node = $nodes[$astNode]; $id = $this->getId($node); $dot .= sprintf(' %s [shape=box,label="%s"', $id, str_replace('"', '\\"', NodeUtil::getStringRepr($astNode))); if ($node === $entryPoint) { $dot .= ",style=filled"; } $dot .= "]\n"; foreach ($node->getOutEdges() as $edge) { $dot .= sprintf(" %s -> %s", $id, $this->getId($edge->getDest())); switch ($edge->getType()) { case GraphEdge::TYPE_ON_FALSE: $dot .= ' [label="false"]'; break; case GraphEdge::TYPE_ON_TRUE: $dot .= ' [label="true"]'; break; case GraphEdge::TYPE_ON_EX: $dot .= ' [label="exception"]'; break; } $dot .= "\n"; } } $dot .= "}"; return $dot; }
private function assignPriorities() { $this->priorityCounter = 0; $entry = $this->graph->getEntryPoint(); $this->prioritizeFromEntryNode($entry); // At this point, all reachable nodes have been given a priority. There might still unreachable nodes which do // not yet have been assigned a priority. Presumably, it does not matter anyway what priority they get since // should not happen in real code. foreach ($nodes = $this->graph->getNodes() as $astNode) { /** @var $candidate GraphNode */ $candidate = $nodes[$astNode]; if (null !== $candidate->getAttribute('priority')) { continue; } $candidate->setAttribute('priority', ++$this->priorityCounter); } // The implicit return is always last. $this->graph->getImplicitReturn()->setAttribute('priority', ++$this->priorityCounter); }