1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of phpDocumentor. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
* |
8
|
|
|
* @copyright 2010-2015 Mike van Riel<[email protected]> |
9
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT |
10
|
|
|
* @link http://phpdoc.org |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
namespace phpDocumentor\Reflection\NodeVisitor; |
15
|
|
|
|
16
|
|
|
use phpDocumentor\Reflection\Fqsen; |
17
|
|
|
use PhpParser\Node; |
18
|
|
|
use PhpParser\Node\Const_; |
19
|
|
|
use PhpParser\Node\Name; |
20
|
|
|
use PhpParser\Node\Stmt\Class_; |
21
|
|
|
use PhpParser\Node\Stmt\ClassConst; |
22
|
|
|
use PhpParser\Node\Stmt\ClassMethod; |
23
|
|
|
use PhpParser\Node\Stmt\Function_; |
24
|
|
|
use PhpParser\Node\Stmt\Interface_; |
25
|
|
|
use PhpParser\Node\Stmt\Namespace_; |
26
|
|
|
use PhpParser\Node\Stmt\PropertyProperty; |
27
|
|
|
use PhpParser\Node\Stmt\Trait_; |
28
|
|
|
use PhpParser\NodeTraverser; |
29
|
|
|
use PhpParser\NodeVisitorAbstract; |
30
|
|
|
|
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) |
44
|
|
|
{ |
45
|
|
|
$this->resetState('\\'); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Performs a reset of the added element when needed. |
50
|
|
|
* |
51
|
|
|
* @param Node $node |
52
|
|
|
*/ |
53
|
1 |
|
public function leaveNode(Node $node) |
54
|
|
|
{ |
55
|
1 |
|
switch (get_class($node)) { |
56
|
1 |
|
case Namespace_::class: |
57
|
1 |
|
case Class_::class: |
58
|
1 |
|
case ClassMethod::class: |
59
|
1 |
|
case Trait_::class: |
60
|
1 |
|
case PropertyProperty::class: |
61
|
1 |
|
case ClassConst::class: |
62
|
1 |
|
case Const_::class: |
63
|
1 |
|
case Interface_::class: |
64
|
1 |
|
case Function_::class: |
65
|
1 |
|
$this->parts->pop(); |
66
|
1 |
|
break; |
67
|
1 |
|
} |
68
|
1 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Adds fqsen property to a node when applicable. |
72
|
|
|
* |
73
|
|
|
* @param Node $node |
74
|
|
|
*/ |
75
|
6 |
|
public function enterNode(Node $node) |
76
|
|
|
{ |
77
|
6 |
|
switch (get_class($node)) { |
78
|
6 |
|
case Namespace_::class: |
79
|
2 |
|
$this->resetState('\\' . $node->name . '\\'); |
|
|
|
|
80
|
2 |
|
$node->fqsen = new Fqsen($this->buildName()); |
|
|
|
|
81
|
2 |
|
break; |
82
|
6 |
|
case Class_::class: |
83
|
6 |
|
case Trait_::class: |
84
|
6 |
|
case Interface_::class: |
85
|
4 |
|
$this->parts->push((string)$node->name); |
|
|
|
|
86
|
|
|
|
87
|
4 |
|
if (is_null($node->name)) { |
|
|
|
|
88
|
2 |
|
return NodeTraverser::DONT_TRAVERSE_CHILDREN; |
89
|
|
|
} |
90
|
|
|
|
91
|
2 |
|
$node->fqsen = new Fqsen($this->buildName()); |
|
|
|
|
92
|
2 |
|
break; |
93
|
4 |
|
case Function_::class: |
94
|
1 |
|
$this->parts->push($node->name . '()'); |
|
|
|
|
95
|
1 |
|
$node->fqsen = new Fqsen($this->buildName()); |
|
|
|
|
96
|
1 |
|
break; |
97
|
3 |
|
case ClassMethod::class: |
98
|
|
|
$this->parts->push('::' . $node->name . '()'); |
|
|
|
|
99
|
|
|
$node->fqsen = new Fqsen($this->buildName()); |
|
|
|
|
100
|
|
|
break; |
101
|
3 |
|
case ClassConst::class: |
102
|
1 |
|
$this->parts->push('::'); |
103
|
1 |
|
break; |
104
|
3 |
|
case Const_::class: |
105
|
2 |
|
$this->parts->push($node->name); |
|
|
|
|
106
|
2 |
|
$node->fqsen = new Fqsen($this->buildName()); |
|
|
|
|
107
|
2 |
|
break; |
108
|
1 |
|
case PropertyProperty::class: |
109
|
|
|
$this->parts->push('::$' . $node->name); |
|
|
|
|
110
|
|
|
$node->fqsen = new Fqsen($this->buildName()); |
|
|
|
|
111
|
|
|
break; |
112
|
5 |
|
} |
113
|
5 |
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Resets the state of the object to an empty state. |
117
|
|
|
* |
118
|
|
|
* @param string $namespace |
119
|
|
|
*/ |
120
|
6 |
|
private function resetState($namespace = null) |
121
|
|
|
{ |
122
|
6 |
|
$this->parts = new \SplDoublyLinkedList(); |
123
|
6 |
|
$this->parts->push($namespace); |
124
|
6 |
|
} |
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
|
5 |
|
private function buildName() |
132
|
|
|
{ |
133
|
5 |
|
$name = null; |
134
|
5 |
|
foreach ($this->parts as $part) { |
135
|
5 |
|
$name .= $part; |
136
|
5 |
|
} |
137
|
5 |
|
return rtrim($name, '\\'); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|