1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace ApiGen\Parser\Reflection; |
4
|
|
|
|
5
|
|
|
use ApiGen\Contracts\Parser\Reflection\Behavior\InClassInterface; |
6
|
|
|
use ApiGen\Contracts\Parser\Reflection\ElementReflectionInterface; |
7
|
|
|
use TokenReflection\ReflectionAnnotation; |
8
|
|
|
use TokenReflection\ReflectionClass; |
|
|
|
|
9
|
|
|
use TokenReflection\ReflectionConstant; |
|
|
|
|
10
|
|
|
use TokenReflection\ReflectionFunction; |
|
|
|
|
11
|
|
|
|
12
|
|
|
abstract class AbstractReflectionElement extends AbstractReflection implements ElementReflectionInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var bool |
16
|
|
|
*/ |
17
|
|
|
protected $isDocumented; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var mixed[] |
21
|
|
|
*/ |
22
|
|
|
protected $annotations; |
23
|
|
|
|
24
|
|
|
public function getStartPosition(): int |
25
|
|
|
{ |
26
|
|
|
return $this->reflection->getStartPosition(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function getEndPosition(): int |
30
|
|
|
{ |
31
|
|
|
return $this->reflection->getEndPosition(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function isDocumented(): bool |
35
|
|
|
{ |
36
|
|
|
if ($this->isDocumented === null) { |
37
|
|
|
$this->isDocumented = $this->reflection->isTokenized() || $this->reflection->isInternal(); |
38
|
|
|
|
39
|
|
|
if ($this->isDocumented) { |
40
|
|
|
if ($this->reflection->isInternal()) { |
41
|
|
|
$this->isDocumented = false; |
42
|
|
|
} elseif ($this->reflection->hasAnnotation('internal')) { |
43
|
|
|
$this->isDocumented = false; |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return $this->isDocumented; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function isDeprecated(): bool |
52
|
|
|
{ |
53
|
|
|
if ($this->reflection->isDeprecated()) { |
54
|
|
|
return true; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if ($this instanceof InClassInterface) { |
58
|
|
|
$class = $this->getDeclaringClass(); |
|
|
|
|
59
|
|
|
return !is_null($class) && $class->isDeprecated(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return false; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function inNamespace(): bool |
66
|
|
|
{ |
67
|
|
|
return $this->getNamespaceName() !== ''; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function getNamespaceName(): string |
71
|
|
|
{ |
72
|
|
|
static $namespaces = []; |
73
|
|
|
|
74
|
|
|
$namespaceName = $this->reflection->getNamespaceName(); |
75
|
|
|
|
76
|
|
|
if (! $namespaceName) { |
77
|
|
|
return $namespaceName; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$lowerNamespaceName = strtolower($namespaceName); |
81
|
|
|
if (! isset($namespaces[$lowerNamespaceName])) { |
82
|
|
|
$namespaces[$lowerNamespaceName] = $namespaceName; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $namespaces[$lowerNamespaceName]; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function getPseudoNamespaceName(): string |
89
|
|
|
{ |
90
|
|
|
return $this->isInternal() ? 'PHP' : $this->getNamespaceName() ?: 'None'; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return string[] |
95
|
|
|
*/ |
96
|
|
|
public function getNamespaceAliases(): array |
97
|
|
|
{ |
98
|
|
|
return $this->reflection->getNamespaceAliases(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function getDescription(): string |
102
|
|
|
{ |
103
|
|
|
$short = $this->getShortDescription(); |
104
|
|
|
$long = $this->reflection->getAnnotation(ReflectionAnnotation::LONG_DESCRIPTION); |
105
|
|
|
|
106
|
|
|
if (! empty($long)) { |
107
|
|
|
$short .= "\n\n" . $long; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $short; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function getDocComment(): string |
114
|
|
|
{ |
115
|
|
|
return (string) $this->reflection->getDocComment(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return mixed[] |
120
|
|
|
*/ |
121
|
|
|
public function getAnnotations(): array |
122
|
|
|
{ |
123
|
|
|
if ($this->annotations === null) { |
124
|
|
|
$annotations = $this->reflection->getAnnotations(); |
125
|
|
|
$annotations = array_change_key_case($annotations, CASE_LOWER); |
126
|
|
|
|
127
|
|
|
unset($annotations[ReflectionAnnotation::SHORT_DESCRIPTION]); |
128
|
|
|
unset($annotations[ReflectionAnnotation::LONG_DESCRIPTION]); |
129
|
|
|
|
130
|
|
|
$annotations += $this->getAnnotationsFromReflection($this->reflection); |
131
|
|
|
$this->annotations = $annotations; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $this->annotations; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @return mixed[] |
139
|
|
|
*/ |
140
|
|
|
public function getAnnotation(string $name): array |
141
|
|
|
{ |
142
|
|
|
return $this->hasAnnotation($name) ? $this->getAnnotations()[$name] : []; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function hasAnnotation(string $name): bool |
146
|
|
|
{ |
147
|
|
|
return isset($this->getAnnotations()[$name]); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param mixed $reflection |
152
|
|
|
* @return mixed[] |
153
|
|
|
*/ |
154
|
|
|
private function getAnnotationsFromReflection($reflection): array |
155
|
|
|
{ |
156
|
|
|
$fileLevel = [ |
157
|
|
|
'package' => true, |
158
|
|
|
'subpackage' => true, |
159
|
|
|
'author' => true, |
160
|
|
|
'license' => true, |
161
|
|
|
'copyright' => true |
162
|
|
|
]; |
163
|
|
|
|
164
|
|
|
$annotations = []; |
165
|
|
|
if ($reflection instanceof ReflectionClass || $reflection instanceof ReflectionFunction |
|
|
|
|
166
|
|
|
|| ($reflection instanceof ReflectionConstant && $reflection->getDeclaringClassName() === '') |
|
|
|
|
167
|
|
|
) { |
168
|
|
|
foreach ($reflection->getFileReflection()->getAnnotations() as $name => $value) { |
169
|
|
|
if (isset($fileLevel[$name]) && empty($annotations[$name])) { |
170
|
|
|
$annotations[$name] = $value; |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
return $annotations; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
private function getShortDescription(): string |
179
|
|
|
{ |
180
|
|
|
$short = $this->reflection->getAnnotation(ReflectionAnnotation::SHORT_DESCRIPTION); |
181
|
|
|
if (! empty($short)) { |
182
|
|
|
return $short; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
if ($this instanceof ReflectionProperty || $this instanceof ReflectionConstant) { |
|
|
|
|
186
|
|
|
$var = $this->getAnnotation('var'); |
187
|
|
|
[, $short] = preg_split('~\s+|$~', $var[0], 2); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
return (string) $short; |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: