@@ -42,52 +42,63 @@ public function import($data): NodeInterface
42
42
/**
43
43
* @param \PhpParser\Node $astNode
44
44
*
45
- * @return array<int, Node>
45
+ * @return \loophp\phptree\Node\NodeInterface
46
+ * @throws \Exception
46
47
*/
47
- private function getNodeChildren (Node $ astNode ): array
48
+ private function createNewNode (Node $ astNode ): NodeInterface
48
49
{
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
+ ];
52
57
53
- foreach ($ subNodeNames as $ subNodeName ) {
54
- $ subNodes = $ astNode ->{$ subNodeName };
58
+ return (new AttributeNode ($ defaultAttributes ))
59
+ ->add (...$ this ->parseNodes ($ this ->getAllNodeChildren ($ astNode )));
60
+ }
55
61
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 };
59
73
60
- foreach ($ subNodes as $ subNode ) {
61
- if (false === ($ subNode instanceof Node)) {
62
- continue ;
74
+ if (!is_array ($ subNodes )) {
75
+ $ subNodes = [$ subNodes ];
63
76
}
64
77
65
- $ nodes [] = $ subNode ;
66
- }
67
- }
78
+ return array_filter (
79
+ $ subNodes ,
80
+ 'is_object '
81
+ );
82
+ },
83
+ $ astNode ->getSubNodeNames ()
84
+ );
68
85
69
- return $ nodes ;
86
+ return array_merge (... $ astNodes ) ;
70
87
}
71
88
72
89
/**
73
90
* @param \PhpParser\Node $astNode
91
+ * @param callable $default
74
92
*
75
- * @throws \Exception
76
- *
77
- * @return AttributeNodeInterface
93
+ * @return \loophp\phptree\Node\AttributeNodeInterface
78
94
*/
79
- private function parseNode (Node $ astNode ): AttributeNodeInterface
95
+ private function getNodeFromCache (Node $ astNode, callable $ default ): AttributeNodeInterface
80
96
{
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
+ }
88
100
89
- return (new AttributeNode ($ attributes ))
90
- ->add (...$ this ->parseNodes ($ this ->getNodeChildren ($ astNode )));
101
+ return $ this ->nodeMap ->offsetGet ($ astNode );
91
102
}
92
103
93
104
/**
@@ -101,14 +112,8 @@ private function parseNodes(array $astNodes): array
101
112
{
102
113
$ treeNodes = [];
103
114
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 ' ]);
112
117
}
113
118
114
119
return $ treeNodes ;
0 commit comments