Skip to content

Commit 355cc74

Browse files
committed
Minor refactoring.
1 parent fd4af8e commit 355cc74

File tree

2 files changed

+44
-39
lines changed

2 files changed

+44
-39
lines changed

spec/loophp/phptree/Importer/NikicPhpParserImporterSpec.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function it_can_import(): void
3333
$this
3434
->import($ast)
3535
->count()
36-
->shouldReturn(524);
36+
->shouldReturn(563);
3737

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

src/Importer/NikicPhpParserImporter.php

+43-38
Original file line numberDiff line numberDiff line change
@@ -42,52 +42,63 @@ public function import($data): NodeInterface
4242
/**
4343
* @param \PhpParser\Node $astNode
4444
*
45-
* @return array<int, Node>
45+
* @return \loophp\phptree\Node\NodeInterface
46+
* @throws \Exception
4647
*/
47-
private function getNodeChildren(Node $astNode): array
48+
private function createNewNode(Node $astNode): NodeInterface
4849
{
49-
$subNodeNames = $astNode->getSubNodeNames();
50-
51-
$nodes = [];
50+
$defaultAttributes = [
51+
'label' => sprintf('%s', $astNode->getType()),
52+
'type' => $astNode->getType(),
53+
'class' => get_class($astNode),
54+
'shape' => 'rect',
55+
'astNode' => $astNode,
56+
];
5257

53-
foreach ($subNodeNames as $subNodeName) {
54-
$subNodes = $astNode->{$subNodeName};
58+
return (new AttributeNode($defaultAttributes))
59+
->add(...$this->parseNodes($this->getAllNodeChildren($astNode)));
60+
}
5561

56-
if (!is_array($subNodes)) {
57-
$subNodes = [$subNodes];
58-
}
62+
/**
63+
* @param \PhpParser\Node $astNode
64+
*
65+
* @return array<int, Node>
66+
*/
67+
private function getAllNodeChildren(Node $astNode): array
68+
{
69+
/** @var array<int, array<int, Node>> $astNodes */
70+
$astNodes = array_map(
71+
static function (string $subNodeName) use ($astNode): array {
72+
$subNodes = $astNode->{$subNodeName};
5973

60-
foreach ($subNodes as $subNode) {
61-
if (false === ($subNode instanceof Node)) {
62-
continue;
74+
if (!is_array($subNodes)) {
75+
$subNodes = [$subNodes];
6376
}
6477

65-
$nodes[] = $subNode;
66-
}
67-
}
78+
return array_filter(
79+
$subNodes,
80+
'is_object'
81+
);
82+
},
83+
$astNode->getSubNodeNames()
84+
);
6885

69-
return $nodes;
86+
return array_merge(...$astNodes);
7087
}
7188

7289
/**
7390
* @param \PhpParser\Node $astNode
91+
* @param callable $default
7492
*
75-
* @throws \Exception
76-
*
77-
* @return AttributeNodeInterface
93+
* @return \loophp\phptree\Node\AttributeNodeInterface
7894
*/
79-
private function parseNode(Node $astNode): AttributeNodeInterface
95+
private function getNodeFromCache(Node $astNode, callable $default): AttributeNodeInterface
8096
{
81-
$attributes = [
82-
'label' => sprintf('%s', $astNode->getType()),
83-
'type' => $astNode->getType(),
84-
'class' => get_class($astNode),
85-
'shape' => 'rect',
86-
'astNode' => $astNode,
87-
];
97+
if (false === $this->nodeMap->contains($astNode)) {
98+
$this->nodeMap->attach($astNode, $default($astNode));
99+
}
88100

89-
return (new AttributeNode($attributes))
90-
->add(...$this->parseNodes($this->getNodeChildren($astNode)));
101+
return $this->nodeMap->offsetGet($astNode);
91102
}
92103

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

104-
foreach ($astNodes as $node) {
105-
if (false === $this->nodeMap->contains($node)) {
106-
$treeNode = $this->parseNode($node);
107-
$treeNode->setAttribute('astNode', $node);
108-
$this->nodeMap->attach($node, $treeNode);
109-
}
110-
111-
$treeNodes[] = $this->nodeMap->offsetGet($node);
115+
foreach ($astNodes as $astNode) {
116+
$treeNodes[] = $this->getNodeFromCache($astNode, [$this, 'createNewNode']);
112117
}
113118

114119
return $treeNodes;

0 commit comments

Comments
 (0)