Skip to content

Commit b7b76b5

Browse files
committed
Refactor nikic/php-parser importer.
1 parent fbe967e commit b7b76b5

File tree

1 file changed

+24
-51
lines changed

1 file changed

+24
-51
lines changed

src/Importer/NikicPhpParser.php

+24-51
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use loophp\phptree\Node\AttributeNodeInterface;
1010
use loophp\phptree\Node\NodeInterface;
1111
use PhpParser\Node;
12-
use SplObjectStorage;
1312

1413
use function is_array;
1514

@@ -18,16 +17,6 @@
1817
*/
1918
final class NikicPhpParser implements ImporterInterface
2019
{
21-
/**
22-
* @var SplObjectStorage
23-
*/
24-
private $nodeMap;
25-
26-
public function __construct()
27-
{
28-
$this->nodeMap = new SplObjectStorage();
29-
}
30-
3120
/**
3221
* @param Node[] $data
3322
*
@@ -37,26 +26,17 @@ public function __construct()
3726
*/
3827
public function import($data): NodeInterface
3928
{
40-
return (new AttributeNode(['label' => 'root']))
41-
->add(...$this->parseNodes(...$data));
29+
return $this->parseNode($this->createNode(['label' => 'root']), ...$data);
4230
}
4331

4432
/**
45-
* @param \PhpParser\Node $astNode
33+
* @param array $attributes
4634
*
47-
* @throws \Exception
48-
*
49-
* @return \loophp\phptree\Node\NodeInterface
35+
* @return \loophp\phptree\Node\AttributeNodeInterface
5036
*/
51-
private function createNewNode(Node $astNode): NodeInterface
37+
private function createNode(array $attributes): AttributeNodeInterface
5238
{
53-
$defaultAttributes = [
54-
'label' => $astNode->getType(),
55-
'astNode' => $astNode,
56-
];
57-
58-
return (new AttributeNode($defaultAttributes))
59-
->add(...$this->parseNodes(...$this->getAllNodeChildren($astNode)));
39+
return new AttributeNode($attributes);
6040
}
6141

6242
/**
@@ -87,35 +67,28 @@ static function (string $subNodeName) use ($astNode): array {
8767
}
8868

8969
/**
90-
* @param \PhpParser\Node $astNode
91-
* @param callable $default
92-
*
93-
* @return \loophp\phptree\Node\AttributeNodeInterface
94-
*/
95-
private function getNodeFromCache(Node $astNode, callable $default): AttributeNodeInterface
96-
{
97-
if (false === $this->nodeMap->contains($astNode)) {
98-
$this->nodeMap->attach($astNode, $default($astNode));
99-
}
100-
101-
return $this->nodeMap->offsetGet($astNode);
102-
}
103-
104-
/**
70+
* @param \loophp\phptree\Node\AttributeNodeInterface $parent
10571
* @param Node ...$astNodes
10672
*
107-
* @throws \Exception
108-
*
109-
* @return AttributeNodeInterface[]
73+
* @return \loophp\phptree\Node\NodeInterface
11074
*/
111-
private function parseNodes(Node ...$astNodes): array
75+
private function parseNode(AttributeNodeInterface $parent, Node ...$astNodes): NodeInterface
11276
{
113-
$treeNodes = [];
114-
115-
foreach ($astNodes as $astNode) {
116-
$treeNodes[] = $this->getNodeFromCache($astNode, [$this, 'createNewNode']);
117-
}
118-
119-
return $treeNodes;
77+
return array_reduce(
78+
$astNodes,
79+
function (AttributeNodeInterface $carry, Node $astNode): NodeInterface {
80+
return $carry
81+
->add(
82+
$this->parseNode(
83+
$this->createNode([
84+
'label' => $astNode->getType(),
85+
'astNode' => $astNode,
86+
]),
87+
...$this->getAllNodeChildren($astNode)
88+
)
89+
);
90+
},
91+
$parent
92+
);
12093
}
12194
}

0 commit comments

Comments
 (0)