/
Type.php
1458 lines (1191 loc) · 44.8 KB
/
Type.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
namespace Psalm;
use Psalm\Exception\TypeParseTreeException;
use Psalm\Storage\FunctionLikeParameter;
use Psalm\Type\Atomic;
use Psalm\Type\Atomic\ObjectLike;
use Psalm\Type\Atomic\TArray;
use Psalm\Type\Atomic\TArrayKey;
use Psalm\Type\Atomic\TBool;
use Psalm\Type\Atomic\TCallable;
use Psalm\Type\Atomic\TClassString;
use Psalm\Type\Atomic\TEmpty;
use Psalm\Type\Atomic\TFalse;
use Psalm\Type\Atomic\TFloat;
use Psalm\Type\Atomic\TGenericObject;
use Psalm\Type\Atomic\TTemplateParam;
use Psalm\Type\Atomic\TInt;
use Psalm\Type\Atomic\TIterable;
use Psalm\Type\Atomic\TLiteralClassString;
use Psalm\Type\Atomic\TLiteralFloat;
use Psalm\Type\Atomic\TLiteralInt;
use Psalm\Type\Atomic\TLiteralString;
use Psalm\Type\Atomic\TMixed;
use Psalm\Type\Atomic\TNamedObject;
use Psalm\Type\Atomic\TNull;
use Psalm\Type\Atomic\TNumeric;
use Psalm\Type\Atomic\TObject;
use Psalm\Type\Atomic\TObjectWithProperties;
use Psalm\Type\Atomic\TResource;
use Psalm\Type\Atomic\TSingleLetter;
use Psalm\Type\Atomic\TString;
use Psalm\Type\Atomic\TTrue;
use Psalm\Type\Atomic\TVoid;
use Psalm\Internal\Type\ParseTree;
use Psalm\Internal\Type\TypeCombination;
use Psalm\Type\Union;
abstract class Type
{
/**
* @var array<string, bool>
*/
const PSALM_RESERVED_WORDS = [
'int' => true,
'string' => true,
'float' => true,
'bool' => true,
'false' => true,
'true' => true,
'object' => true,
'empty' => true,
'callable' => true,
'array' => true,
'non-empty-array' => true,
'iterable' => true,
'null' => true,
'mixed' => true,
'numeric-string' => true,
'class-string' => true,
'callable-string' => true,
'trait-string' => true,
'mysql-escaped-string' => true,
'html-escaped-string' => true,
'boolean' => true,
'integer' => true,
'double' => true,
'real' => true,
'resource' => true,
'void' => true,
'self' => true,
'static' => true,
'scalar' => true,
'numeric' => true,
'no-return' => true,
'never-return' => true,
'never-returns' => true,
'array-key' => true,
'key-of' => true,
'value-of' => true,
];
/**
* @var array<string, array<int, string>>
*/
private static $memoized_tokens = [];
/**
* Parses a string type representation
*
* @param string $type_string
* @param array{int,int}|null $php_version
* @param array<string, array<string, array{Type\Union}>> $template_type_map
*
* @return Union
*/
public static function parseString(
$type_string,
array $php_version = null,
array $template_type_map = []
) {
return self::parseTokens(self::tokenize($type_string), $php_version, $template_type_map);
}
/**
* Parses a string type representation
*
* @param array<int, string> $type_tokens
* @param array{int,int}|null $php_version
* @param array<string, array<string, array{Type\Union}>> $template_type_map
*
* @return Union
*/
public static function parseTokens(
array $type_tokens,
array $php_version = null,
array $template_type_map = []
) {
if (count($type_tokens) === 1) {
$only_token = $type_tokens[0];
// Note: valid identifiers can include class names or $this
if (!preg_match('@^(\$this|\\\\?[a-zA-Z_\x7f-\xff][\\\\\-0-9a-zA-Z_\x7f-\xff]*)$@', $only_token)) {
throw new TypeParseTreeException("Invalid type '$only_token'");
}
$only_token = self::fixScalarTerms($only_token, $php_version);
return new Union([Atomic::create($only_token, $php_version, $template_type_map)]);
}
$parse_tree = ParseTree::createFromTokens($type_tokens);
$parsed_type = self::getTypeFromTree($parse_tree, $php_version, $template_type_map);
if (!($parsed_type instanceof Union)) {
$parsed_type = new Union([$parsed_type]);
}
return $parsed_type;
}
/**
* @param string $type_string
* @param array{int,int}|null $php_version
*
* @return string
*/
private static function fixScalarTerms($type_string, array $php_version = null)
{
$type_string_lc = strtolower($type_string);
switch ($type_string_lc) {
case 'int':
case 'void':
case 'float':
case 'string':
case 'bool':
case 'callable':
case 'iterable':
case 'array':
case 'object':
case 'numeric':
case 'true':
case 'false':
case 'null':
case 'mixed':
case 'resource':
return $type_string_lc;
}
switch ($type_string) {
case 'boolean':
return $php_version !== null ? $type_string : 'bool';
case 'integer':
return $php_version !== null ? $type_string : 'int';
case 'double':
case 'real':
return $php_version !== null ? $type_string : 'float';
}
return $type_string;
}
/**
* @param ParseTree $parse_tree
* @param array{int,int}|null $php_version
* @param array<string, array<string, array{Type\Union}>> $template_type_map
*
* @return Atomic|TArray|TGenericObject|ObjectLike|Union
*/
public static function getTypeFromTree(
ParseTree $parse_tree,
array $php_version = null,
array $template_type_map = []
) {
if ($parse_tree instanceof ParseTree\GenericTree) {
$generic_type = $parse_tree->value;
$generic_params = array_map(
/**
* @return Union
*/
function (ParseTree $child_tree) use ($template_type_map) {
$tree_type = self::getTypeFromTree($child_tree, null, $template_type_map);
return $tree_type instanceof Union ? $tree_type : new Union([$tree_type]);
},
$parse_tree->children
);
$generic_type_value = self::fixScalarTerms($generic_type);
if (($generic_type_value === 'array' || $generic_type_value === 'non-empty-array')
&& count($generic_params) === 1
) {
array_unshift($generic_params, new Union([new TArrayKey]));
} elseif (($generic_type_value === 'iterable' || $generic_type_value === 'Traversable')
&& count($generic_params) === 1
) {
array_unshift($generic_params, new Union([new TMixed]));
} elseif ($generic_type_value === 'Generator') {
if (count($generic_params) === 1) {
array_unshift($generic_params, new Union([new TMixed]));
}
for ($i = 0, $l = 4 - count($generic_params); $i < $l; $i++) {
$generic_params[] = new Union([new TMixed]);
}
}
if (!$generic_params) {
throw new TypeParseTreeException('No generic params provided for type');
}
if ($generic_type_value === 'array') {
return new TArray($generic_params);
}
if ($generic_type_value === 'non-empty-array') {
return new Type\Atomic\TNonEmptyArray($generic_params);
}
if ($generic_type_value === 'iterable') {
return new TIterable($generic_params);
}
if ($generic_type_value === 'class-string') {
$class_name = (string) $generic_params[0];
if (isset($template_type_map[$class_name])) {
$first_class = array_keys($template_type_map[$class_name])[0];
return self::getGenericParamClass(
$class_name,
$template_type_map[$class_name][$first_class][0],
$first_class
);
}
$param_union_types = array_values($generic_params[0]->getTypes());
if (count($param_union_types) > 1) {
throw new TypeParseTreeException('Union types are not allowed in class string param');
}
if (!$param_union_types[0] instanceof TNamedObject) {
throw new TypeParseTreeException('Class string param should be a named object');
}
return new TClassString($class_name, $param_union_types[0]);
}
if ($generic_type_value === 'key-of') {
$param_name = (string) $generic_params[0];
if (isset($template_type_map[$param_name])) {
$defining_class = array_keys($template_type_map[$param_name])[0];
return new Atomic\TTemplateKeyOf(
$param_name,
$defining_class
);
}
$param_union_types = array_values($generic_params[0]->getTypes());
if (count($param_union_types) > 1) {
throw new TypeParseTreeException('Union types are not allowed in key-of type');
}
if (!$param_union_types[0] instanceof Atomic\TScalarClassConstant) {
throw new TypeParseTreeException(
'Untemplated key-of param ' . $param_name . ' should be a class constant'
);
}
return new Atomic\TKeyOfClassConstant(
$param_union_types[0]->fq_classlike_name,
$param_union_types[0]->const_name
);
}
if ($generic_type_value === 'value-of') {
$param_name = (string) $generic_params[0];
if (isset($template_type_map[$param_name])) {
$defining_class = array_keys($template_type_map[$param_name])[0];
return new Atomic\TTemplateKeyOf(
$param_name,
$defining_class
);
}
$param_union_types = array_values($generic_params[0]->getTypes());
if (count($param_union_types) > 1) {
throw new TypeParseTreeException('Union types are not allowed in value-of type');
}
if (!$param_union_types[0] instanceof Atomic\TScalarClassConstant) {
throw new TypeParseTreeException(
'Untemplated value-of param ' . $param_name . ' should be a class constant'
);
}
return new Atomic\TValueOfClassConstant(
$param_union_types[0]->fq_classlike_name,
$param_union_types[0]->const_name
);
}
if (isset(self::PSALM_RESERVED_WORDS[$generic_type_value])
&& $generic_type_value !== 'self'
&& $generic_type_value !== 'static'
) {
throw new TypeParseTreeException('Cannot create generic object with reserved word');
}
return new TGenericObject($generic_type_value, $generic_params);
}
if ($parse_tree instanceof ParseTree\UnionTree) {
$has_null = false;
$atomic_types = [];
foreach ($parse_tree->children as $child_tree) {
if ($child_tree instanceof ParseTree\NullableTree) {
$atomic_type = self::getTypeFromTree($child_tree->children[0], null, $template_type_map);
$has_null = true;
} else {
$atomic_type = self::getTypeFromTree($child_tree, null, $template_type_map);
}
if ($atomic_type instanceof Union) {
foreach ($atomic_type->getTypes() as $type) {
$atomic_types[] = $type;
}
continue;
}
$atomic_types[] = $atomic_type;
}
if ($has_null) {
$atomic_types[] = new TNull;
}
return TypeCombination::combineTypes($atomic_types);
}
if ($parse_tree instanceof ParseTree\IntersectionTree) {
$intersection_types = array_map(
/**
* @return Atomic
*/
function (ParseTree $child_tree) use ($template_type_map) {
$atomic_type = self::getTypeFromTree($child_tree, null, $template_type_map);
if (!$atomic_type instanceof Atomic) {
throw new TypeParseTreeException(
'Intersection types cannot contain unions'
);
}
return $atomic_type;
},
$parse_tree->children
);
$keyed_intersection_types = [];
foreach ($intersection_types as $intersection_type) {
if (!$intersection_type instanceof TNamedObject
&& !$intersection_type instanceof TTemplateParam
&& !$intersection_type instanceof TIterable
) {
throw new TypeParseTreeException(
'Intersection types must all be objects, ' . get_class($intersection_type) . ' provided'
);
}
$keyed_intersection_types[
$intersection_type instanceof TIterable
? $intersection_type->getId()
: $intersection_type->getKey()
] = $intersection_type;
}
$first_type = array_shift($keyed_intersection_types);
$first_type->extra_types = $keyed_intersection_types;
return $first_type;
}
if ($parse_tree instanceof ParseTree\ObjectLikeTree) {
$properties = [];
$type = $parse_tree->value;
foreach ($parse_tree->children as $i => $property_branch) {
if (!$property_branch instanceof ParseTree\ObjectLikePropertyTree) {
$property_type = self::getTypeFromTree($property_branch, null, $template_type_map);
$property_maybe_undefined = false;
$property_key = (string)$i;
} elseif (count($property_branch->children) === 1) {
$property_type = self::getTypeFromTree($property_branch->children[0], null, $template_type_map);
$property_maybe_undefined = $property_branch->possibly_undefined;
$property_key = $property_branch->value;
} else {
throw new TypeParseTreeException(
'Missing property type'
);
}
if (!$property_type instanceof Union) {
$property_type = new Union([$property_type]);
}
if ($property_maybe_undefined) {
$property_type->possibly_undefined = true;
}
$properties[$property_key] = $property_type;
}
if ($type !== 'array' && $type !== 'object') {
throw new TypeParseTreeException('Unexpected brace character');
}
if (!$properties) {
throw new TypeParseTreeException('No properties supplied for ObjectLike');
}
if ($type === 'object') {
return new TObjectWithProperties($properties);
}
return new ObjectLike($properties);
}
if ($parse_tree instanceof ParseTree\CallableWithReturnTypeTree) {
$callable_type = self::getTypeFromTree($parse_tree->children[0], null, $template_type_map);
if (!$callable_type instanceof TCallable && !$callable_type instanceof Type\Atomic\Fn) {
throw new \InvalidArgumentException('Parsing callable tree node should return TCallable');
}
if (!isset($parse_tree->children[1])) {
throw new TypeParseTreeException('Invalid return type');
}
$return_type = self::getTypeFromTree($parse_tree->children[1], null, $template_type_map);
$callable_type->return_type = $return_type instanceof Union ? $return_type : new Union([$return_type]);
return $callable_type;
}
if ($parse_tree instanceof ParseTree\CallableTree) {
$params = array_map(
/**
* @return FunctionLikeParameter
*/
function (ParseTree $child_tree) use ($template_type_map) {
$is_variadic = false;
$is_optional = false;
if ($child_tree instanceof ParseTree\CallableParamTree) {
$tree_type = self::getTypeFromTree($child_tree->children[0], null, $template_type_map);
$is_variadic = $child_tree->variadic;
$is_optional = $child_tree->has_default;
} else {
if ($child_tree instanceof ParseTree\Value && strpos($child_tree->value, '$') > 0) {
$child_tree->value = preg_replace('/(.+)\$.*/', '$1', $child_tree->value);
}
$tree_type = self::getTypeFromTree($child_tree, null, $template_type_map);
}
$tree_type = $tree_type instanceof Union ? $tree_type : new Union([$tree_type]);
$param = new FunctionLikeParameter(
'',
false,
$tree_type,
null,
null,
$is_optional,
false,
$is_variadic
);
// type is not authoratative
$param->signature_type = null;
return $param;
},
$parse_tree->children
);
if (in_array(strtolower($parse_tree->value), ['closure', '\closure'], true)) {
return new Type\Atomic\Fn('Closure', $params);
}
return new TCallable($parse_tree->value, $params);
}
if ($parse_tree instanceof ParseTree\EncapsulationTree) {
return self::getTypeFromTree($parse_tree->children[0], null, $template_type_map);
}
if ($parse_tree instanceof ParseTree\NullableTree) {
if (!isset($parse_tree->children[0])) {
throw new TypeParseTreeException('Misplaced question mark');
}
$non_nullable_type = self::getTypeFromTree($parse_tree->children[0], null, $template_type_map);
if ($non_nullable_type instanceof Union) {
$non_nullable_type->addType(new TNull);
return $non_nullable_type;
}
if ($non_nullable_type instanceof Atomic) {
return TypeCombination::combineTypes([
new TNull,
$non_nullable_type
]);
}
throw new \UnexpectedValueException(
'Was expecting an atomic or union type, got ' . get_class($non_nullable_type)
);
}
if ($parse_tree instanceof ParseTree\MethodTree
|| $parse_tree instanceof ParseTree\MethodWithReturnTypeTree
) {
throw new TypeParseTreeException('Misplaced brackets');
}
if ($parse_tree instanceof ParseTree\IndexedAccessTree) {
if (!isset($parse_tree->children[0]) || !$parse_tree->children[0] instanceof ParseTree\Value) {
throw new TypeParseTreeException('Unrecognised indexed access');
}
$offset_param_name = $parse_tree->value;
$array_param_name = $parse_tree->children[0]->value;
if (!isset($template_type_map[$offset_param_name])) {
throw new TypeParseTreeException('Unrecognised template param ' . $offset_param_name);
}
if (!isset($template_type_map[$array_param_name])) {
throw new TypeParseTreeException('Unrecognised template param ' . $array_param_name);
}
$offset_template_data = $template_type_map[$offset_param_name];
$offset_defining_class = array_keys($offset_template_data)[0];
if (!$offset_defining_class && $offset_template_data[''][0]->isSingle()) {
$offset_template_type = array_values($offset_template_data[''][0]->getTypes())[0];
if ($offset_template_type instanceof Type\Atomic\TTemplateKeyOf) {
$offset_defining_class = (string) $offset_template_type->defining_class;
}
}
$array_defining_class = array_keys($template_type_map[$array_param_name])[0];
if ($offset_defining_class !== $array_defining_class) {
throw new TypeParseTreeException('Template params are defined in different locations');
}
return new Atomic\TTemplateIndexedAccess(
$array_param_name,
$offset_param_name,
$array_defining_class
);
}
if (!$parse_tree instanceof ParseTree\Value) {
throw new \InvalidArgumentException('Unrecognised parse tree type ' . get_class($parse_tree));
}
if ($parse_tree->value[0] === '"' || $parse_tree->value[0] === '\'') {
return new TLiteralString(substr($parse_tree->value, 1, -1));
}
if (strpos($parse_tree->value, '::')) {
list($fq_classlike_name, $const_name) = explode('::', $parse_tree->value);
if (isset($template_type_map[$fq_classlike_name]) && $const_name === 'class') {
$first_class = array_keys($template_type_map[$fq_classlike_name])[0];
return self::getGenericParamClass(
$fq_classlike_name,
$template_type_map[$fq_classlike_name][$first_class][0],
$first_class
);
}
if ($const_name === 'class') {
return new Atomic\TLiteralClassString($fq_classlike_name);
}
return new Atomic\TScalarClassConstant($fq_classlike_name, $const_name);
}
if (preg_match('/^\-?(0|[1-9][0-9]*)(\.[0-9]{1,})$/', $parse_tree->value)) {
return new TLiteralFloat((float) $parse_tree->value);
}
if (preg_match('/^\-?(0|[1-9][0-9]*)$/', $parse_tree->value)) {
return new TLiteralInt((int) $parse_tree->value);
}
if (!preg_match('@^(\$this|\\\\?[a-zA-Z_\x7f-\xff][\\\\\-0-9a-zA-Z_\x7f-\xff]*)$@', $parse_tree->value)) {
throw new TypeParseTreeException('Invalid type \'' . $parse_tree->value . '\'');
}
$atomic_type = self::fixScalarTerms($parse_tree->value, $php_version);
return Atomic::create($atomic_type, $php_version, $template_type_map);
}
private static function getGenericParamClass(
string $param_name,
Union $as,
string $defining_class = null
) : Atomic\TTemplateParamClass {
if ($as->hasMixed()) {
return new Atomic\TTemplateParamClass(
$param_name,
'object',
null,
$defining_class
);
}
if (!$as->isSingle()) {
throw new TypeParseTreeException(
'Invalid templated classname \'' . $as . '\''
);
}
foreach ($as->getTypes() as $t) {
if ($t instanceof TObject) {
return new Atomic\TTemplateParamClass(
$param_name,
'object',
null,
$defining_class
);
}
if ($t instanceof TIterable) {
$traversable = new TGenericObject(
'Traversable',
$t->type_params
);
$as->substitute(new Union([$t]), new Union([$traversable]));
return new Atomic\TTemplateParamClass(
$param_name,
$traversable->value,
$traversable,
$defining_class
);
}
if (!$t instanceof TNamedObject) {
throw new TypeParseTreeException(
'Invalid templated classname \'' . $t . '\''
);
}
return new Atomic\TTemplateParamClass(
$param_name,
$t->value,
$t,
$defining_class
);
}
throw new \LogicException('Should never get here');
}
/**
* @param string $string_type
* @param bool $ignore_space
*
* @return array<int,string>
*/
public static function tokenize($string_type, $ignore_space = true)
{
$type_tokens = [''];
$was_char = false;
$quote_char = null;
$escaped = false;
if (isset(self::$memoized_tokens[$string_type])) {
return self::$memoized_tokens[$string_type];
}
// index of last type token
$rtc = 0;
$chars = str_split($string_type);
for ($i = 0, $c = count($chars); $i < $c; ++$i) {
$char = $chars[$i];
if (!$quote_char && $char === ' ' && $ignore_space) {
continue;
}
if ($was_char) {
$type_tokens[++$rtc] = '';
}
if ($quote_char) {
if ($char === $quote_char && $i > 1 && !$escaped) {
$quote_char = null;
$type_tokens[$rtc] .= $char;
$was_char = true;
continue;
}
$was_char = false;
if ($char === '\\'
&& !$escaped
&& $i < $c - 1
&& ($chars[$i + 1] === $quote_char || $chars[$i + 1] === '\\')
) {
$escaped = true;
continue;
}
$escaped = false;
$type_tokens[$rtc] .= $char;
continue;
}
if ($char === '"' || $char === '\'') {
if ($type_tokens[$rtc] === '') {
$type_tokens[$rtc] = $char;
} else {
$type_tokens[++$rtc] = $char;
}
$quote_char = $char;
$was_char = false;
continue;
}
if ($char === '<'
|| $char === '>'
|| $char === '|'
|| $char === '?'
|| $char === ','
|| $char === '{'
|| $char === '}'
|| $char === '['
|| $char === ']'
|| $char === '('
|| $char === ')'
|| $char === ' '
|| $char === '&'
|| $char === '='
) {
if ($type_tokens[$rtc] === '') {
$type_tokens[$rtc] = $char;
} else {
$type_tokens[++$rtc] = $char;
}
$was_char = true;
continue;
}
if ($char === ':') {
if ($i + 1 < $c && $chars[$i + 1] === ':') {
if ($type_tokens[$rtc] === '') {
$type_tokens[$rtc] = '::';
} else {
$type_tokens[++$rtc] = '::';
}
$was_char = true;
$i++;
continue;
}
if ($type_tokens[$rtc] === '') {
$type_tokens[$rtc] = ':';
} else {
$type_tokens[++$rtc] = ':';
}
$was_char = true;
continue;
}
if ($char === '.') {
if ($i + 1 < $c
&& is_numeric($chars[$i + 1])
&& $i > 0
&& is_numeric($chars[$i - 1])
) {
$type_tokens[$rtc] .= $char;
$was_char = false;
continue;
}
if ($i + 2 > $c || $chars[$i + 1] !== '.' || $chars[$i + 2] !== '.') {
throw new TypeParseTreeException('Unexpected token ' . $char);
}
if ($type_tokens[$rtc] === '') {
$type_tokens[$rtc] = '...';
} else {
$type_tokens[++$rtc] = '...';
}
$was_char = true;
$i += 2;
continue;
}
$type_tokens[$rtc] .= $char;
$was_char = false;
}
self::$memoized_tokens[$string_type] = $type_tokens;
return $type_tokens;
}
/**
* @param string $string_type
* @param Aliases $aliases
* @param array<string, mixed>|null $template_type_map
* @param array<string, array<int, string>>|null $type_aliases
*
* @return array<int, string>
*/
public static function fixUpLocalType(
$string_type,
Aliases $aliases,
array $template_type_map = null,
array $type_aliases = null
) {
$type_tokens = self::tokenize($string_type);
for ($i = 0, $l = count($type_tokens); $i < $l; $i++) {
$string_type_token = $type_tokens[$i];
if (in_array(
$string_type_token,
[
'<', '>', '|', '?', ',', '{', '}', ':', '::', '[', ']', '(', ')', '&', '=', '...'
],
true
)) {
continue;
}
if ($string_type_token[0] === '"'
|| $string_type_token[0] === '\''
|| $string_type_token === '0'
|| preg_match('/[1-9]/', $string_type_token[0])
) {
continue;
}
if (isset($type_tokens[$i + 1]) && $type_tokens[$i + 1] === ':') {
continue;
}
if ($i > 0 && $type_tokens[$i - 1] === '::') {
continue;
}
if (strpos($string_type_token, '$')) {
$string_type_token = preg_replace('/(.+)\$.*/', '$1', $string_type_token);
}
$type_tokens[$i] = $string_type_token = self::fixScalarTerms($string_type_token);
if (isset(self::PSALM_RESERVED_WORDS[$string_type_token])) {
continue;
}
if (isset($template_type_map[$string_type_token])) {
continue;
}
if (isset($type_tokens[$i + 1])) {
$next_char = $type_tokens[$i + 1];
if ($next_char === ':') {
continue;
}
if ($next_char === '?' && isset($type_tokens[$i + 2]) && $type_tokens[$i + 2] === ':') {
continue;
}
}
if ($string_type_token[0] === '$') {
continue;
}
if (isset($type_aliases[$string_type_token])) {
$replacement_tokens = $type_aliases[$string_type_token];
array_unshift($replacement_tokens, '(');
array_push($replacement_tokens, ')');
$diff = count($replacement_tokens) - 1;
array_splice($type_tokens, $i, 1, $replacement_tokens);
$i += $diff;
$l += $diff;
} else {
$type_tokens[$i] = self::getFQCLNFromString(
$string_type_token,
$aliases
);
}
}
return $type_tokens;
}
/**
* @param string $class
* @param Aliases $aliases
*
* @return string
*/
public static function getFQCLNFromString($class, Aliases $aliases)
{
if ($class === '') {
throw new \InvalidArgumentException('$class cannot be empty');
}
if ($class[0] === '\\') {
return substr($class, 1);
}
$imported_namespaces = $aliases->uses;
if (strpos($class, '\\') !== false) {
$class_parts = explode('\\', $class);
$first_namespace = array_shift($class_parts);
if (isset($imported_namespaces[strtolower($first_namespace)])) {
return $imported_namespaces[strtolower($first_namespace)] . '\\' . implode('\\', $class_parts);
}
} elseif (isset($imported_namespaces[strtolower($class)])) {