Skip to content

Commit

Permalink
Improve min/max return type cc @orklah
Browse files Browse the repository at this point in the history
Type::combineUnionTypes preserves metadata for union types, and is more accurate
  • Loading branch information
muglug committed Mar 14, 2021
1 parent b549989 commit f1a8407
Showing 1 changed file with 10 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,23 @@ public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $ev
return $array_type->value;
}

$atomics = [];
$return_type = null;

foreach ($call_args as $arg) {
if ($array_arg_type = $nodeTypeProvider->getType($arg->value)) {
foreach ($array_arg_type->getAtomicTypes() as $atomicType) {
$atomics[] = $atomicType;
if (!$return_type) {
$return_type = $array_arg_type;
} else {
$return_type = \Psalm\Type::combineUnionTypes(
$return_type,
$array_arg_type
);
}
} else {
return Type::getMixed();
}
}

if ($atomics === []) {
return Type::getMixed();
}

return new Type\Union($atomics);
return $return_type;
}
}

1 comment on commit f1a8407

@orklah
Copy link
Collaborator

@orklah orklah commented on f1a8407 Mar 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, I see, Thanks!

Please sign in to comment.