chore: fix counting empty array

Signed-off-by: azjezz <azjezz@protonmail.com>
This commit is contained in:
azjezz 2022-02-27 21:03:07 +01:00
parent 0fe10f0af9
commit 19d14701e5
No known key found for this signature in database
GPG Key ID: B00E0A46B3F1C157

View File

@ -54,24 +54,28 @@ final class FunctionReturnTypeProvider implements FunctionReturnTypeProviderInte
return Type::getInt(false, $count); 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) { if ($array_argument_type instanceof Type\Atomic\TKeyedArray) {
// Psalm allows extra properties in keyed arrays, so we can't return a literal integer // Psalm allows extra properties in keyed arrays, so we can't return a literal integer
// for this. if (($size = count($array_argument_type->properties)) >= 1) {
// return new Type\Union([new Type\Atomic\TIntRange($size, null)]);
// 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)]);
} }
return Type::getInt(); return Type::getInt();
} }
if ($array_argument_type instanceof Type\Atomic\TArray) { 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); 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(); return Type::getInt();