9
9
use loophp \phptree \Node \AttributeNodeInterface ;
10
10
use loophp \phptree \Node \NodeInterface ;
11
11
use PhpParser \Node ;
12
- use SplObjectStorage ;
13
12
14
13
use function is_array ;
15
14
18
17
*/
19
18
final class NikicPhpParser implements ImporterInterface
20
19
{
21
- /**
22
- * @var SplObjectStorage
23
- */
24
- private $ nodeMap ;
25
-
26
- public function __construct ()
27
- {
28
- $ this ->nodeMap = new SplObjectStorage ();
29
- }
30
-
31
20
/**
32
21
* @param Node[] $data
33
22
*
@@ -37,26 +26,17 @@ public function __construct()
37
26
*/
38
27
public function import ($ data ): NodeInterface
39
28
{
40
- return (new AttributeNode (['label ' => 'root ' ]))
41
- ->add (...$ this ->parseNodes (...$ data ));
29
+ return $ this ->parseNode ($ this ->createNode (['label ' => 'root ' ]), ...$ data );
42
30
}
43
31
44
32
/**
45
- * @param \PhpParser\Node $astNode
33
+ * @param array $attributes
46
34
*
47
- * @throws \Exception
48
- *
49
- * @return \loophp\phptree\Node\NodeInterface
35
+ * @return \loophp\phptree\Node\AttributeNodeInterface
50
36
*/
51
- private function createNewNode ( Node $ astNode ): NodeInterface
37
+ private function createNode ( array $ attributes ): AttributeNodeInterface
52
38
{
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 );
60
40
}
61
41
62
42
/**
@@ -87,35 +67,28 @@ static function (string $subNodeName) use ($astNode): array {
87
67
}
88
68
89
69
/**
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
105
71
* @param Node ...$astNodes
106
72
*
107
- * @throws \Exception
108
- *
109
- * @return AttributeNodeInterface[]
73
+ * @return \loophp\phptree\Node\NodeInterface
110
74
*/
111
- private function parseNodes ( Node ...$ astNodes ): array
75
+ private function parseNode ( AttributeNodeInterface $ parent , Node ...$ astNodes ): NodeInterface
112
76
{
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
+ );
120
93
}
121
94
}
0 commit comments