|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Doctrine\Annotations; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Annotations\Annotation\Attribute; |
|
6
|
|
|
use Doctrine\Annotations\Metadata\AnnotationTarget; |
|
7
|
|
|
use Doctrine\Annotations\Metadata\Builder\AnnotationMetadataBuilder; |
|
8
|
|
|
use Doctrine\Annotations\Metadata\Builder\PropertyMetadataBuilder; |
|
9
|
|
|
use Doctrine\Annotations\Metadata\InternalAnnotations; |
|
10
|
|
|
use Doctrine\Annotations\Metadata\MetadataCollection; |
|
11
|
|
|
use Doctrine\Annotations\Metadata\TransientMetadataCollection; |
|
12
|
|
|
use ReflectionClass; |
|
13
|
|
|
use Doctrine\Annotations\Annotation\Enum; |
|
14
|
|
|
use Doctrine\Annotations\Annotation\Target; |
|
15
|
|
|
use Doctrine\Annotations\Annotation\Attributes; |
|
16
|
|
|
use function array_key_exists; |
|
17
|
|
|
use function array_keys; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* A parser for docblock annotations. |
|
21
|
|
|
* |
|
22
|
|
|
* It is strongly discouraged to change the default annotation parsing process. |
|
23
|
|
|
* |
|
24
|
|
|
* @author Benjamin Eberlei <[email protected]> |
|
25
|
|
|
* @author Guilherme Blanco <[email protected]> |
|
26
|
|
|
* @author Jonathan Wage <[email protected]> |
|
27
|
|
|
* @author Roman Borschel <[email protected]> |
|
28
|
|
|
* @author Johannes M. Schmitt <[email protected]> |
|
29
|
|
|
* @author Fabio B. Silva <[email protected]> |
|
30
|
|
|
*/ |
|
31
|
|
|
final class DocParser |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* An array of all valid tokens for a class name. |
|
35
|
|
|
* |
|
36
|
|
|
* @var array |
|
37
|
|
|
*/ |
|
38
|
|
|
private static $classIdentifiers = [ |
|
39
|
|
|
DocLexer::T_IDENTIFIER, |
|
40
|
|
|
DocLexer::T_TRUE, |
|
41
|
|
|
DocLexer::T_FALSE, |
|
42
|
|
|
DocLexer::T_NULL |
|
43
|
|
|
]; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* The lexer. |
|
47
|
|
|
* |
|
48
|
|
|
* @var \Doctrine\Annotations\DocLexer |
|
49
|
|
|
*/ |
|
50
|
|
|
private $lexer; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Current target context. |
|
54
|
|
|
* |
|
55
|
|
|
* @var integer |
|
56
|
|
|
*/ |
|
57
|
|
|
private $target; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Doc parser used to collect annotation target. |
|
61
|
|
|
* |
|
62
|
|
|
* @var \Doctrine\Annotations\DocParser |
|
63
|
|
|
*/ |
|
64
|
|
|
private static $metadataParser; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Flag to control if the current annotation is nested or not. |
|
68
|
|
|
* |
|
69
|
|
|
* @var boolean |
|
70
|
|
|
*/ |
|
71
|
|
|
private $isNestedAnnotation = false; |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Hashmap containing all use-statements that are to be used when parsing |
|
75
|
|
|
* the given doc block. |
|
76
|
|
|
* |
|
77
|
|
|
* @var array |
|
78
|
|
|
*/ |
|
79
|
|
|
private $imports = []; |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* This hashmap is used internally to cache results of class_exists() |
|
83
|
|
|
* look-ups. |
|
84
|
|
|
* |
|
85
|
|
|
* @var array |
|
86
|
|
|
*/ |
|
87
|
|
|
private $classExists = []; |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Whether annotations that have not been imported should be ignored. |
|
91
|
|
|
* |
|
92
|
|
|
* @var boolean |
|
93
|
|
|
*/ |
|
94
|
|
|
private $ignoreNotImportedAnnotations = false; |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* An array of default namespaces if operating in simple mode. |
|
98
|
|
|
* |
|
99
|
|
|
* @var string[] |
|
100
|
|
|
*/ |
|
101
|
|
|
private $namespaces = []; |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* A list with annotations that are not causing exceptions when not resolved to an annotation class. |
|
105
|
|
|
* |
|
106
|
|
|
* The names must be the raw names as used in the class, not the fully qualified |
|
107
|
|
|
* class names. |
|
108
|
|
|
* |
|
109
|
|
|
* @var bool[] indexed by annotation name |
|
110
|
|
|
*/ |
|
111
|
|
|
private $ignoredAnnotationNames = []; |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* A list with annotations in namespaced format |
|
115
|
|
|
* that are not causing exceptions when not resolved to an annotation class. |
|
116
|
|
|
* |
|
117
|
|
|
* @var bool[] indexed by namespace name |
|
118
|
|
|
*/ |
|
119
|
|
|
private $ignoredAnnotationNamespaces = []; |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @var string |
|
123
|
|
|
*/ |
|
124
|
|
|
private $context = ''; |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Hash-map for caching annotation metadata. |
|
128
|
|
|
* |
|
129
|
|
|
* @var MetadataCollection |
|
130
|
|
|
*/ |
|
131
|
|
|
private $metadata; |
|
132
|
|
|
|
|
133
|
|
|
/** @var array<string, bool> */ |
|
134
|
|
|
private $nonAnnotationClasses = []; |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Hash-map for handle types declaration. |
|
138
|
|
|
* |
|
139
|
|
|
* @var array |
|
140
|
|
|
*/ |
|
141
|
|
|
private static $typeMap = [ |
|
142
|
|
|
'float' => 'double', |
|
143
|
|
|
'bool' => 'boolean', |
|
144
|
|
|
// allow uppercase Boolean in honor of George Boole |
|
145
|
|
|
'Boolean' => 'boolean', |
|
146
|
|
|
'int' => 'integer', |
|
147
|
|
|
]; |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Constructs a new DocParser. |
|
151
|
|
|
*/ |
|
152
|
290 |
|
public function __construct() |
|
153
|
|
|
{ |
|
154
|
290 |
|
$this->lexer = new DocLexer; |
|
155
|
290 |
|
$this->metadata = InternalAnnotations::createMetadata(); |
|
156
|
290 |
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Sets the annotation names that are ignored during the parsing process. |
|
160
|
|
|
* |
|
161
|
|
|
* The names are supposed to be the raw names as used in the class, not the |
|
162
|
|
|
* fully qualified class names. |
|
163
|
|
|
* |
|
164
|
|
|
* @param bool[] $names indexed by annotation name |
|
165
|
|
|
* |
|
166
|
|
|
* @return void |
|
167
|
|
|
*/ |
|
168
|
47 |
|
public function setIgnoredAnnotationNames(array $names) |
|
169
|
|
|
{ |
|
170
|
47 |
|
$this->ignoredAnnotationNames = $names; |
|
171
|
47 |
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Sets the annotation namespaces that are ignored during the parsing process. |
|
175
|
|
|
* |
|
176
|
|
|
* @param bool[] $ignoredAnnotationNamespaces indexed by annotation namespace name |
|
177
|
|
|
* |
|
178
|
|
|
* @return void |
|
179
|
|
|
*/ |
|
180
|
58 |
|
public function setIgnoredAnnotationNamespaces($ignoredAnnotationNamespaces) |
|
181
|
|
|
{ |
|
182
|
58 |
|
$this->ignoredAnnotationNamespaces = $ignoredAnnotationNamespaces; |
|
183
|
58 |
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Sets ignore on not-imported annotations. |
|
187
|
|
|
* |
|
188
|
|
|
* @param boolean $bool |
|
189
|
|
|
* |
|
190
|
|
|
* @return void |
|
191
|
|
|
*/ |
|
192
|
270 |
|
public function setIgnoreNotImportedAnnotations($bool) |
|
193
|
|
|
{ |
|
194
|
270 |
|
$this->ignoreNotImportedAnnotations = (boolean) $bool; |
|
195
|
270 |
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Sets the default namespaces. |
|
199
|
|
|
* |
|
200
|
|
|
* @param string $namespace |
|
201
|
|
|
* |
|
202
|
|
|
* @return void |
|
203
|
|
|
* |
|
204
|
|
|
* @throws \RuntimeException |
|
205
|
|
|
*/ |
|
206
|
2 |
|
public function addNamespace($namespace) |
|
207
|
|
|
{ |
|
208
|
2 |
|
if ($this->imports) { |
|
|
|
|
|
|
209
|
|
|
throw new \RuntimeException('You must either use addNamespace(), or setImports(), but not both.'); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
2 |
|
$this->namespaces[] = $namespace; |
|
213
|
2 |
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* Sets the imports. |
|
217
|
|
|
* |
|
218
|
|
|
* @param array $imports |
|
219
|
|
|
* |
|
220
|
|
|
* @return void |
|
221
|
|
|
* |
|
222
|
|
|
* @throws \RuntimeException |
|
223
|
|
|
*/ |
|
224
|
269 |
|
public function setImports(array $imports) |
|
225
|
|
|
{ |
|
226
|
269 |
|
if ($this->namespaces) { |
|
|
|
|
|
|
227
|
|
|
throw new \RuntimeException('You must either use addNamespace(), or setImports(), but not both.'); |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
269 |
|
$this->imports = $imports; |
|
231
|
269 |
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* Sets current target context as bitmask. |
|
235
|
|
|
* |
|
236
|
|
|
* @param integer $target |
|
237
|
|
|
* |
|
238
|
|
|
* @return void |
|
239
|
|
|
*/ |
|
240
|
271 |
|
public function setTarget($target) |
|
241
|
|
|
{ |
|
242
|
271 |
|
$this->target = $target; |
|
243
|
271 |
|
} |
|
244
|
|
|
|
|
245
|
|
|
/** |
|
246
|
|
|
* Parses the given docblock string for annotations. |
|
247
|
|
|
* |
|
248
|
|
|
* @param string $input The docblock string to parse. |
|
249
|
|
|
* @param string $context The parsing context. |
|
250
|
|
|
* |
|
251
|
|
|
* @return array Array of annotations. If no annotations are found, an empty array is returned. |
|
252
|
|
|
*/ |
|
253
|
290 |
|
public function parse($input, $context = '') |
|
254
|
|
|
{ |
|
255
|
290 |
|
$pos = $this->findInitialTokenPosition($input); |
|
256
|
290 |
|
if ($pos === null) { |
|
257
|
19 |
|
return []; |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
289 |
|
$this->context = $context; |
|
261
|
|
|
|
|
262
|
289 |
|
$this->lexer->setInput(trim(substr($input, $pos), '* /')); |
|
263
|
289 |
|
$this->lexer->moveNext(); |
|
264
|
|
|
|
|
265
|
289 |
|
return $this->Annotations(); |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
/** |
|
269
|
|
|
* Finds the first valid annotation |
|
270
|
|
|
* |
|
271
|
|
|
* @param string $input The docblock string to parse |
|
272
|
|
|
* |
|
273
|
|
|
* @return int|null |
|
274
|
|
|
*/ |
|
275
|
290 |
|
private function findInitialTokenPosition($input) |
|
276
|
|
|
{ |
|
277
|
290 |
|
$pos = 0; |
|
278
|
|
|
|
|
279
|
|
|
// search for first valid annotation |
|
280
|
290 |
|
while (($pos = strpos($input, '@', $pos)) !== false) { |
|
281
|
290 |
|
$preceding = substr($input, $pos - 1, 1); |
|
282
|
|
|
|
|
283
|
|
|
// if the @ is preceded by a space, a tab or * it is valid |
|
284
|
290 |
|
if ($pos === 0 || $preceding === ' ' || $preceding === '*' || $preceding === "\t") { |
|
285
|
289 |
|
return $pos; |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
2 |
|
$pos++; |
|
289
|
|
|
} |
|
290
|
|
|
|
|
291
|
19 |
|
return null; |
|
292
|
|
|
} |
|
293
|
|
|
|
|
294
|
|
|
/** |
|
295
|
|
|
* Attempts to match the given token with the current lookahead token. |
|
296
|
|
|
* If they match, updates the lookahead token; otherwise raises a syntax error. |
|
297
|
|
|
* |
|
298
|
|
|
* @param integer $token Type of token. |
|
299
|
|
|
* |
|
300
|
|
|
* @return boolean True if tokens match; false otherwise. |
|
301
|
|
|
*/ |
|
302
|
289 |
|
private function match($token) |
|
303
|
|
|
{ |
|
304
|
289 |
|
if ( ! $this->lexer->isNextToken($token) ) { |
|
305
|
|
|
$this->syntaxError($this->lexer->getLiteral($token)); |
|
306
|
|
|
} |
|
307
|
|
|
|
|
308
|
289 |
|
return $this->lexer->moveNext(); |
|
309
|
|
|
} |
|
310
|
|
|
|
|
311
|
|
|
/** |
|
312
|
|
|
* Attempts to match the current lookahead token with any of the given tokens. |
|
313
|
|
|
* |
|
314
|
|
|
* If any of them matches, this method updates the lookahead token; otherwise |
|
315
|
|
|
* a syntax error is raised. |
|
316
|
|
|
* |
|
317
|
|
|
* @param array $tokens |
|
318
|
|
|
* |
|
319
|
|
|
* @return boolean |
|
320
|
|
|
*/ |
|
321
|
14 |
|
private function matchAny(array $tokens) |
|
322
|
|
|
{ |
|
323
|
14 |
|
if ( ! $this->lexer->isNextTokenAny($tokens)) { |
|
324
|
1 |
|
$this->syntaxError(implode(' or ', array_map([$this->lexer, 'getLiteral'], $tokens))); |
|
325
|
|
|
} |
|
326
|
|
|
|
|
327
|
13 |
|
return $this->lexer->moveNext(); |
|
328
|
|
|
} |
|
329
|
|
|
|
|
330
|
|
|
/** |
|
331
|
|
|
* Generates a new syntax error. |
|
332
|
|
|
* |
|
333
|
|
|
* @param string $expected Expected string. |
|
334
|
|
|
* @param array|null $token Optional token. |
|
335
|
|
|
* |
|
336
|
|
|
* @return void |
|
337
|
|
|
* |
|
338
|
|
|
* @throws AnnotationException |
|
339
|
|
|
*/ |
|
340
|
11 |
|
private function syntaxError($expected, $token = null) |
|
341
|
|
|
{ |
|
342
|
11 |
|
if ($token === null) { |
|
343
|
11 |
|
$token = $this->lexer->lookahead; |
|
344
|
|
|
} |
|
345
|
|
|
|
|
346
|
11 |
|
$message = sprintf('Expected %s, got ', $expected); |
|
347
|
11 |
|
$message .= ($this->lexer->lookahead === null) |
|
348
|
|
|
? 'end of string' |
|
349
|
11 |
|
: sprintf("'%s' at position %s", $token['value'], $token['position']); |
|
350
|
|
|
|
|
351
|
11 |
|
if (strlen($this->context)) { |
|
352
|
8 |
|
$message .= ' in ' . $this->context; |
|
353
|
|
|
} |
|
354
|
|
|
|
|
355
|
11 |
|
$message .= '.'; |
|
356
|
|
|
|
|
357
|
11 |
|
throw AnnotationException::syntaxError($message); |
|
358
|
|
|
} |
|
359
|
|
|
|
|
360
|
|
|
/** |
|
361
|
|
|
* Attempts to check if a class exists or not. This always uses PHP autoloading mechanism. |
|
362
|
|
|
* |
|
363
|
|
|
* @param string $fqcn |
|
364
|
|
|
* |
|
365
|
|
|
* @return boolean |
|
366
|
|
|
*/ |
|
367
|
284 |
|
private function classExists($fqcn) |
|
368
|
|
|
{ |
|
369
|
284 |
|
if (isset($this->classExists[$fqcn])) { |
|
370
|
240 |
|
return $this->classExists[$fqcn]; |
|
371
|
|
|
} |
|
372
|
|
|
|
|
373
|
|
|
// final check, does this class exist? |
|
374
|
284 |
|
return $this->classExists[$fqcn] = class_exists($fqcn); |
|
375
|
|
|
} |
|
376
|
|
|
|
|
377
|
|
|
/** |
|
378
|
|
|
* Collects parsing metadata for a given annotation class |
|
379
|
|
|
* |
|
380
|
|
|
* @param string $name The annotation name |
|
381
|
|
|
* |
|
382
|
|
|
* @return void |
|
383
|
|
|
*/ |
|
384
|
263 |
|
private function collectAnnotationMetadata($name) |
|
385
|
|
|
{ |
|
386
|
263 |
|
if (self::$metadataParser === null) { |
|
387
|
1 |
|
self::$metadataParser = new self(); |
|
388
|
|
|
|
|
389
|
1 |
|
self::$metadataParser->setIgnoreNotImportedAnnotations(true); |
|
390
|
1 |
|
self::$metadataParser->setIgnoredAnnotationNames($this->ignoredAnnotationNames); |
|
391
|
1 |
|
self::$metadataParser->setImports([ |
|
392
|
1 |
|
'enum' => 'Doctrine\Annotations\Annotation\Enum', |
|
393
|
|
|
'target' => 'Doctrine\Annotations\Annotation\Target', |
|
394
|
|
|
'attribute' => 'Doctrine\Annotations\Annotation\Attribute', |
|
395
|
|
|
'attributes' => 'Doctrine\Annotations\Annotation\Attributes' |
|
396
|
|
|
]); |
|
397
|
|
|
} |
|
398
|
|
|
|
|
399
|
263 |
|
$class = new ReflectionClass($name); |
|
400
|
263 |
|
$docComment = $class->getDocComment(); |
|
401
|
|
|
|
|
402
|
|
|
// verify that the class is really meant to be an annotation |
|
403
|
263 |
|
if (strpos($docComment, '@Annotation') === false) { |
|
404
|
4 |
|
$this->nonAnnotationClasses[$name] = true; |
|
405
|
4 |
|
return; |
|
406
|
|
|
} |
|
407
|
|
|
|
|
408
|
259 |
|
$constructor = $class->getConstructor(); |
|
409
|
259 |
|
$useConstructor = $constructor !== null && $constructor->getNumberOfParameters() > 0; |
|
410
|
259 |
|
$annotationBuilder = new AnnotationMetadataBuilder($name); |
|
411
|
|
|
|
|
412
|
259 |
|
if ($useConstructor) { |
|
413
|
89 |
|
$annotationBuilder->withUsingConstructor(); |
|
414
|
|
|
} |
|
415
|
|
|
|
|
416
|
259 |
|
self::$metadataParser->setTarget(Target::TARGET_CLASS); |
|
417
|
|
|
|
|
418
|
259 |
|
foreach (self::$metadataParser->parse($docComment, 'class @' . $name) as $annotation) { |
|
419
|
203 |
|
if ($annotation instanceof Target) { |
|
420
|
203 |
|
$annotationBuilder->withTarget(AnnotationTarget::fromAnnotation($annotation)); |
|
421
|
|
|
|
|
422
|
203 |
|
continue; |
|
423
|
|
|
} |
|
424
|
|
|
|
|
425
|
84 |
|
if ($annotation instanceof Attributes) { |
|
426
|
84 |
|
foreach ($annotation->value as $attribute) { |
|
427
|
84 |
|
$propertyBuilder = new PropertyMetadataBuilder($attribute->name); |
|
428
|
|
|
|
|
429
|
84 |
|
$this->collectAttributeTypeMetadata($propertyBuilder, $attribute); |
|
430
|
84 |
|
$annotationBuilder->withProperty($propertyBuilder->build()); |
|
431
|
|
|
} |
|
432
|
|
|
} |
|
433
|
|
|
} |
|
434
|
|
|
|
|
435
|
253 |
|
if ($useConstructor) { |
|
436
|
89 |
|
$this->metadata->add($annotationBuilder->build()); |
|
437
|
|
|
|
|
438
|
89 |
|
return; |
|
439
|
|
|
} |
|
440
|
|
|
|
|
441
|
|
|
// if there is no constructor we will inject values into public properties |
|
442
|
|
|
|
|
443
|
|
|
// collect all public properties |
|
444
|
182 |
|
foreach ($class->getProperties(\ReflectionProperty::IS_PUBLIC) as $i => $property) { |
|
445
|
168 |
|
$propertyBuilder = new PropertyMetadataBuilder($property->getName()); |
|
446
|
168 |
|
$propertyComment = $property->getDocComment(); |
|
447
|
|
|
|
|
448
|
168 |
|
if ($i === 0) { |
|
449
|
168 |
|
$propertyBuilder->withBeingDefault(); |
|
450
|
|
|
} |
|
451
|
|
|
|
|
452
|
|
|
|
|
453
|
168 |
|
if ($propertyComment === false) { |
|
454
|
73 |
|
$annotationBuilder->withProperty($propertyBuilder->build()); |
|
455
|
|
|
|
|
456
|
73 |
|
continue; |
|
457
|
|
|
} |
|
458
|
|
|
|
|
459
|
113 |
|
$attribute = new Attribute(); |
|
460
|
113 |
|
$attribute->required = (false !== strpos($propertyComment, '@Required')); |
|
|
|
|
|
|
461
|
113 |
|
$attribute->name = $property->name; |
|
462
|
113 |
|
$attribute->type = (false !== strpos($propertyComment, '@var') && preg_match('/@var\s+([^\s]+)/',$propertyComment, $matches)) |
|
|
|
|
|
|
463
|
113 |
|
? $matches[1] |
|
464
|
|
|
: 'mixed'; |
|
465
|
|
|
|
|
466
|
113 |
|
$this->collectAttributeTypeMetadata($propertyBuilder, $attribute); |
|
467
|
|
|
|
|
468
|
|
|
// checks if the property has @Enum |
|
469
|
113 |
|
if (false !== strpos($propertyComment, '@Enum')) { |
|
470
|
5 |
|
$context = 'property ' . $class->name . "::\$" . $property->name; |
|
471
|
|
|
|
|
472
|
5 |
|
self::$metadataParser->setTarget(Target::TARGET_PROPERTY); |
|
473
|
|
|
|
|
474
|
5 |
|
foreach (self::$metadataParser->parse($propertyComment, $context) as $annotation) { |
|
|
|
|
|
|
475
|
3 |
|
if ( ! $annotation instanceof Enum) { |
|
476
|
|
|
continue; |
|
477
|
|
|
} |
|
478
|
|
|
|
|
479
|
3 |
|
$propertyBuilder = $propertyBuilder->withEnum([ |
|
480
|
3 |
|
'value' => $annotation->value, |
|
481
|
3 |
|
'literal' => ( ! empty($annotation->literal)) |
|
482
|
1 |
|
? $annotation->literal |
|
483
|
3 |
|
: $annotation->value, |
|
484
|
|
|
]); |
|
485
|
|
|
} |
|
486
|
|
|
} |
|
487
|
|
|
|
|
488
|
111 |
|
$annotationBuilder->withProperty($propertyBuilder->build()); |
|
489
|
|
|
} |
|
490
|
|
|
|
|
491
|
180 |
|
$this->metadata->add($annotationBuilder->build()); |
|
492
|
180 |
|
} |
|
493
|
|
|
|
|
494
|
|
|
/** |
|
495
|
|
|
* Collects parsing metadata for a given attribute. |
|
496
|
|
|
* |
|
497
|
|
|
* @param array $metadata |
|
498
|
|
|
* @param Attribute $attribute |
|
499
|
|
|
*/ |
|
500
|
197 |
|
private function collectAttributeTypeMetadata( |
|
501
|
|
|
PropertyMetadataBuilder $metadata, |
|
502
|
|
|
Attribute $attribute |
|
503
|
|
|
) : void { |
|
504
|
|
|
// handle internal type declaration |
|
505
|
197 |
|
$type = self::$typeMap[$attribute->type] ?? $attribute->type; |
|
506
|
|
|
|
|
507
|
|
|
// handle the case if the property type is mixed |
|
508
|
197 |
|
if ('mixed' === $type) { |
|
509
|
193 |
|
return; |
|
510
|
|
|
} |
|
511
|
|
|
|
|
512
|
173 |
|
if ($attribute->required) { |
|
513
|
3 |
|
$metadata->withBeingRequired(); |
|
514
|
|
|
} |
|
515
|
|
|
|
|
516
|
|
|
// Evaluate type |
|
517
|
|
|
|
|
518
|
|
|
// Checks if the property has array<type> |
|
519
|
173 |
|
if (false !== $pos = strpos($type, '<')) { |
|
520
|
169 |
|
$arrayType = substr($type, $pos + 1, -1); |
|
521
|
|
|
|
|
522
|
169 |
|
$metadata->withType([ |
|
523
|
169 |
|
'type' => 'array', |
|
524
|
169 |
|
'array_type' => self::$typeMap[$arrayType] ?? $arrayType, |
|
525
|
|
|
]); |
|
526
|
|
|
|
|
527
|
169 |
|
return; |
|
528
|
|
|
} |
|
529
|
|
|
|
|
530
|
|
|
// Checks if the property has type[] |
|
531
|
173 |
|
if (false !== $pos = strrpos($type, '[')) { |
|
532
|
169 |
|
$arrayType = substr($type, 0, $pos); |
|
533
|
|
|
|
|
534
|
169 |
|
$metadata->withType([ |
|
535
|
169 |
|
'type' => 'array', |
|
536
|
169 |
|
'array_type' => self::$typeMap[$arrayType] ?? $arrayType, |
|
537
|
|
|
]); |
|
538
|
|
|
|
|
539
|
169 |
|
return; |
|
540
|
|
|
} |
|
541
|
|
|
|
|
542
|
173 |
|
$metadata->withType([ |
|
543
|
173 |
|
'type' => $type, |
|
544
|
173 |
|
'value' => $attribute->type, |
|
545
|
|
|
]); |
|
546
|
173 |
|
} |
|
547
|
|
|
|
|
548
|
|
|
/** |
|
549
|
|
|
* Annotations ::= Annotation {[ "*" ]* [Annotation]}* |
|
550
|
|
|
* |
|
551
|
|
|
* @return array |
|
552
|
|
|
*/ |
|
553
|
289 |
|
private function Annotations() |
|
554
|
|
|
{ |
|
555
|
289 |
|
$annotations = []; |
|
556
|
|
|
|
|
557
|
289 |
|
while (null !== $this->lexer->lookahead) { |
|
558
|
289 |
|
if (DocLexer::T_AT !== $this->lexer->lookahead['type']) { |
|
559
|
33 |
|
$this->lexer->moveNext(); |
|
560
|
33 |
|
continue; |
|
561
|
|
|
} |
|
562
|
|
|
|
|
563
|
|
|
// make sure the @ is preceded by non-catchable pattern |
|
564
|
289 |
|
if (null !== $this->lexer->token && $this->lexer->lookahead['position'] === $this->lexer->token['position'] + strlen($this->lexer->token['value'])) { |
|
565
|
7 |
|
$this->lexer->moveNext(); |
|
566
|
7 |
|
continue; |
|
567
|
|
|
} |
|
568
|
|
|
|
|
569
|
|
|
// make sure the @ is followed by either a namespace separator, or |
|
570
|
|
|
// an identifier token |
|
571
|
289 |
|
if ((null === $peek = $this->lexer->glimpse()) |
|
572
|
289 |
|
|| (DocLexer::T_NAMESPACE_SEPARATOR !== $peek['type'] && !in_array($peek['type'], self::$classIdentifiers, true)) |
|
573
|
289 |
|
|| $peek['position'] !== $this->lexer->lookahead['position'] + 1) { |
|
574
|
1 |
|
$this->lexer->moveNext(); |
|
575
|
1 |
|
continue; |
|
576
|
|
|
} |
|
577
|
|
|
|
|
578
|
289 |
|
$this->isNestedAnnotation = false; |
|
579
|
289 |
|
if (false !== $annot = $this->Annotation()) { |
|
580
|
243 |
|
$annotations[] = $annot; |
|
581
|
|
|
} |
|
582
|
|
|
} |
|
583
|
|
|
|
|
584
|
283 |
|
return $annotations; |
|
585
|
|
|
} |
|
586
|
|
|
|
|
587
|
|
|
/** |
|
588
|
|
|
* Annotation ::= "@" AnnotationName MethodCall |
|
589
|
|
|
* AnnotationName ::= QualifiedName | SimpleName |
|
590
|
|
|
* QualifiedName ::= NameSpacePart "\" {NameSpacePart "\"}* SimpleName |
|
591
|
|
|
* NameSpacePart ::= identifier | null | false | true |
|
592
|
|
|
* SimpleName ::= identifier | null | false | true |
|
593
|
|
|
* |
|
594
|
|
|
* @return mixed False if it is not a valid annotation. |
|
595
|
|
|
* |
|
596
|
|
|
* @throws AnnotationException |
|
597
|
|
|
*/ |
|
598
|
289 |
|
private function Annotation() |
|
599
|
|
|
{ |
|
600
|
289 |
|
$this->match(DocLexer::T_AT); |
|
601
|
|
|
|
|
602
|
|
|
// check if we have an annotation |
|
603
|
289 |
|
$name = $this->Identifier(); |
|
604
|
|
|
|
|
605
|
|
|
// only process names which are not fully qualified, yet |
|
606
|
|
|
// fully qualified names must start with a \ |
|
607
|
288 |
|
$originalName = $name; |
|
608
|
|
|
|
|
609
|
288 |
|
if ('\\' !== $name[0]) { |
|
610
|
286 |
|
$pos = strpos($name, '\\'); |
|
611
|
286 |
|
$alias = (false === $pos)? $name : substr($name, 0, $pos); |
|
612
|
286 |
|
$found = false; |
|
613
|
286 |
|
$loweredAlias = strtolower($alias); |
|
614
|
|
|
|
|
615
|
286 |
|
if ($this->namespaces) { |
|
|
|
|
|
|
616
|
2 |
|
foreach ($this->namespaces as $namespace) { |
|
617
|
2 |
|
if ($this->classExists($namespace.'\\'.$name)) { |
|
618
|
2 |
|
$name = $namespace.'\\'.$name; |
|
619
|
2 |
|
$found = true; |
|
620
|
2 |
|
break; |
|
621
|
|
|
} |
|
622
|
|
|
} |
|
623
|
286 |
|
} elseif (isset($this->imports[$loweredAlias])) { |
|
624
|
236 |
|
$found = true; |
|
625
|
236 |
|
$name = (false !== $pos) |
|
626
|
1 |
|
? $this->imports[$loweredAlias] . substr($name, $pos) |
|
627
|
236 |
|
: $this->imports[$loweredAlias]; |
|
628
|
286 |
|
} elseif ( ! isset($this->ignoredAnnotationNames[$name]) |
|
629
|
286 |
|
&& isset($this->imports['__NAMESPACE__']) |
|
630
|
286 |
|
&& $this->classExists($this->imports['__NAMESPACE__'] . '\\' . $name) |
|
631
|
|
|
) { |
|
632
|
35 |
|
$name = $this->imports['__NAMESPACE__'].'\\'.$name; |
|
633
|
35 |
|
$found = true; |
|
634
|
286 |
|
} elseif (! isset($this->ignoredAnnotationNames[$name]) && $this->classExists($name)) { |
|
635
|
182 |
|
$found = true; |
|
636
|
|
|
} |
|
637
|
|
|
|
|
638
|
286 |
|
if ( ! $found) { |
|
639
|
286 |
|
if ($this->isIgnoredAnnotation($name)) { |
|
640
|
284 |
|
return false; |
|
641
|
|
|
} |
|
642
|
|
|
|
|
643
|
2 |
|
throw AnnotationException::semanticalError(sprintf('The annotation "@%s" in %s was never imported. Did you maybe forget to add a "use" statement for this annotation?', $name, $this->context)); |
|
644
|
|
|
} |
|
645
|
|
|
} |
|
646
|
|
|
|
|
647
|
263 |
|
$name = ltrim($name,'\\'); |
|
648
|
|
|
|
|
649
|
263 |
|
if ( ! $this->classExists($name)) { |
|
650
|
|
|
throw AnnotationException::semanticalError(sprintf('The annotation "@%s" in %s does not exist, or could not be auto-loaded.', $name, $this->context)); |
|
651
|
|
|
} |
|
652
|
|
|
|
|
653
|
|
|
// at this point, $name contains the fully qualified class name of the |
|
654
|
|
|
// annotation, and it is also guaranteed that this class exists, and |
|
655
|
|
|
// that it is loaded |
|
656
|
|
|
|
|
657
|
|
|
|
|
658
|
|
|
// collects the metadata annotation only if there is not yet |
|
659
|
263 |
|
if (! $this->metadata->has($name) && ! array_key_exists($name, $this->nonAnnotationClasses)) { |
|
660
|
263 |
|
$this->collectAnnotationMetadata($name); |
|
661
|
|
|
} |
|
662
|
|
|
|
|
663
|
|
|
// verify that the class is really meant to be an annotation and not just any ordinary class |
|
664
|
263 |
|
if (array_key_exists($name, $this->nonAnnotationClasses)) { |
|
665
|
4 |
|
if ($this->ignoreNotImportedAnnotations || isset($this->ignoredAnnotationNames[$originalName])) { |
|
666
|
2 |
|
return false; |
|
667
|
|
|
} |
|
668
|
|
|
|
|
669
|
2 |
|
throw AnnotationException::semanticalError(sprintf('The class "%s" is not annotated with @Annotation. Are you sure this class can be used as annotation? If so, then you need to add @Annotation to the _class_ doc comment of "%s". If it is indeed no annotation, then you need to add @IgnoreAnnotation("%s") to the _class_ doc comment of %s.', $name, $name, $originalName, $this->context)); |
|
670
|
|
|
} |
|
671
|
|
|
|
|
672
|
|
|
//if target is nested annotation |
|
673
|
259 |
|
$target = $this->isNestedAnnotation ? Target::TARGET_ANNOTATION : $this->target; |
|
674
|
|
|
|
|
675
|
|
|
// Next will be nested |
|
676
|
259 |
|
$this->isNestedAnnotation = true; |
|
677
|
259 |
|
$metadata = $this->metadata->get($name); |
|
678
|
|
|
|
|
679
|
|
|
//if annotation does not support current target |
|
680
|
259 |
|
if (($metadata->getTarget()->unwrap() & $target) === 0 && $target) { |
|
681
|
5 |
|
throw AnnotationException::semanticalError( |
|
682
|
5 |
|
sprintf('Annotation @%s is not allowed to be declared on %s. You may only use this annotation on these code elements: %s.', |
|
683
|
5 |
|
$originalName, $this->context, $metadata->getTarget()->describe()) |
|
684
|
|
|
); |
|
685
|
|
|
} |
|
686
|
|
|
|
|
687
|
259 |
|
$values = $this->MethodCall(); |
|
688
|
|
|
|
|
689
|
|
|
// checks all declared attributes for enums |
|
690
|
248 |
|
foreach ($metadata->getProperties() as $property) { |
|
691
|
232 |
|
$propertyName = $property->getName(); |
|
692
|
232 |
|
$enum = $property->getEnum(); |
|
693
|
|
|
|
|
694
|
|
|
// checks if the attribute is a valid enumerator |
|
695
|
232 |
|
if ($enum !== null && isset($values[$propertyName]) && ! in_array($values[$propertyName], $enum['value'])) { |
|
696
|
3 |
|
throw AnnotationException::enumeratorError($propertyName, $name, $this->context, $enum['literal'], $values[$propertyName]); |
|
697
|
|
|
} |
|
698
|
|
|
} |
|
699
|
|
|
|
|
700
|
|
|
// checks all declared attributes |
|
701
|
248 |
|
foreach ($metadata->getProperties() as $property) { |
|
702
|
232 |
|
$propertyName = $property->getName(); |
|
703
|
232 |
|
$valueName = $propertyName; |
|
704
|
232 |
|
$type = $property->getType(); |
|
705
|
|
|
|
|
706
|
232 |
|
if ($property->isDefault() && !isset($values[$propertyName]) && isset($values['value'])) { |
|
707
|
90 |
|
$valueName = 'value'; |
|
708
|
|
|
} |
|
709
|
|
|
|
|
710
|
|
|
// handle a not given attribute or null value |
|
711
|
232 |
|
if (! isset($values[$valueName])) { |
|
712
|
203 |
|
if ($property->isRequired()) { |
|
713
|
2 |
|
throw AnnotationException::requiredError($propertyName, $originalName, $this->context, 'a(n) ' . $type['value']); |
|
714
|
|
|
} |
|
715
|
|
|
|
|
716
|
203 |
|
continue; |
|
717
|
|
|
} |
|
718
|
|
|
|
|
719
|
225 |
|
if ($type !== null && $type['type'] === 'array') { |
|
720
|
|
|
// handle the case of a single value |
|
721
|
204 |
|
if ( ! is_array($values[$valueName])) { |
|
722
|
201 |
|
$values[$valueName] = [$values[$valueName]]; |
|
723
|
|
|
} |
|
724
|
|
|
|
|
725
|
|
|
// checks if the attribute has array type declaration, such as "array<string>" |
|
726
|
204 |
|
if (isset($type['array_type'])) { |
|
727
|
204 |
|
foreach ($values[$valueName] as $item) { |
|
728
|
204 |
|
if (gettype($item) !== $type['array_type'] && !$item instanceof $type['array_type']) { |
|
729
|
36 |
|
throw AnnotationException::attributeTypeError($propertyName, $originalName, $this->context, 'either a(n) '.$type['array_type'].', or an array of '.$type['array_type'].'s', $item); |
|
730
|
|
|
} |
|
731
|
|
|
} |
|
732
|
|
|
} |
|
733
|
190 |
|
} elseif ($type !== null && gettype($values[$valueName]) !== $type['type'] && !$values[$valueName] instanceof $type['type']) { |
|
734
|
72 |
|
throw AnnotationException::attributeTypeError($propertyName, $originalName, $this->context, 'a(n) '.$type['value'], $values[$valueName]); |
|
735
|
|
|
} |
|
736
|
|
|
} |
|
737
|
|
|
|
|
738
|
|
|
// check if the annotation expects values via the constructor, |
|
739
|
|
|
// or directly injected into public properties |
|
740
|
248 |
|
if ($metadata->usesConstructor()) { |
|
741
|
210 |
|
return new $name($values); |
|
742
|
|
|
} |
|
743
|
|
|
|
|
744
|
188 |
|
$instance = new $name(); |
|
745
|
|
|
|
|
746
|
188 |
|
foreach ($values as $property => $value) { |
|
747
|
164 |
|
if (! isset($metadata->getProperties()[$property])) { |
|
748
|
93 |
|
if ('value' !== $property) { |
|
749
|
1 |
|
throw AnnotationException::creationError( |
|
750
|
1 |
|
sprintf( |
|
751
|
1 |
|
'The annotation @%s declared on %s does not have a property named "%s". Available properties: %s', |
|
752
|
1 |
|
$originalName, |
|
753
|
1 |
|
$this->context, |
|
754
|
1 |
|
$property, |
|
755
|
1 |
|
implode(', ', array_keys($metadata->getProperties())) |
|
756
|
|
|
) |
|
757
|
|
|
); |
|
758
|
|
|
} |
|
759
|
|
|
|
|
760
|
92 |
|
$defaultProperty = $metadata->getDefaultProperty(); |
|
761
|
|
|
|
|
762
|
|
|
// handle the case if the property has no annotations |
|
763
|
92 |
|
if ($defaultProperty === null) { |
|
764
|
2 |
|
throw AnnotationException::creationError(sprintf('The annotation @%s declared on %s does not accept any values, but got %s.', $originalName, $this->context, json_encode($values))); |
|
765
|
|
|
} |
|
766
|
|
|
|
|
767
|
90 |
|
$property = $defaultProperty->getName(); |
|
768
|
|
|
} |
|
769
|
|
|
|
|
770
|
161 |
|
$instance->{$property} = $value; |
|
771
|
|
|
} |
|
772
|
|
|
|
|
773
|
185 |
|
return $instance; |
|
774
|
|
|
} |
|
775
|
|
|
|
|
776
|
|
|
/** |
|
777
|
|
|
* MethodCall ::= ["(" [Values] ")"] |
|
778
|
|
|
* |
|
779
|
|
|
* @return array |
|
780
|
|
|
*/ |
|
781
|
259 |
|
private function MethodCall() |
|
782
|
|
|
{ |
|
783
|
259 |
|
$values = []; |
|
784
|
|
|
|
|
785
|
259 |
|
if ( ! $this->lexer->isNextToken(DocLexer::T_OPEN_PARENTHESIS)) { |
|
786
|
55 |
|
return $values; |
|
787
|
|
|
} |
|
788
|
|
|
|
|
789
|
242 |
|
$this->match(DocLexer::T_OPEN_PARENTHESIS); |
|
790
|
|
|
|
|
791
|
242 |
|
if ( ! $this->lexer->isNextToken(DocLexer::T_CLOSE_PARENTHESIS)) { |
|
792
|
242 |
|
$values = $this->Values(); |
|
793
|
|
|
} |
|
794
|
|
|
|
|
795
|
231 |
|
$this->match(DocLexer::T_CLOSE_PARENTHESIS); |
|
796
|
|
|
|
|
797
|
231 |
|
return $values; |
|
798
|
|
|
} |
|
799
|
|
|
|
|
800
|
|
|
/** |
|
801
|
|
|
* Values ::= Array | Value {"," Value}* [","] |
|
802
|
|
|
* |
|
803
|
|
|
* @return array |
|
804
|
|
|
*/ |
|
805
|
242 |
|
private function Values() |
|
806
|
|
|
{ |
|
807
|
242 |
|
$values = [$this->Value()]; |
|
808
|
|
|
|
|
809
|
231 |
|
while ($this->lexer->isNextToken(DocLexer::T_COMMA)) { |
|
810
|
94 |
|
$this->match(DocLexer::T_COMMA); |
|
811
|
|
|
|
|
812
|
94 |
|
if ($this->lexer->isNextToken(DocLexer::T_CLOSE_PARENTHESIS)) { |
|
813
|
1 |
|
break; |
|
814
|
|
|
} |
|
815
|
|
|
|
|
816
|
93 |
|
$token = $this->lexer->lookahead; |
|
817
|
93 |
|
$value = $this->Value(); |
|
818
|
|
|
|
|
819
|
93 |
|
if ( ! is_object($value) && ! is_array($value)) { |
|
820
|
|
|
$this->syntaxError('Value', $token); |
|
821
|
|
|
} |
|
822
|
|
|
|
|
823
|
93 |
|
$values[] = $value; |
|
824
|
|
|
} |
|
825
|
|
|
|
|
826
|
231 |
|
foreach ($values as $k => $value) { |
|
827
|
231 |
|
if (is_object($value) && $value instanceof \stdClass) { |
|
828
|
194 |
|
$values[$value->name] = $value->value; |
|
829
|
217 |
|
} else if ( ! isset($values['value'])){ |
|
830
|
217 |
|
$values['value'] = $value; |
|
831
|
|
|
} else { |
|
832
|
1 |
|
if ( ! is_array($values['value'])) { |
|
833
|
1 |
|
$values['value'] = [$values['value']]; |
|
834
|
|
|
} |
|
835
|
|
|
|
|
836
|
1 |
|
$values['value'][] = $value; |
|
837
|
|
|
} |
|
838
|
|
|
|
|
839
|
231 |
|
unset($values[$k]); |
|
840
|
|
|
} |
|
841
|
|
|
|
|
842
|
231 |
|
return $values; |
|
843
|
|
|
} |
|
844
|
|
|
|
|
845
|
|
|
/** |
|
846
|
|
|
* Constant ::= integer | string | float | boolean |
|
847
|
|
|
* |
|
848
|
|
|
* @return mixed |
|
849
|
|
|
* |
|
850
|
|
|
* @throws AnnotationException |
|
851
|
|
|
*/ |
|
852
|
74 |
|
private function Constant() |
|
853
|
|
|
{ |
|
854
|
74 |
|
$identifier = $this->Identifier(); |
|
855
|
|
|
|
|
856
|
74 |
|
if ( ! defined($identifier) && false !== strpos($identifier, '::') && '\\' !== $identifier[0]) { |
|
857
|
16 |
|
list($className, $const) = explode('::', $identifier); |
|
858
|
|
|
|
|
859
|
16 |
|
$pos = strpos($className, '\\'); |
|
860
|
16 |
|
$alias = (false === $pos) ? $className : substr($className, 0, $pos); |
|
861
|
16 |
|
$found = false; |
|
862
|
16 |
|
$loweredAlias = strtolower($alias); |
|
863
|
|
|
|
|
864
|
|
|
switch (true) { |
|
865
|
16 |
|
case !empty ($this->namespaces): |
|
866
|
|
|
foreach ($this->namespaces as $ns) { |
|
867
|
|
|
if (class_exists($ns.'\\'.$className) || interface_exists($ns.'\\'.$className)) { |
|
868
|
|
|
$className = $ns.'\\'.$className; |
|
869
|
|
|
$found = true; |
|
870
|
|
|
break; |
|
871
|
|
|
} |
|
872
|
|
|
} |
|
873
|
|
|
break; |
|
874
|
|
|
|
|
875
|
16 |
|
case isset($this->imports[$loweredAlias]): |
|
876
|
14 |
|
$found = true; |
|
877
|
14 |
|
$className = (false !== $pos) |
|
878
|
|
|
? $this->imports[$loweredAlias] . substr($className, $pos) |
|
879
|
14 |
|
: $this->imports[$loweredAlias]; |
|
880
|
14 |
|
break; |
|
881
|
|
|
|
|
882
|
|
|
default: |
|
883
|
2 |
|
if(isset($this->imports['__NAMESPACE__'])) { |
|
884
|
|
|
$ns = $this->imports['__NAMESPACE__']; |
|
885
|
|
|
|
|
886
|
|
|
if (class_exists($ns.'\\'.$className) || interface_exists($ns.'\\'.$className)) { |
|
887
|
|
|
$className = $ns.'\\'.$className; |
|
888
|
|
|
$found = true; |
|
889
|
|
|
} |
|
890
|
|
|
} |
|
891
|
2 |
|
break; |
|
892
|
|
|
} |
|
893
|
|
|
|
|
894
|
16 |
|
if ($found) { |
|
895
|
14 |
|
$identifier = $className . '::' . $const; |
|
896
|
|
|
} |
|
897
|
|
|
} |
|
898
|
|
|
|
|
899
|
|
|
// checks if identifier ends with ::class, \strlen('::class') === 7 |
|
900
|
74 |
|
$classPos = stripos($identifier, '::class'); |
|
901
|
74 |
|
if ($classPos === strlen($identifier) - 7) { |
|
902
|
4 |
|
return substr($identifier, 0, $classPos); |
|
903
|
|
|
} |
|
904
|
|
|
|
|
905
|
70 |
|
if (!defined($identifier)) { |
|
906
|
1 |
|
throw AnnotationException::semanticalErrorConstants($identifier, $this->context); |
|
907
|
|
|
} |
|
908
|
|
|
|
|
909
|
69 |
|
return constant($identifier); |
|
910
|
|
|
} |
|
911
|
|
|
|
|
912
|
|
|
/** |
|
913
|
|
|
* Identifier ::= string |
|
914
|
|
|
* |
|
915
|
|
|
* @return string |
|
916
|
|
|
*/ |
|
917
|
289 |
|
private function Identifier() |
|
918
|
|
|
{ |
|
919
|
|
|
// check if we have an annotation |
|
920
|
289 |
|
if ( ! $this->lexer->isNextTokenAny(self::$classIdentifiers)) { |
|
921
|
8 |
|
$this->syntaxError('namespace separator or identifier'); |
|
922
|
|
|
} |
|
923
|
|
|
|
|
924
|
289 |
|
$this->lexer->moveNext(); |
|
925
|
|
|
|
|
926
|
289 |
|
$className = $this->lexer->token['value']; |
|
927
|
|
|
|
|
928
|
289 |
|
while ($this->lexer->lookahead['position'] === ($this->lexer->token['position'] + strlen($this->lexer->token['value'])) |
|
929
|
289 |
|
&& $this->lexer->isNextToken(DocLexer::T_NAMESPACE_SEPARATOR)) { |
|
930
|
|
|
|
|
931
|
1 |
|
$this->match(DocLexer::T_NAMESPACE_SEPARATOR); |
|
932
|
1 |
|
$this->matchAny(self::$classIdentifiers); |
|
933
|
|
|
|
|
934
|
|
|
$className .= '\\' . $this->lexer->token['value']; |
|
935
|
|
|
} |
|
936
|
|
|
|
|
937
|
288 |
|
return $className; |
|
938
|
|
|
} |
|
939
|
|
|
|
|
940
|
|
|
/** |
|
941
|
|
|
* Value ::= PlainValue | FieldAssignment |
|
942
|
|
|
* |
|
943
|
|
|
* @return mixed |
|
944
|
|
|
*/ |
|
945
|
242 |
|
private function Value() |
|
946
|
|
|
{ |
|
947
|
242 |
|
$peek = $this->lexer->glimpse(); |
|
948
|
|
|
|
|
949
|
242 |
|
if (DocLexer::T_EQUALS === $peek['type']) { |
|
950
|
196 |
|
return $this->FieldAssignment(); |
|
951
|
|
|
} |
|
952
|
|
|
|
|
953
|
227 |
|
return $this->PlainValue(); |
|
954
|
|
|
} |
|
955
|
|
|
|
|
956
|
|
|
/** |
|
957
|
|
|
* PlainValue ::= integer | string | float | boolean | Array | Annotation |
|
958
|
|
|
* |
|
959
|
|
|
* @return mixed |
|
960
|
|
|
*/ |
|
961
|
242 |
|
private function PlainValue() |
|
962
|
|
|
{ |
|
963
|
242 |
|
if ($this->lexer->isNextToken(DocLexer::T_OPEN_CURLY_BRACES)) { |
|
964
|
138 |
|
return $this->Arrayx(); |
|
965
|
|
|
} |
|
966
|
|
|
|
|
967
|
241 |
|
if ($this->lexer->isNextToken(DocLexer::T_AT)) { |
|
968
|
113 |
|
return $this->Annotation(); |
|
969
|
|
|
} |
|
970
|
|
|
|
|
971
|
233 |
|
if ($this->lexer->isNextToken(DocLexer::T_IDENTIFIER)) { |
|
972
|
74 |
|
return $this->Constant(); |
|
973
|
|
|
} |
|
974
|
|
|
|
|
975
|
232 |
|
switch ($this->lexer->lookahead['type']) { |
|
976
|
232 |
|
case DocLexer::T_STRING: |
|
977
|
225 |
|
$this->match(DocLexer::T_STRING); |
|
978
|
225 |
|
return $this->lexer->token['value']; |
|
979
|
|
|
|
|
980
|
81 |
|
case DocLexer::T_INTEGER: |
|
981
|
53 |
|
$this->match(DocLexer::T_INTEGER); |
|
982
|
53 |
|
return (int)$this->lexer->token['value']; |
|
983
|
|
|
|
|
984
|
30 |
|
case DocLexer::T_FLOAT: |
|
985
|
28 |
|
$this->match(DocLexer::T_FLOAT); |
|
986
|
28 |
|
return (float)$this->lexer->token['value']; |
|
987
|
|
|
|
|
988
|
2 |
|
case DocLexer::T_TRUE: |
|
989
|
|
|
$this->match(DocLexer::T_TRUE); |
|
990
|
|
|
return true; |
|
991
|
|
|
|
|
992
|
2 |
|
case DocLexer::T_FALSE: |
|
993
|
|
|
$this->match(DocLexer::T_FALSE); |
|
994
|
|
|
return false; |
|
995
|
|
|
|
|
996
|
2 |
|
case DocLexer::T_NULL: |
|
997
|
|
|
$this->match(DocLexer::T_NULL); |
|
998
|
|
|
return null; |
|
999
|
|
|
|
|
1000
|
|
|
default: |
|
1001
|
2 |
|
$this->syntaxError('PlainValue'); |
|
1002
|
|
|
} |
|
1003
|
|
|
} |
|
1004
|
|
|
|
|
1005
|
|
|
/** |
|
1006
|
|
|
* FieldAssignment ::= FieldName "=" PlainValue |
|
1007
|
|
|
* FieldName ::= identifier |
|
1008
|
|
|
* |
|
1009
|
|
|
* @return \stdClass |
|
1010
|
|
|
*/ |
|
1011
|
196 |
|
private function FieldAssignment() |
|
1012
|
|
|
{ |
|
1013
|
196 |
|
$this->match(DocLexer::T_IDENTIFIER); |
|
1014
|
196 |
|
$fieldName = $this->lexer->token['value']; |
|
1015
|
|
|
|
|
1016
|
196 |
|
$this->match(DocLexer::T_EQUALS); |
|
1017
|
|
|
|
|
1018
|
196 |
|
$item = new \stdClass(); |
|
1019
|
196 |
|
$item->name = $fieldName; |
|
1020
|
196 |
|
$item->value = $this->PlainValue(); |
|
1021
|
|
|
|
|
1022
|
194 |
|
return $item; |
|
1023
|
|
|
} |
|
1024
|
|
|
|
|
1025
|
|
|
/** |
|
1026
|
|
|
* Array ::= "{" ArrayEntry {"," ArrayEntry}* [","] "}" |
|
1027
|
|
|
* |
|
1028
|
|
|
* @return array |
|
1029
|
|
|
*/ |
|
1030
|
138 |
|
private function Arrayx() |
|
1031
|
|
|
{ |
|
1032
|
138 |
|
$array = $values = []; |
|
1033
|
|
|
|
|
1034
|
138 |
|
$this->match(DocLexer::T_OPEN_CURLY_BRACES); |
|
1035
|
|
|
|
|
1036
|
|
|
// If the array is empty, stop parsing and return. |
|
1037
|
138 |
|
if ($this->lexer->isNextToken(DocLexer::T_CLOSE_CURLY_BRACES)) { |
|
1038
|
1 |
|
$this->match(DocLexer::T_CLOSE_CURLY_BRACES); |
|
1039
|
|
|
|
|
1040
|
1 |
|
return $array; |
|
1041
|
|
|
} |
|
1042
|
|
|
|
|
1043
|
138 |
|
$values[] = $this->ArrayEntry(); |
|
1044
|
|
|
|
|
1045
|
138 |
|
while ($this->lexer->isNextToken(DocLexer::T_COMMA)) { |
|
1046
|
119 |
|
$this->match(DocLexer::T_COMMA); |
|
1047
|
|
|
|
|
1048
|
|
|
// optional trailing comma |
|
1049
|
119 |
|
if ($this->lexer->isNextToken(DocLexer::T_CLOSE_CURLY_BRACES)) { |
|
1050
|
86 |
|
break; |
|
1051
|
|
|
} |
|
1052
|
|
|
|
|
1053
|
119 |
|
$values[] = $this->ArrayEntry(); |
|
1054
|
|
|
} |
|
1055
|
|
|
|
|
1056
|
138 |
|
$this->match(DocLexer::T_CLOSE_CURLY_BRACES); |
|
1057
|
|
|
|
|
1058
|
138 |
|
foreach ($values as $value) { |
|
1059
|
138 |
|
list ($key, $val) = $value; |
|
1060
|
|
|
|
|
1061
|
138 |
|
if ($key !== null) { |
|
1062
|
13 |
|
$array[$key] = $val; |
|
1063
|
|
|
} else { |
|
1064
|
129 |
|
$array[] = $val; |
|
1065
|
|
|
} |
|
1066
|
|
|
} |
|
1067
|
|
|
|
|
1068
|
138 |
|
return $array; |
|
1069
|
|
|
} |
|
1070
|
|
|
|
|
1071
|
|
|
/** |
|
1072
|
|
|
* ArrayEntry ::= Value | KeyValuePair |
|
1073
|
|
|
* KeyValuePair ::= Key ("=" | ":") PlainValue | Constant |
|
1074
|
|
|
* Key ::= string | integer | Constant |
|
1075
|
|
|
* |
|
1076
|
|
|
* @return array |
|
1077
|
|
|
*/ |
|
1078
|
138 |
|
private function ArrayEntry() |
|
1079
|
|
|
{ |
|
1080
|
138 |
|
$peek = $this->lexer->glimpse(); |
|
1081
|
|
|
|
|
1082
|
138 |
|
if (DocLexer::T_EQUALS === $peek['type'] |
|
1083
|
138 |
|
|| DocLexer::T_COLON === $peek['type']) { |
|
1084
|
|
|
|
|
1085
|
13 |
|
if ($this->lexer->isNextToken(DocLexer::T_IDENTIFIER)) { |
|
1086
|
5 |
|
$key = $this->Constant(); |
|
1087
|
|
|
} else { |
|
1088
|
8 |
|
$this->matchAny([DocLexer::T_INTEGER, DocLexer::T_STRING]); |
|
1089
|
8 |
|
$key = $this->lexer->token['value']; |
|
1090
|
|
|
} |
|
1091
|
|
|
|
|
1092
|
13 |
|
$this->matchAny([DocLexer::T_EQUALS, DocLexer::T_COLON]); |
|
1093
|
|
|
|
|
1094
|
13 |
|
return [$key, $this->PlainValue()]; |
|
1095
|
|
|
} |
|
1096
|
|
|
|
|
1097
|
129 |
|
return [null, $this->Value()]; |
|
1098
|
|
|
} |
|
1099
|
|
|
|
|
1100
|
|
|
/** |
|
1101
|
|
|
* Checks whether the given $name matches any ignored annotation name or namespace |
|
1102
|
|
|
* |
|
1103
|
|
|
* @param string $name |
|
1104
|
|
|
* |
|
1105
|
|
|
* @return bool |
|
1106
|
|
|
*/ |
|
1107
|
286 |
|
private function isIgnoredAnnotation($name) |
|
1108
|
|
|
{ |
|
1109
|
286 |
|
if ($this->ignoreNotImportedAnnotations || isset($this->ignoredAnnotationNames[$name])) { |
|
1110
|
270 |
|
return true; |
|
1111
|
|
|
} |
|
1112
|
|
|
|
|
1113
|
17 |
|
foreach (array_keys($this->ignoredAnnotationNamespaces) as $ignoredAnnotationNamespace) { |
|
1114
|
15 |
|
$ignoredAnnotationNamespace = rtrim($ignoredAnnotationNamespace, '\\') . '\\'; |
|
1115
|
|
|
|
|
1116
|
15 |
|
if (0 === stripos(rtrim($name, '\\') . '\\', $ignoredAnnotationNamespace)) { |
|
1117
|
15 |
|
return true; |
|
1118
|
|
|
} |
|
1119
|
|
|
} |
|
1120
|
|
|
|
|
1121
|
2 |
|
return false; |
|
1122
|
|
|
} |
|
1123
|
|
|
} |
|
1124
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.