File tree Expand file tree Collapse file tree 5 files changed +38
-3
lines changed
Analyzer/Statements/Expression/Assignment Expand file tree Collapse file tree 5 files changed +38
-3
lines changed Original file line number Diff line number Diff line change @@ -351,7 +351,9 @@ private static function updateTypeWithKeyValues(
351
351
if (!$ has_matching_objectlike_property && !$ has_matching_string ) {
352
352
$ properties = [];
353
353
$ classStrings = [];
354
- $ current_type = $ current_type ->setPossiblyUndefined (count ($ key_values ) > 1 );
354
+ $ current_type = $ current_type ->setPossiblyUndefined (
355
+ $ current_type ->possibly_undefined || count ($ key_values ) > 1 ,
356
+ );
355
357
foreach ($ key_values as $ key_value ) {
356
358
$ properties [$ key_value ->value ] = $ current_type ;
357
359
if ($ key_value instanceof TLiteralClassString) {
Original file line number Diff line number Diff line change @@ -55,6 +55,9 @@ final class TypeCombination
55
55
/** @var array<string|int, Union> */
56
56
public array $ objectlike_entries = [];
57
57
58
+ /** @var array<string, true> */
59
+ public array $ objectlike_class_string_keys = [];
60
+
58
61
public bool $ objectlike_sealed = true ;
59
62
60
63
public ?Union $ objectlike_key_type = null ;
Original file line number Diff line number Diff line change @@ -667,6 +667,7 @@ private static function scrapeTypeProperties(
667
667
668
668
$ has_defined_keys = false ;
669
669
670
+ $ class_strings = $ type ->class_strings ?? [];
670
671
foreach ($ type ->properties as $ candidate_property_name => $ candidate_property_type ) {
671
672
$ value_type = $ combination ->objectlike_entries [$ candidate_property_name ] ?? null ;
672
673
@@ -705,6 +706,15 @@ private static function scrapeTypeProperties(
705
706
);
706
707
}
707
708
709
+ if (isset ($ combination ->objectlike_class_string_keys [$ candidate_property_name ])) {
710
+ $ combination ->objectlike_class_string_keys [$ candidate_property_name ] =
711
+ $ combination ->objectlike_class_string_keys [$ candidate_property_name ]
712
+ && ($ class_strings [$ candidate_property_name ] ?? false );
713
+ } else {
714
+ $ combination ->objectlike_class_string_keys [$ candidate_property_name ] =
715
+ ($ class_strings [$ candidate_property_name ] ?? false );
716
+ }
717
+
708
718
unset($ missing_entries [$ candidate_property_name ]);
709
719
}
710
720
@@ -1421,7 +1431,7 @@ private static function handleKeyedArrayEntries(
1421
1431
} else {
1422
1432
$ objectlike = new TKeyedArray (
1423
1433
$ combination ->objectlike_entries ,
1424
- null ,
1434
+ $ combination -> objectlike_class_string_keys ,
1425
1435
$ sealed || $ fallback_key_type === null || $ fallback_value_type === null
1426
1436
? null
1427
1437
: [$ fallback_key_type , $ fallback_value_type ],
Original file line number Diff line number Diff line change @@ -1132,7 +1132,9 @@ private static function adjustTKeyedArrayType(
1132
1132
1133
1133
$ base_key = implode ($ key_parts );
1134
1134
1135
- $ result_type = $ result_type ->setPossiblyUndefined (count ($ array_key_offsets ) > 1 );
1135
+ $ result_type = $ result_type ->setPossiblyUndefined (
1136
+ $ result_type ->possibly_undefined || count ($ array_key_offsets ) > 1 ,
1137
+ );
1136
1138
1137
1139
foreach ($ array_key_offsets as $ array_key_offset ) {
1138
1140
if (isset ($ existing_types [$ base_key ]) && $ array_key_offset !== false ) {
Original file line number Diff line number Diff line change @@ -55,6 +55,24 @@ public function providerValidCodeParse(): iterable
55
55
'$resultOpt=== ' => 'array{a?: true, b?: true} ' ,
56
56
],
57
57
],
58
+ 'assignUnionOfLiteralsClassKeys ' => [
59
+ 'code ' => '<?php
60
+ class a {}
61
+ class b {}
62
+
63
+ $result = [];
64
+
65
+ foreach ([a::class, b::class] as $k) {
66
+ $result[$k] = true;
67
+ }
68
+
69
+ foreach ($result as $k => $v) {
70
+ $vv = new $k;
71
+ } ' ,
72
+ 'assertions ' => [
73
+ '$result=== ' => 'array{a::class: true, b::class: true} ' ,
74
+ ],
75
+ ],
58
76
'genericArrayCreationWithSingleIntValue ' => [
59
77
'code ' => '<?php
60
78
$out = [];
You can’t perform that action at this time.
0 commit comments