13
13
namespace phpDocumentor \Reflection ;
14
14
15
15
use phpDocumentor \Reflection \Types \Array_ ;
16
+ use phpDocumentor \Reflection \Types \Collection ;
16
17
use phpDocumentor \Reflection \Types \Compound ;
17
18
use phpDocumentor \Reflection \Types \Context ;
19
+ use phpDocumentor \Reflection \Types \Integer ;
18
20
use phpDocumentor \Reflection \Types \Iterable_ ;
19
21
use phpDocumentor \Reflection \Types \Nullable ;
20
22
use phpDocumentor \Reflection \Types \Object_ ;
21
- use phpDocumentor \Reflection \Types \Collection ;
22
23
use phpDocumentor \Reflection \Types \String_ ;
23
- use phpDocumentor \Reflection \Types \Integer ;
24
24
25
25
final class TypeResolver
26
26
{
@@ -42,9 +42,8 @@ final class TypeResolver
42
42
/** @var integer the iterator parser is inside a collection expression context */
43
43
const PARSER_IN_COLLECTION_EXPRESSION = 3 ;
44
44
45
-
46
45
/** @var string[] List of recognized keywords and unto which Value Object they map */
47
- private $ keywords = array (
46
+ private $ keywords = [
48
47
'string ' => Types \String_::class,
49
48
'int ' => Types \Integer::class,
50
49
'integer ' => Types \Integer::class,
@@ -69,15 +68,13 @@ final class TypeResolver
69
68
'static ' => Types \Static_::class,
70
69
'parent ' => Types \Parent_::class,
71
70
'iterable ' => Iterable_::class,
72
- ) ;
71
+ ] ;
73
72
74
73
/** @var FqsenResolver */
75
74
private $ fqsenResolver ;
76
75
77
76
/**
78
77
* Initializes this TypeResolver with the means to create and resolve Fqsen objects.
79
- *
80
- * @param FqsenResolver $fqsenResolver
81
78
*/
82
79
public function __construct (FqsenResolver $ fqsenResolver = null )
83
80
{
@@ -94,13 +91,10 @@ public function __construct(FqsenResolver $fqsenResolver = null)
94
91
* This method only works as expected if the namespace and aliases are set;
95
92
* no dynamic reflection is being performed here.
96
93
*
97
- * @param string $type The relative or absolute type.
98
- * @param Context $context
99
- *
94
+ * @param string $type The relative or absolute type.
100
95
* @uses Context::getNamespace() to determine with what to prefix the type name.
101
96
* @uses Context::getNamespaceAliases() to check whether the first part of the relative type name should not be
102
- * replaced with another namespace.
103
- *
97
+ * replaced with another namespace.
104
98
* @return Type
105
99
*/
106
100
public function resolve ($ type , Context $ context = null )
@@ -130,26 +124,25 @@ public function resolve($type, Context $context = null)
130
124
/**
131
125
* Analyse each tokens and creates types
132
126
*
133
- * @param \ArrayIterator $tokens the iterator on tokens
134
- * @param Context $context
135
- * @param integer $parserContext on of self::PARSER_* constants, indicating
136
- * the context where we are in the parsing
137
- *
127
+ * @param ArrayIterator $tokens the iterator on tokens
128
+ * @param int $parserContext on of self::PARSER_* constants, indicating
129
+ * the context where we are in the parsing
138
130
* @return Type
139
131
*/
140
132
private function parseTypes (\ArrayIterator $ tokens , Context $ context , $ parserContext )
141
133
{
142
- $ types = array () ;
134
+ $ types = [] ;
143
135
$ token = '' ;
144
136
while ($ tokens ->valid ()) {
145
137
$ token = $ tokens ->current ();
146
138
147
- if ($ token == '| ' ) {
148
- if (count ($ types ) == 0 ) {
139
+ if ($ token === '| ' ) {
140
+ if (count ($ types ) === 0 ) {
149
141
throw new \RuntimeException (
150
142
'A type is missing before a type separator '
151
143
);
152
144
}
145
+
153
146
if ($ parserContext !== self ::PARSER_IN_COMPOUND
154
147
&& $ parserContext !== self ::PARSER_IN_ARRAY_EXPRESSION
155
148
&& $ parserContext !== self ::PARSER_IN_COLLECTION_EXPRESSION
@@ -158,9 +151,9 @@ private function parseTypes(\ArrayIterator $tokens, Context $context, $parserCon
158
151
'Unexpected type separator '
159
152
);
160
153
}
161
- $ tokens ->next ();
162
154
163
- } else if ($ token == '? ' ) {
155
+ $ tokens ->next ();
156
+ } elseif ($ token === '? ' ) {
164
157
if ($ parserContext !== self ::PARSER_IN_COMPOUND
165
158
&& $ parserContext !== self ::PARSER_IN_ARRAY_EXPRESSION
166
159
&& $ parserContext !== self ::PARSER_IN_COLLECTION_EXPRESSION
@@ -173,40 +166,38 @@ private function parseTypes(\ArrayIterator $tokens, Context $context, $parserCon
173
166
$ tokens ->next ();
174
167
$ type = $ this ->parseTypes ($ tokens , $ context , self ::PARSER_IN_NULLABLE );
175
168
$ types [] = new Nullable ($ type );
176
-
177
- } else if ($ token === '( ' ) {
169
+ } elseif ($ token === '( ' ) {
178
170
$ tokens ->next ();
179
171
$ type = $ this ->parseTypes ($ tokens , $ context , self ::PARSER_IN_ARRAY_EXPRESSION );
180
172
181
173
$ resolvedType = new Array_ ($ type );
182
174
183
175
// we generates arrays corresponding to the number of '[]'
184
176
// after the ')'
185
- $ numberOfArrays = (strlen ($ tokens ->current ()) -1 ) / 2 ;
186
- for ($ i = 0 ; $ i < $ numberOfArrays - 1 ; $ i ++ ) {
177
+ $ numberOfArrays = (strlen ($ tokens ->current ()) - 1 ) / 2 ;
178
+ for ($ i = 0 ; $ i < $ numberOfArrays - 1 ; ++ $ i ) {
187
179
$ resolvedType = new Array_ ($ resolvedType );
188
180
}
181
+
189
182
$ types [] = $ resolvedType ;
190
183
$ tokens ->next ();
191
-
192
- } else if ($ parserContext === self ::PARSER_IN_ARRAY_EXPRESSION
184
+ } elseif ($ parserContext === self ::PARSER_IN_ARRAY_EXPRESSION
193
185
&& $ token [0 ] === ') '
194
186
) {
195
187
break ;
196
-
197
- } else if ($ token === '< ' ) {
188
+ } elseif ($ token === '< ' ) {
198
189
if (count ($ types ) === 0 ) {
199
190
throw new \RuntimeException (
200
191
'Unexpected collection operator "<", class name is missing '
201
192
);
202
193
}
194
+
203
195
$ classType = array_pop ($ types );
204
196
205
197
$ types [] = $ this ->resolveCollection ($ tokens , $ classType , $ context );
206
198
207
199
$ tokens ->next ();
208
-
209
- } else if ($ parserContext === self ::PARSER_IN_COLLECTION_EXPRESSION
200
+ } elseif ($ parserContext === self ::PARSER_IN_COLLECTION_EXPRESSION
210
201
&& ($ token === '> ' || $ token === ', ' )
211
202
) {
212
203
break ;
@@ -216,47 +207,51 @@ private function parseTypes(\ArrayIterator $tokens, Context $context, $parserCon
216
207
if ($ parserContext === self ::PARSER_IN_NULLABLE ) {
217
208
return $ type ;
218
209
}
210
+
219
211
$ types [] = $ type ;
220
212
}
221
213
}
222
214
223
- if ($ token == '| ' ) {
215
+ if ($ token === '| ' ) {
224
216
throw new \RuntimeException (
225
217
'A type is missing after a type separator '
226
218
);
227
219
}
228
220
229
- if (count ($ types ) == 0 ) {
230
- if ($ parserContext == self ::PARSER_IN_NULLABLE ) {
221
+ if (count ($ types ) === 0 ) {
222
+ if ($ parserContext === self ::PARSER_IN_NULLABLE ) {
231
223
throw new \RuntimeException (
232
224
'A type is missing after a nullable character '
233
225
);
234
226
}
235
- if ($ parserContext == self ::PARSER_IN_ARRAY_EXPRESSION ) {
227
+
228
+ if ($ parserContext === self ::PARSER_IN_ARRAY_EXPRESSION ) {
236
229
throw new \RuntimeException (
237
230
'A type is missing in an array expression '
238
231
);
239
232
}
240
- if ($ parserContext == self ::PARSER_IN_COLLECTION_EXPRESSION ) {
233
+
234
+ if ($ parserContext === self ::PARSER_IN_COLLECTION_EXPRESSION ) {
241
235
throw new \RuntimeException (
242
236
'A type is missing in a collection expression '
243
237
);
244
238
}
239
+
245
240
throw new \RuntimeException (
246
241
'No types in a compound list '
247
242
);
248
- } else if (count ($ types ) == 1 ) {
243
+ } elseif (count ($ types ) = == 1 ) {
249
244
return $ types [0 ];
250
245
}
246
+
251
247
return new Compound ($ types );
252
248
}
253
249
254
250
/**
255
251
* resolve the given type into a type object
256
252
*
257
- * @param string $type the type string, representing a single type
258
- * @param Context $context
259
- * @return Type|Array_|Object_
253
+ * @param string $type the type string, representing a single type
254
+ * @return Type|\Array_|\Object_
260
255
*/
261
256
private function resolveSingleType ($ type , Context $ context )
262
257
{
@@ -276,6 +271,7 @@ private function resolveSingleType($type, Context $context)
276
271
'Unable to resolve type " ' . $ type . '", there is no known method to resolve it '
277
272
);
278
273
}
274
+
279
275
// @codeCoverageIgnoreEnd
280
276
}
281
277
@@ -284,8 +280,6 @@ private function resolveSingleType($type, Context $context)
284
280
*
285
281
* @param string $keyword
286
282
* @param string $typeClassName
287
- *
288
- * @return void
289
283
*/
290
284
public function addKeyword ($ keyword , $ typeClassName )
291
285
{
@@ -357,8 +351,6 @@ private function isFqsen($type)
357
351
* Resolves the given typed array string (i.e. `string[]`) into an Array object with the right types set.
358
352
*
359
353
* @param string $type
360
- * @param Context $context
361
- *
362
354
* @return Array_
363
355
*/
364
356
private function resolveTypedArray ($ type , Context $ context )
@@ -396,20 +388,17 @@ private function resolveTypedObject($type, Context $context = null)
396
388
/**
397
389
* Resolves the collection values and keys
398
390
*
399
- * @param \ArrayIterator $tokens
400
- * @param Type $classType
401
- * @param Context $context
402
- * @return Array_|Collection
391
+ * @return Array_|\Collection
403
392
*/
404
- private function resolveCollection (\ArrayIterator $ tokens , Type $ classType , Context $ context ) {
405
-
406
- $ isArray = ('array ' == (string ) $ classType );
393
+ private function resolveCollection (\ArrayIterator $ tokens , Type $ classType , Context $ context )
394
+ {
395
+ $ isArray = ('array ' === (string ) $ classType );
407
396
408
397
// allow only "array" or class name before "<"
409
398
if (!$ isArray
410
399
&& (! $ classType instanceof Object_ || $ classType ->getFqsen () === null )) {
411
400
throw new \RuntimeException (
412
- $ classType. ' is not a collection '
401
+ $ classType . ' is not a collection '
413
402
);
414
403
}
415
404
@@ -418,7 +407,7 @@ private function resolveCollection(\ArrayIterator $tokens, Type $classType, Cont
418
407
$ valueType = $ this ->parseTypes ($ tokens , $ context , self ::PARSER_IN_COLLECTION_EXPRESSION );
419
408
$ keyType = null ;
420
409
421
- if ($ tokens ->current () == ', ' ) {
410
+ if ($ tokens ->current () === ', ' ) {
422
411
// if we have a comma, then we just parsed the key type, not the value type
423
412
$ keyType = $ valueType ;
424
413
if ($ isArray ) {
@@ -432,8 +421,9 @@ private function resolveCollection(\ArrayIterator $tokens, Type $classType, Cont
432
421
'An array can have only integers or strings as keys '
433
422
);
434
423
}
424
+
435
425
if ($ keyType instanceof Compound) {
436
- foreach ($ keyType ->getIterator () as $ item ) {
426
+ foreach ($ keyType ->getIterator () as $ item ) {
437
427
if (! $ item instanceof String_ &&
438
428
! $ item instanceof Integer
439
429
) {
@@ -444,22 +434,24 @@ private function resolveCollection(\ArrayIterator $tokens, Type $classType, Cont
444
434
}
445
435
}
446
436
}
437
+
447
438
$ tokens ->next ();
448
439
// now let's parse the value type
449
440
$ valueType = $ this ->parseTypes ($ tokens , $ context , self ::PARSER_IN_COLLECTION_EXPRESSION );
450
441
}
451
442
452
443
if ($ tokens ->current () !== '> ' ) {
453
- if ($ tokens ->current () == '' ) {
444
+ if ($ tokens ->current () === '' ) {
454
445
throw new \RuntimeException (
455
446
'Collection: ">" is missing '
456
447
);
457
448
}
458
449
459
450
throw new \RuntimeException (
460
- 'Unexpected character " ' . $ tokens ->current (). '", ">" is missing '
451
+ 'Unexpected character " ' . $ tokens ->current () . '", ">" is missing '
461
452
);
462
453
}
454
+
463
455
if ($ isArray ) {
464
456
return new Array_ ($ valueType , $ keyType );
465
457
}
@@ -470,8 +462,6 @@ private function resolveCollection(\ArrayIterator $tokens, Type $classType, Cont
470
462
}
471
463
472
464
/**
473
- * @param Object_ $object
474
- * @param Type $valueType
475
465
* @param Type|null $keyType
476
466
* @return Collection
477
467
*/
0 commit comments