From 65efab3a8de2a99732c377971c958eebb09e98dc Mon Sep 17 00:00:00 2001 From: orklah Date: Sun, 9 Jan 2022 15:48:58 +0100 Subject: [PATCH 1/2] keep class-strings through array_merge --- .../ArrayMergeReturnTypeProvider.php | 9 +++++++++ tests/ArrayFunctionCallTest.php | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/Psalm/Internal/Provider/ReturnTypeProvider/ArrayMergeReturnTypeProvider.php b/src/Psalm/Internal/Provider/ReturnTypeProvider/ArrayMergeReturnTypeProvider.php index 578560b635c..63059484798 100644 --- a/src/Psalm/Internal/Provider/ReturnTypeProvider/ArrayMergeReturnTypeProvider.php +++ b/src/Psalm/Internal/Provider/ReturnTypeProvider/ArrayMergeReturnTypeProvider.php @@ -50,6 +50,7 @@ public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $ev $codebase = $statements_source->getCodebase(); $generic_properties = []; + $class_strings = []; $all_keyed_arrays = true; $all_int_offsets = true; $all_nonempty_lists = true; @@ -107,6 +108,10 @@ public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $ev continue; } + if (isset($unpacked_type_part->class_strings[$key])) { + $class_strings[$key] = true; + } + if (!isset($generic_properties[$key]) || !$type->possibly_undefined) { $generic_properties[$key] = $type; } else { @@ -218,6 +223,10 @@ public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $ev ) { $objectlike = new TKeyedArray($generic_properties); + if ($class_strings !== []) { + $objectlike->class_strings = $class_strings; + } + if ($all_nonempty_lists || $all_int_offsets) { $objectlike->is_list = true; } diff --git a/tests/ArrayFunctionCallTest.php b/tests/ArrayFunctionCallTest.php index f504a6f0def..01954168671 100644 --- a/tests/ArrayFunctionCallTest.php +++ b/tests/ArrayFunctionCallTest.php @@ -2121,6 +2121,24 @@ function foo(): array return $a; }', ], + 'arrayUnshiftOnEmptyArrayMeansNonEmptyList' => [ + ' */ + private array $a; + + public function __construct() { + $this->a = []; + } + + public function handle(): void { + $b = [A::class => "d"]; + $this->a = array_merge($this->a, $b); + } + } + ', + ], ]; } From 52ae26f143fda0ebab57a00b7703a75496837a76 Mon Sep 17 00:00:00 2001 From: orklah Date: Sun, 9 Jan 2022 15:53:43 +0100 Subject: [PATCH 2/2] fix test --- tests/ArrayFunctionCallTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ArrayFunctionCallTest.php b/tests/ArrayFunctionCallTest.php index 01954168671..b06a524a54b 100644 --- a/tests/ArrayFunctionCallTest.php +++ b/tests/ArrayFunctionCallTest.php @@ -2121,7 +2121,7 @@ function foo(): array return $a; }', ], - 'arrayUnshiftOnEmptyArrayMeansNonEmptyList' => [ + 'keepClassStringInOffsetThroughArrayMerge' => [ '