@@ -114,21 +114,17 @@ class ArrayType extends SimpleType {
114
114
/** @var Type */
115
115
public $ valueType ;
116
116
117
- /** @var bool */
118
- public $ supportsReferences ;
119
-
120
117
public static function createGenericArray (): self
121
118
{
122
- return new ArrayType (Type::fromString ("int|string " ), Type::fromString ("mixed " ), false );
119
+ return new ArrayType (Type::fromString ("int|string " ), Type::fromString ("mixed " ));
123
120
}
124
121
125
- public function __construct (Type $ keyType , Type $ valueType, bool $ supportsReferences )
122
+ public function __construct (Type $ keyType , Type $ valueType )
126
123
{
127
124
parent ::__construct ("array " , true );
128
125
129
126
$ this ->keyType = $ keyType ;
130
127
$ this ->valueType = $ valueType ;
131
- $ this ->supportsReferences = $ supportsReferences ;
132
128
}
133
129
134
130
public function toOptimizerTypeMask (): string {
@@ -138,10 +134,6 @@ public function toOptimizerTypeMask(): string {
138
134
$ this ->valueType ->toOptimizerTypeMaskForArrayValue (),
139
135
];
140
136
141
- if ($ this ->supportsReferences ) {
142
- $ typeMasks [] = "MAY_BE_ARRAY_OF_REF " ;
143
- }
144
-
145
137
return implode ("| " , $ typeMasks );
146
138
}
147
139
@@ -153,8 +145,7 @@ public function equals(SimpleType $other): bool {
153
145
assert (get_class ($ other ) === self ::class);
154
146
155
147
return Type::equals ($ this ->keyType , $ other ->keyType ) &&
156
- Type::equals ($ this ->valueType , $ other ->valueType ) &&
157
- $ this ->supportsReferences === $ other ->supportsReferences ;
148
+ Type::equals ($ this ->valueType , $ other ->valueType );
158
149
}
159
150
}
160
151
@@ -218,13 +209,13 @@ public static function fromString(string $typeString): SimpleType
218
209
$ matches = [];
219
210
$ isArray = preg_match ("/(.*)\s*\[\s*\]/ " , $ typeString , $ matches );
220
211
if ($ isArray ) {
221
- return new ArrayType (Type::fromString ("int " ), Type::fromString ($ matches [1 ]), false );
212
+ return new ArrayType (Type::fromString ("int " ), Type::fromString ($ matches [1 ]));
222
213
}
223
214
224
215
$ matches = [];
225
- $ isArray = preg_match ("/array(-ref)? \s*<\s*([A-Za-z0-9_-|]+)\s*,\s*([A-Za-z0-9_-|]+)\s*>/i " , $ typeString , $ matches );
216
+ $ isArray = preg_match ("/array\s*<\s*([A-Za-z0-9_-|]+)\s*,\s*([A-Za-z0-9_-|]+)\s*>/i " , $ typeString , $ matches );
226
217
if ($ isArray ) {
227
- return new ArrayType (Type::fromString ($ matches [2 ]), Type::fromString ($ matches [3 ]), ! empty ( $ matches [ 1 ]));
218
+ return new ArrayType (Type::fromString ($ matches [1 ]), Type::fromString ($ matches [2 ]));
228
219
}
229
220
230
221
return new SimpleType ($ typeString , false );
@@ -400,31 +391,33 @@ class Type {
400
391
/** @var SimpleType[] */
401
392
public $ types ;
402
393
403
- /**
404
- * @param SimpleType[] $types
405
- */
406
- public function __construct (array $ types ) {
407
- $ this ->types = $ types ;
408
- }
394
+ /** @var bool */
395
+ public $ isReferenceSupported ;
409
396
410
397
public static function fromNode (Node $ node ): Type {
411
398
if ($ node instanceof Node \UnionType) {
412
- return new Type (array_map (['SimpleType ' , 'fromNode ' ], $ node ->types ));
399
+ return new Type (array_map (['SimpleType ' , 'fromNode ' ], $ node ->types ), false );
413
400
}
401
+
414
402
if ($ node instanceof Node \NullableType) {
415
- return new Type ([
416
- SimpleType::fromNode ($ node ->type ),
417
- SimpleType::null (),
418
- ]);
403
+ return new Type (
404
+ [
405
+ SimpleType::fromNode ($ node ->type ),
406
+ SimpleType::null (),
407
+ ],
408
+ false
409
+ );
419
410
}
420
- return new Type ([SimpleType::fromNode ($ node )]);
411
+
412
+ return new Type ([SimpleType::fromNode ($ node )], false );
421
413
}
422
414
423
415
public static function fromString (string $ typeString ): self {
424
416
$ typeString .= "| " ;
425
417
$ simpleTypes = [];
426
418
$ simpleTypeOffset = 0 ;
427
419
$ inArray = false ;
420
+ $ referenceSupported = false ;
428
421
429
422
$ typeStringLength = strlen ($ typeString );
430
423
for ($ i = 0 ; $ i < $ typeStringLength ; $ i ++) {
@@ -446,13 +439,26 @@ public static function fromString(string $typeString): self {
446
439
447
440
if ($ char === "| " ) {
448
441
$ simpleTypeName = trim (substr ($ typeString , $ simpleTypeOffset , $ i - $ simpleTypeOffset ));
449
- $ simpleTypes [] = SimpleType::fromString ($ simpleTypeName );
442
+
443
+ if (strtolower ($ simpleTypeName ) === "ref " ) {
444
+ $ referenceSupported = true ;
445
+ } else {
446
+ $ simpleTypes [] = SimpleType::fromString ($ simpleTypeName );
447
+ }
450
448
451
449
$ simpleTypeOffset = $ i + 1 ;
452
450
}
453
451
}
454
452
455
- return new Type ($ simpleTypes );
453
+ return new Type ($ simpleTypes , $ referenceSupported );
454
+ }
455
+
456
+ /**
457
+ * @param SimpleType[] $types
458
+ */
459
+ private function __construct (array $ types , bool $ isReferenceSupported ) {
460
+ $ this ->types = $ types ;
461
+ $ this ->isReferenceSupported = $ isReferenceSupported ;
456
462
}
457
463
458
464
public function isScalar (): bool {
@@ -476,9 +482,15 @@ public function isNullable(): bool {
476
482
}
477
483
478
484
public function getWithoutNull (): Type {
479
- return new Type (array_filter ($ this ->types , function (SimpleType $ type ) {
480
- return !$ type ->isNull ();
481
- }));
485
+ return new Type (
486
+ array_filter (
487
+ $ this ->types ,
488
+ function (SimpleType $ type ) {
489
+ return !$ type ->isNull ();
490
+ }
491
+ ),
492
+ false
493
+ );
482
494
}
483
495
484
496
public function tryToSimpleType (): ?SimpleType {
@@ -529,6 +541,10 @@ public function toOptimizerTypeMaskForArrayValue(): string {
529
541
$ typeMasks [] = $ type ->toOptimizerTypeMaskForArrayValue ();
530
542
}
531
543
544
+ if ($ this ->isReferenceSupported ) {
545
+ $ typeMasks [] = "MAY_BE_ARRAY_OF_REF " ;
546
+ }
547
+
532
548
return implode ("| " , $ typeMasks );
533
549
}
534
550
@@ -570,6 +586,10 @@ public static function equals(?Type $a, ?Type $b): bool {
570
586
}
571
587
}
572
588
589
+ if ($ a ->isReferenceSupported !== $ b ->isReferenceSupported ) {
590
+ return false ;
591
+ }
592
+
573
593
return true ;
574
594
}
575
595
0 commit comments