From 19d14701e55b3c77917966c10d214726a9e27ac3 Mon Sep 17 00:00:00 2001 From: azjezz Date: Sun, 27 Feb 2022 21:03:07 +0100 Subject: [PATCH] chore: fix counting empty array Signed-off-by: azjezz --- .../Iter/Count/FunctionReturnTypeProvider.php | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/EventHandler/Iter/Count/FunctionReturnTypeProvider.php b/src/EventHandler/Iter/Count/FunctionReturnTypeProvider.php index 9ff45a4..e035364 100644 --- a/src/EventHandler/Iter/Count/FunctionReturnTypeProvider.php +++ b/src/EventHandler/Iter/Count/FunctionReturnTypeProvider.php @@ -54,24 +54,28 @@ final class FunctionReturnTypeProvider implements FunctionReturnTypeProviderInte return Type::getInt(false, $count); } - // array{foo: bar} -> literal-int(1) + // array{foo: bar} -> int<1, max> + // array{foo, bar, baz} -> int<3, max> if ($array_argument_type instanceof Type\Atomic\TKeyedArray) { // Psalm allows extra properties in keyed arrays, so we can't return a literal integer - // for this. - // - // return Type::getInt(false, count($array_argument_type->properties)); - - if (count($array_argument_type->properties) >= 1) { - return new Type\Union([new Type\Atomic\TIntRange(1, null)]); + if (($size = count($array_argument_type->properties)) >= 1) { + return new Type\Union([new Type\Atomic\TIntRange($size, null)]); } return Type::getInt(); } if ($array_argument_type instanceof Type\Atomic\TArray) { - if ($array_argument_type->type_params[0]->isNever() && $array_argument_type->type_params[1]->isNever()) { + if (method_exists($array_argument_type, 'isEmptyArray') && $array_argument_type->isEmptyArray()) { return Type::getInt(false, 0); } + + if (method_exists($array_argument_type->type_params[0], 'isEmpty')) { + if ($array_argument_type->type_params[0]->isEmpty() && $array_argument_type->type_params[1]->isEmpty()) { + return Type::getInt(false, 0); + } + } + } return Type::getInt();