@@ -120,21 +120,17 @@ class ArrayType extends SimpleType {
120
120
/** @var Type */
121
121
public $ valueType ;
122
122
123
- /** @var bool */
124
- public $ supportsReferences ;
125
-
126
123
public static function createGenericArray (): self
127
124
{
128
- return new ArrayType (Type::fromString ("int|string " ), Type::fromString ("mixed " ), false );
125
+ return new ArrayType (Type::fromString ("int|string " ), Type::fromString ("mixed " ));
129
126
}
130
127
131
- public function __construct (Type $ keyType , Type $ valueType, bool $ supportsReferences )
128
+ public function __construct (Type $ keyType , Type $ valueType )
132
129
{
133
130
parent ::__construct ("array " , true );
134
131
135
132
$ this ->keyType = $ keyType ;
136
133
$ this ->valueType = $ valueType ;
137
- $ this ->supportsReferences = $ supportsReferences ;
138
134
}
139
135
140
136
public function toOptimizerTypeMask (): string {
@@ -144,10 +140,6 @@ public function toOptimizerTypeMask(): string {
144
140
$ this ->valueType ->toOptimizerTypeMaskForArrayValue (),
145
141
];
146
142
147
- if ($ this ->supportsReferences ) {
148
- $ typeMasks [] = "MAY_BE_ARRAY_OF_REF " ;
149
- }
150
-
151
143
return implode ("| " , $ typeMasks );
152
144
}
153
145
@@ -159,8 +151,7 @@ public function equals(SimpleType $other): bool {
159
151
assert (get_class ($ other ) === self ::class);
160
152
161
153
return Type::equals ($ this ->keyType , $ other ->keyType ) &&
162
- Type::equals ($ this ->valueType , $ other ->valueType ) &&
163
- $ this ->supportsReferences === $ other ->supportsReferences ;
154
+ Type::equals ($ this ->valueType , $ other ->valueType );
164
155
}
165
156
}
166
157
@@ -224,13 +215,13 @@ public static function fromString(string $typeString): SimpleType
224
215
$ matches = [];
225
216
$ isArray = preg_match ("/(.*)\s*\[\s*\]/ " , $ typeString , $ matches );
226
217
if ($ isArray ) {
227
- return new ArrayType (Type::fromString ("int " ), Type::fromString ($ matches [1 ]), false );
218
+ return new ArrayType (Type::fromString ("int " ), Type::fromString ($ matches [1 ]));
228
219
}
229
220
230
221
$ matches = [];
231
- $ isArray = preg_match ("/array(-ref)? \s*<\s*([A-Za-z0-9_-|]+)\s*,\s*([A-Za-z0-9_-|]+)\s*>/i " , $ typeString , $ matches );
222
+ $ isArray = preg_match ("/array\s*<\s*([A-Za-z0-9_-|]+)\s*,\s*([A-Za-z0-9_-|]+)\s*>/i " , $ typeString , $ matches );
232
223
if ($ isArray ) {
233
- return new ArrayType (Type::fromString ($ matches [2 ]), Type::fromString ($ matches [3 ]), ! empty ( $ matches [ 1 ]));
224
+ return new ArrayType (Type::fromString ($ matches [1 ]), Type::fromString ($ matches [2 ]));
234
225
}
235
226
236
227
return new SimpleType ($ typeString , false );
@@ -406,31 +397,33 @@ class Type {
406
397
/** @var SimpleType[] */
407
398
public $ types ;
408
399
409
- /**
410
- * @param SimpleType[] $types
411
- */
412
- public function __construct (array $ types ) {
413
- $ this ->types = $ types ;
414
- }
400
+ /** @var bool */
401
+ public $ isReferenceSupported ;
415
402
416
403
public static function fromNode (Node $ node ): Type {
417
404
if ($ node instanceof Node \UnionType) {
418
- return new Type (array_map (['SimpleType ' , 'fromNode ' ], $ node ->types ));
405
+ return new Type (array_map (['SimpleType ' , 'fromNode ' ], $ node ->types ), false );
419
406
}
407
+
420
408
if ($ node instanceof Node \NullableType) {
421
- return new Type ([
422
- SimpleType::fromNode ($ node ->type ),
423
- SimpleType::null (),
424
- ]);
409
+ return new Type (
410
+ [
411
+ SimpleType::fromNode ($ node ->type ),
412
+ SimpleType::null (),
413
+ ],
414
+ false
415
+ );
425
416
}
426
- return new Type ([SimpleType::fromNode ($ node )]);
417
+
418
+ return new Type ([SimpleType::fromNode ($ node )], false );
427
419
}
428
420
429
421
public static function fromString (string $ typeString ): self {
430
422
$ typeString .= "| " ;
431
423
$ simpleTypes = [];
432
424
$ simpleTypeOffset = 0 ;
433
425
$ inArray = false ;
426
+ $ referenceSupported = false ;
434
427
435
428
$ typeStringLength = strlen ($ typeString );
436
429
for ($ i = 0 ; $ i < $ typeStringLength ; $ i ++) {
@@ -452,13 +445,26 @@ public static function fromString(string $typeString): self {
452
445
453
446
if ($ char === "| " ) {
454
447
$ simpleTypeName = trim (substr ($ typeString , $ simpleTypeOffset , $ i - $ simpleTypeOffset ));
455
- $ simpleTypes [] = SimpleType::fromString ($ simpleTypeName );
448
+
449
+ if (strtolower ($ simpleTypeName ) === "ref " ) {
450
+ $ referenceSupported = true ;
451
+ } else {
452
+ $ simpleTypes [] = SimpleType::fromString ($ simpleTypeName );
453
+ }
456
454
457
455
$ simpleTypeOffset = $ i + 1 ;
458
456
}
459
457
}
460
458
461
- return new Type ($ simpleTypes );
459
+ return new Type ($ simpleTypes , $ referenceSupported );
460
+ }
461
+
462
+ /**
463
+ * @param SimpleType[] $types
464
+ */
465
+ private function __construct (array $ types , bool $ isReferenceSupported ) {
466
+ $ this ->types = $ types ;
467
+ $ this ->isReferenceSupported = $ isReferenceSupported ;
462
468
}
463
469
464
470
public function isScalar (): bool {
@@ -482,9 +488,15 @@ public function isNullable(): bool {
482
488
}
483
489
484
490
public function getWithoutNull (): Type {
485
- return new Type (array_filter ($ this ->types , function (SimpleType $ type ) {
486
- return !$ type ->isNull ();
487
- }));
491
+ return new Type (
492
+ array_filter (
493
+ $ this ->types ,
494
+ function (SimpleType $ type ) {
495
+ return !$ type ->isNull ();
496
+ }
497
+ ),
498
+ false
499
+ );
488
500
}
489
501
490
502
public function tryToSimpleType (): ?SimpleType {
@@ -535,6 +547,10 @@ public function toOptimizerTypeMaskForArrayValue(): string {
535
547
$ typeMasks [] = $ type ->toOptimizerTypeMaskForArrayValue ();
536
548
}
537
549
550
+ if ($ this ->isReferenceSupported ) {
551
+ $ typeMasks [] = "MAY_BE_ARRAY_OF_REF " ;
552
+ }
553
+
538
554
return implode ("| " , $ typeMasks );
539
555
}
540
556
@@ -576,6 +592,10 @@ public static function equals(?Type $a, ?Type $b): bool {
576
592
}
577
593
}
578
594
595
+ if ($ a ->isReferenceSupported !== $ b ->isReferenceSupported ) {
596
+ return false ;
597
+ }
598
+
579
599
return true ;
580
600
}
581
601
0 commit comments