Skip to content

Commit

Permalink
Minor refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Feb 28, 2020
1 parent fd4af8e commit 355cc74
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function it_can_import(): void
$this
->import($ast)
->count()
->shouldReturn(524);
->shouldReturn(563);

$file = __DIR__ . '/../../../../tests/sample.php';

Expand Down
81 changes: 43 additions & 38 deletions src/Importer/NikicPhpParserImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,52 +42,63 @@ public function import($data): NodeInterface
/**
* @param \PhpParser\Node $astNode
*
* @return array<int, Node>
* @return \loophp\phptree\Node\NodeInterface
* @throws \Exception
*/
private function getNodeChildren(Node $astNode): array
private function createNewNode(Node $astNode): NodeInterface
{
$subNodeNames = $astNode->getSubNodeNames();

$nodes = [];
$defaultAttributes = [
'label' => sprintf('%s', $astNode->getType()),
'type' => $astNode->getType(),
'class' => get_class($astNode),
'shape' => 'rect',
'astNode' => $astNode,
];

foreach ($subNodeNames as $subNodeName) {
$subNodes = $astNode->{$subNodeName};
return (new AttributeNode($defaultAttributes))
->add(...$this->parseNodes($this->getAllNodeChildren($astNode)));
}

if (!is_array($subNodes)) {
$subNodes = [$subNodes];
}
/**
* @param \PhpParser\Node $astNode
*
* @return array<int, Node>
*/
private function getAllNodeChildren(Node $astNode): array
{
/** @var array<int, array<int, Node>> $astNodes */
$astNodes = array_map(
static function (string $subNodeName) use ($astNode): array {
$subNodes = $astNode->{$subNodeName};

foreach ($subNodes as $subNode) {
if (false === ($subNode instanceof Node)) {
continue;
if (!is_array($subNodes)) {
$subNodes = [$subNodes];
}

$nodes[] = $subNode;
}
}
return array_filter(
$subNodes,
'is_object'
);
},
$astNode->getSubNodeNames()
);

return $nodes;
return array_merge(...$astNodes);
}

/**
* @param \PhpParser\Node $astNode
* @param callable $default
*
* @throws \Exception
*
* @return AttributeNodeInterface
* @return \loophp\phptree\Node\AttributeNodeInterface
*/
private function parseNode(Node $astNode): AttributeNodeInterface
private function getNodeFromCache(Node $astNode, callable $default): AttributeNodeInterface
{
$attributes = [
'label' => sprintf('%s', $astNode->getType()),
'type' => $astNode->getType(),
'class' => get_class($astNode),
'shape' => 'rect',
'astNode' => $astNode,
];
if (false === $this->nodeMap->contains($astNode)) {
$this->nodeMap->attach($astNode, $default($astNode));
}

return (new AttributeNode($attributes))
->add(...$this->parseNodes($this->getNodeChildren($astNode)));
return $this->nodeMap->offsetGet($astNode);
}

/**
Expand All @@ -101,14 +112,8 @@ private function parseNodes(array $astNodes): array
{
$treeNodes = [];

foreach ($astNodes as $node) {
if (false === $this->nodeMap->contains($node)) {
$treeNode = $this->parseNode($node);
$treeNode->setAttribute('astNode', $node);
$this->nodeMap->attach($node, $treeNode);
}

$treeNodes[] = $this->nodeMap->offsetGet($node);
foreach ($astNodes as $astNode) {
$treeNodes[] = $this->getNodeFromCache($astNode, [$this, 'createNewNode']);
}

return $treeNodes;
Expand Down

0 comments on commit 355cc74

Please sign in to comment.