1 | <?php |
||
31 | final class ElementNameResolver extends NodeVisitorAbstract |
||
|
|||
32 | { |
||
33 | /** |
||
34 | * @var \SplDoublyLinkedList |
||
35 | */ |
||
36 | private $parts = null; |
||
37 | |||
38 | /** |
||
39 | * Resets the object to a known state before start processing. |
||
40 | * |
||
41 | * @param array $nodes |
||
42 | */ |
||
43 | public function beforeTraverse(array $nodes) |
||
47 | |||
48 | /** |
||
49 | * Performs a reset of the added element when needed. |
||
50 | * |
||
51 | * @param Node $node |
||
52 | */ |
||
53 | public function leaveNode(Node $node) |
||
54 | { |
||
55 | switch (get_class($node)) { |
||
56 | case Namespace_::class: |
||
57 | case Class_::class: |
||
58 | case ClassMethod::class: |
||
59 | case Trait_::class: |
||
60 | case PropertyProperty::class: |
||
61 | case ClassConst::class: |
||
62 | case Const_::class: |
||
63 | case Interface_::class: |
||
64 | case Function_::class: |
||
65 | $this->parts->pop(); |
||
66 | break; |
||
67 | } |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * Adds fqsen property to a node when applicable. |
||
72 | * |
||
73 | * @param Node $node |
||
74 | */ |
||
75 | 5 | public function enterNode(Node $node) |
|
76 | { |
||
77 | 5 | switch (get_class($node)) { |
|
78 | 5 | case Namespace_::class: |
|
79 | 1 | $this->resetState('\\' . $node->name . '\\'); |
|
80 | 1 | $node->fqsen = new Fqsen($this->buildName()); |
|
81 | 1 | break; |
|
82 | 5 | case Class_::class: |
|
83 | 3 | case Trait_::class: |
|
84 | 3 | case Interface_::class: |
|
85 | 3 | $this->parts->push((string)$node->name); |
|
86 | |||
87 | 3 | if (is_null($node->name)) { |
|
88 | 1 | return NodeTraverser::DONT_TRAVERSE_CHILDREN; |
|
89 | } |
||
90 | |||
91 | 2 | $node->fqsen = new Fqsen($this->buildName()); |
|
92 | 2 | break; |
|
93 | 3 | case Function_::class: |
|
94 | 1 | $this->parts->push($node->name . '()'); |
|
95 | 1 | $node->fqsen = new Fqsen($this->buildName()); |
|
96 | 1 | break; |
|
97 | 2 | case ClassMethod::class: |
|
98 | $this->parts->push('::' . $node->name . '()'); |
||
99 | $node->fqsen = new Fqsen($this->buildName()); |
||
100 | break; |
||
101 | 2 | case ClassConst::class: |
|
102 | 1 | $this->parts->push('::'); |
|
103 | 1 | break; |
|
104 | 2 | case Const_::class: |
|
105 | 2 | $this->parts->push($node->name); |
|
106 | 2 | $node->fqsen = new Fqsen($this->buildName()); |
|
107 | 2 | break; |
|
108 | case PropertyProperty::class: |
||
109 | $this->parts->push('::$' . $node->name); |
||
110 | $node->fqsen = new Fqsen($this->buildName()); |
||
111 | break; |
||
112 | } |
||
113 | 4 | } |
|
114 | |||
115 | /** |
||
116 | * Resets the state of the object to an empty state. |
||
117 | * |
||
118 | * @param string $namespace |
||
119 | */ |
||
120 | 5 | private function resetState($namespace = null) |
|
125 | |||
126 | /** |
||
127 | * Builds the name of the current node using the parts that are pushed to the parts list. |
||
128 | * |
||
129 | * @return null|string |
||
130 | */ |
||
131 | 4 | private function buildName() |
|
139 | } |
||
140 |