From 0df867cdff3f3abf170ff561ba203a82071fbbcb Mon Sep 17 00:00:00 2001 From: orklah Date: Fri, 27 Nov 2020 23:05:54 +0100 Subject: [PATCH] fix array_column with possibly_undefined keys (#4719) --- .../ArrayColumnReturnTypeProvider.php | 7 +++++-- tests/ArrayFunctionCallTest.php | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/Psalm/Internal/Provider/ReturnTypeProvider/ArrayColumnReturnTypeProvider.php b/src/Psalm/Internal/Provider/ReturnTypeProvider/ArrayColumnReturnTypeProvider.php index 14489c725..1fa1a82b3 100644 --- a/src/Psalm/Internal/Provider/ReturnTypeProvider/ArrayColumnReturnTypeProvider.php +++ b/src/Psalm/Internal/Provider/ReturnTypeProvider/ArrayColumnReturnTypeProvider.php @@ -92,10 +92,13 @@ class ArrayColumnReturnTypeProvider implements \Psalm\Plugin\Hook\FunctionReturn // calculate results if ($row_shape instanceof Type\Atomic\TKeyedArray) { if ((null !== $value_column_name) && isset($row_shape->properties[$value_column_name])) { - if ($input_array_not_empty) { + $result_element_type = $row_shape->properties[$value_column_name]; + // When the selected key is possibly_undefined, the resulting array can be empty + if ($input_array_not_empty && $result_element_type->possibly_undefined !== true) { $have_at_least_one_res = true; } - $result_element_type = $row_shape->properties[$value_column_name]; + //array_column skips undefined elements so resulting type is necesseraly defined + $result_element_type->possibly_undefined = false; } else { $result_element_type = Type::getMixed(); } diff --git a/tests/ArrayFunctionCallTest.php b/tests/ArrayFunctionCallTest.php index 92b174f3e..75d5b2b86 100644 --- a/tests/ArrayFunctionCallTest.php +++ b/tests/ArrayFunctionCallTest.php @@ -1151,6 +1151,8 @@ class ArrayFunctionCallTest extends TestCase function makeShapeArray(): array { return []; } /** @return array */ function makeUnionArray(): array { return []; } + /** @return array */ + function makeKeyedArray(): array { return []; } $a = array_column([[1], [2], [3]], 0); $b = array_column([["a" => 1], ["a" => 2], ["a" => 3]], "a"); $c = array_column([["k" => "a", "v" => 1], ["k" => "b", "v" => 2]], "v", "k"); @@ -1162,6 +1164,10 @@ class ArrayFunctionCallTest extends TestCase $i = array_column(makeShapeArray(), 0); $j = array_column(makeUnionArray(), 0); $k = array_column([[0 => "test"]], 0); + $l = array_column(makeKeyedArray(), "y"); + $m_prepare = makeKeyedArray(); + assert($m_prepare !== []); + $m = array_column($m_prepare, "y"); ', 'assertions' => [ '$a' => 'non-empty-list', @@ -1175,6 +1181,8 @@ class ArrayFunctionCallTest extends TestCase '$i' => 'list', '$j' => 'list', '$k' => 'non-empty-list', + '$l' => 'list', + '$m' => 'list', ], ], 'splatArrayIntersect' => [ @@ -1870,6 +1878,15 @@ class ArrayFunctionCallTest extends TestCase } }' ], + 'arrayColumnwithKeyedArrayWithoutRedundantUnion' => [ + ' $foos + */ + function foo(array $foos): void { + array_multisort($formLayoutFields, SORT_ASC, array_column($foos, "y")); + }' + ], ]; }