|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the ApiGen (http://apigen.org) |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view |
|
7
|
|
|
* the file LICENSE that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace ApiGen\Parser\Reflection; |
|
11
|
|
|
|
|
12
|
|
|
use ApiGen\Contracts\Parser\Reflection\Behavior\InClassInterface; |
|
13
|
|
|
use ApiGen\Contracts\Parser\Reflection\ElementReflectionInterface; |
|
14
|
|
|
use TokenReflection; |
|
15
|
|
|
use TokenReflection\Exception\BaseException; |
|
16
|
|
|
use TokenReflection\ReflectionAnnotation; |
|
17
|
|
|
use TokenReflection\ReflectionClass; |
|
|
|
|
|
|
18
|
|
|
use TokenReflection\ReflectionConstant; |
|
|
|
|
|
|
19
|
|
|
use TokenReflection\ReflectionFunction; |
|
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
abstract class ReflectionElement extends ReflectionBase implements ElementReflectionInterface |
|
22
|
|
|
{ |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var bool |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $isDocumented; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var array |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $annotations; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Reasons why this element's reflection is invalid. |
|
36
|
|
|
* |
|
37
|
|
|
* @var array |
|
38
|
|
|
*/ |
|
39
|
|
|
private $reasons = []; |
|
40
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @return ReflectionExtension|NULL |
|
44
|
|
|
*/ |
|
45
|
|
|
public function getExtension() |
|
46
|
|
|
{ |
|
47
|
|
|
$extension = $this->reflection->getExtension(); |
|
48
|
|
|
return $extension === null ? null : $this->reflectionFactory->createFromReflection($extension); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @return bool |
|
54
|
|
|
*/ |
|
55
|
|
|
public function getExtensionName() |
|
56
|
|
|
{ |
|
57
|
|
|
return $this->reflection->getExtensionName(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* {@inheritdoc} |
|
63
|
|
|
*/ |
|
64
|
|
|
public function getStartPosition() |
|
65
|
|
|
{ |
|
66
|
|
|
return $this->reflection->getStartPosition(); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* {@inheritdoc} |
|
72
|
|
|
*/ |
|
73
|
|
|
public function getEndPosition() |
|
74
|
|
|
{ |
|
75
|
|
|
return $this->reflection->getEndPosition(); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* {@inheritdoc} |
|
81
|
|
|
*/ |
|
82
|
|
|
public function isMain() |
|
83
|
|
|
{ |
|
84
|
|
|
$main = $this->configuration->getMain(); |
|
85
|
|
|
return empty($main) || strpos($this->getName(), $main) === 0; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* {@inheritdoc} |
|
91
|
|
|
*/ |
|
92
|
|
|
public function isDocumented() |
|
93
|
|
|
{ |
|
94
|
|
|
if ($this->isDocumented === null) { |
|
95
|
|
|
$this->isDocumented = $this->reflection->isTokenized() || $this->reflection->isInternal(); |
|
96
|
|
|
|
|
97
|
|
|
if ($this->isDocumented) { |
|
98
|
|
|
$php = $this->configuration->isPhpCoreDocumented(); |
|
99
|
|
|
$internal = $this->configuration->isInternalDocumented(); |
|
100
|
|
|
|
|
101
|
|
|
if (! $php && $this->reflection->isInternal()) { |
|
102
|
|
|
$this->isDocumented = false; |
|
103
|
|
|
} elseif (! $internal && $this->reflection->hasAnnotation('internal')) { |
|
104
|
|
|
$this->isDocumented = false; |
|
105
|
|
|
} elseif ($this->reflection->hasAnnotation('ignore')) { |
|
106
|
|
|
$this->isDocumented = false; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return $this->isDocumented; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* {@inheritdoc} |
|
117
|
|
|
*/ |
|
118
|
|
|
public function isDeprecated() |
|
119
|
|
|
{ |
|
120
|
|
|
if ($this->reflection->isDeprecated()) { |
|
121
|
|
|
return true; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
if ($this instanceof InClassInterface) { |
|
125
|
|
|
$class = $this->getDeclaringClass(); |
|
|
|
|
|
|
126
|
|
|
return !is_null($class) && $class->isDeprecated(); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
return false; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* {@inheritdoc} |
|
135
|
|
|
*/ |
|
136
|
|
|
public function inPackage() |
|
137
|
|
|
{ |
|
138
|
|
|
return ($this->getPackageName() !== ''); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* {@inheritdoc} |
|
144
|
|
|
*/ |
|
145
|
|
|
public function getPackageName() |
|
146
|
|
|
{ |
|
147
|
|
|
static $packages = []; |
|
148
|
|
|
|
|
149
|
|
|
if ($package = $this->getAnnotation('package')) { |
|
150
|
|
|
$packageName = preg_replace('~\s+.*~s', '', $package[0]); |
|
151
|
|
|
if (empty($packageName)) { |
|
152
|
|
|
return ''; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
if ($subpackage = $this->getAnnotation('subpackage')) { |
|
156
|
|
|
$subpackageName = preg_replace('~\s+.*~s', '', $subpackage[0]); |
|
157
|
|
|
if (! empty($subpackageName) && strpos($subpackageName, $packageName) === 0) { |
|
158
|
|
|
$packageName = $subpackageName; |
|
159
|
|
|
} else { |
|
160
|
|
|
$packageName .= '\\' . $subpackageName; |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
$packageName = strtr($packageName, '._/', '\\\\\\'); |
|
164
|
|
|
|
|
165
|
|
|
$lowerPackageName = strtolower($packageName); |
|
166
|
|
|
if (! isset($packages[$lowerPackageName])) { |
|
167
|
|
|
$packages[$lowerPackageName] = $packageName; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
return $packages[$lowerPackageName]; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
return ''; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* {@inheritdoc} |
|
179
|
|
|
*/ |
|
180
|
|
|
public function getPseudoPackageName() |
|
181
|
|
|
{ |
|
182
|
|
|
if ($this->isInternal()) { |
|
183
|
|
|
return 'PHP'; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
return $this->getPackageName() ?: 'None'; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* {@inheritdoc} |
|
192
|
|
|
*/ |
|
193
|
|
|
public function inNamespace() |
|
194
|
|
|
{ |
|
195
|
|
|
return $this->getNamespaceName() !== ''; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* {@inheritdoc} |
|
201
|
|
|
*/ |
|
202
|
|
|
public function getNamespaceName() |
|
203
|
|
|
{ |
|
204
|
|
|
static $namespaces = []; |
|
205
|
|
|
|
|
206
|
|
|
$namespaceName = $this->reflection->getNamespaceName(); |
|
207
|
|
|
|
|
208
|
|
|
if (! $namespaceName) { |
|
209
|
|
|
return $namespaceName; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
$lowerNamespaceName = strtolower($namespaceName); |
|
213
|
|
|
if (! isset($namespaces[$lowerNamespaceName])) { |
|
214
|
|
|
$namespaces[$lowerNamespaceName] = $namespaceName; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
return $namespaces[$lowerNamespaceName]; |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* {@inheritdoc} |
|
223
|
|
|
*/ |
|
224
|
|
|
public function getPseudoNamespaceName() |
|
225
|
|
|
{ |
|
226
|
|
|
return $this->isInternal() ? 'PHP' : $this->getNamespaceName() ?: 'None'; |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* {@inheritdoc} |
|
232
|
|
|
*/ |
|
233
|
|
|
public function getNamespaceAliases() |
|
234
|
|
|
{ |
|
235
|
|
|
return $this->reflection->getNamespaceAliases(); |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* {@inheritdoc} |
|
241
|
|
|
*/ |
|
242
|
|
|
public function getShortDescription() |
|
243
|
|
|
{ |
|
244
|
|
|
$short = $this->reflection->getAnnotation(ReflectionAnnotation::SHORT_DESCRIPTION); |
|
245
|
|
|
if (! empty($short)) { |
|
246
|
|
|
return $short; |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
if ($this instanceof ReflectionProperty || $this instanceof ReflectionConstant) { |
|
|
|
|
|
|
250
|
|
|
$var = $this->getAnnotation('var'); |
|
251
|
|
|
list(, $short) = preg_split('~\s+|$~', $var[0], 2); |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
return $short; |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
|
|
258
|
|
|
/** |
|
259
|
|
|
* {@inheritdoc} |
|
260
|
|
|
*/ |
|
261
|
|
|
public function getLongDescription() |
|
262
|
|
|
{ |
|
263
|
|
|
$short = $this->getShortDescription(); |
|
264
|
|
|
$long = $this->reflection->getAnnotation(ReflectionAnnotation::LONG_DESCRIPTION); |
|
265
|
|
|
|
|
266
|
|
|
if (! empty($long)) { |
|
267
|
|
|
$short .= "\n\n" . $long; |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
return $short; |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
|
|
274
|
|
|
/** |
|
275
|
|
|
* {@inheritdoc} |
|
276
|
|
|
*/ |
|
277
|
|
|
public function getDocComment() |
|
278
|
|
|
{ |
|
279
|
|
|
return $this->reflection->getDocComment(); |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
|
|
283
|
|
|
/** |
|
284
|
|
|
* {@inheritdoc} |
|
285
|
|
|
*/ |
|
286
|
|
|
public function getAnnotations() |
|
287
|
|
|
{ |
|
288
|
|
|
if ($this->annotations === null) { |
|
289
|
|
|
$annotations = $this->reflection->getAnnotations(); |
|
290
|
|
|
$annotations = array_change_key_case($annotations, CASE_LOWER); |
|
291
|
|
|
|
|
292
|
|
|
unset($annotations[ReflectionAnnotation::SHORT_DESCRIPTION]); |
|
293
|
|
|
unset($annotations[ReflectionAnnotation::LONG_DESCRIPTION]); |
|
294
|
|
|
|
|
295
|
|
|
$annotations += $this->getAnnotationsFromReflection($this->reflection); |
|
296
|
|
|
$this->annotations = $annotations; |
|
297
|
|
|
} |
|
298
|
|
|
|
|
299
|
|
|
return $this->annotations; |
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
|
|
|
|
303
|
|
|
/** |
|
304
|
|
|
* {@inheritdoc} |
|
305
|
|
|
*/ |
|
306
|
|
|
public function getAnnotation($name) |
|
307
|
|
|
{ |
|
308
|
|
|
return $this->hasAnnotation($name) ? $this->getAnnotations()[$name] : null; |
|
309
|
|
|
} |
|
310
|
|
|
|
|
311
|
|
|
|
|
312
|
|
|
/** |
|
313
|
|
|
* {@inheritdoc} |
|
314
|
|
|
*/ |
|
315
|
|
|
public function hasAnnotation($name) |
|
316
|
|
|
{ |
|
317
|
|
|
$annotations = $this->getAnnotations(); |
|
318
|
|
|
return isset($annotations[$name]); |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
|
|
|
|
322
|
|
|
/** |
|
323
|
|
|
* {@inheritdoc} |
|
324
|
|
|
*/ |
|
325
|
|
|
public function addAnnotation($annotation, $value) |
|
326
|
|
|
{ |
|
327
|
|
|
if ($this->annotations === null) { |
|
328
|
|
|
$this->getAnnotations(); |
|
329
|
|
|
} |
|
330
|
|
|
$this->annotations[$annotation][] = $value; |
|
331
|
|
|
|
|
332
|
|
|
return $this; |
|
333
|
|
|
} |
|
334
|
|
|
|
|
335
|
|
|
|
|
336
|
|
|
/** |
|
337
|
|
|
* {@inheritdoc} |
|
338
|
|
|
*/ |
|
339
|
|
|
public function addReason(BaseException $reason) |
|
340
|
|
|
{ |
|
341
|
|
|
$this->reasons[] = $reason; |
|
342
|
|
|
return $this; |
|
343
|
|
|
} |
|
344
|
|
|
|
|
345
|
|
|
|
|
346
|
|
|
/** |
|
347
|
|
|
* {@inheritdoc} |
|
348
|
|
|
*/ |
|
349
|
|
|
public function getReasons() |
|
350
|
|
|
{ |
|
351
|
|
|
return $this->reasons; |
|
352
|
|
|
} |
|
353
|
|
|
|
|
354
|
|
|
|
|
355
|
|
|
/** |
|
356
|
|
|
* {@inheritdoc} |
|
357
|
|
|
*/ |
|
358
|
|
|
public function hasReasons() |
|
359
|
|
|
{ |
|
360
|
|
|
return ! empty($this->reasons); |
|
361
|
|
|
} |
|
362
|
|
|
|
|
363
|
|
|
|
|
364
|
|
|
/** |
|
365
|
|
|
* @param mixed $reflection |
|
366
|
|
|
* @return array |
|
367
|
|
|
*/ |
|
368
|
|
|
private function getAnnotationsFromReflection($reflection) |
|
369
|
|
|
{ |
|
370
|
|
|
$fileLevel = [ |
|
371
|
|
|
'package' => true, |
|
372
|
|
|
'subpackage' => true, |
|
373
|
|
|
'author' => true, |
|
374
|
|
|
'license' => true, |
|
375
|
|
|
'copyright' => true |
|
376
|
|
|
]; |
|
377
|
|
|
|
|
378
|
|
|
$annotations = []; |
|
379
|
|
|
if ($reflection instanceof ReflectionClass || $reflection instanceof ReflectionFunction |
|
|
|
|
|
|
380
|
|
|
|| ($reflection instanceof ReflectionConstant && $reflection->getDeclaringClassName() === null) |
|
|
|
|
|
|
381
|
|
|
) { |
|
382
|
|
|
foreach ($reflection->getFileReflection()->getAnnotations() as $name => $value) { |
|
383
|
|
|
if (isset($fileLevel[$name]) && empty($annotations[$name])) { |
|
384
|
|
|
$annotations[$name] = $value; |
|
385
|
|
|
} |
|
386
|
|
|
} |
|
387
|
|
|
} |
|
388
|
|
|
return $annotations; |
|
389
|
|
|
} |
|
390
|
|
|
} |
|
391
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare 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.phpHowever, as
OtherDir/Foo.phpdoes 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: