Skip to content

Commit

Permalink
chore: backport counting fix
Browse files Browse the repository at this point in the history
Signed-off-by: azjezz <azjezz@protonmail.com>
  • Loading branch information
azjezz committed Feb 27, 2022
1 parent 294e1cb commit 134176c
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions src/EventHandler/Iter/Count/FunctionReturnTypeProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,24 +54,29 @@ public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $ev
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()) {
if ($array_argument_type->type_params[1]->isEmpty()) {
return Type::getInt(false, 0);
}
}
}
}

return Type::getInt();
Expand Down

0 comments on commit 134176c

Please sign in to comment.