diff --git a/src/Psalm/Internal/Provider/ReturnTypeProvider/RoundReturnTypeProvider.php b/src/Psalm/Internal/Provider/ReturnTypeProvider/RoundReturnTypeProvider.php new file mode 100644 index 00000000000..ff3790fbb97 --- /dev/null +++ b/src/Psalm/Internal/Provider/ReturnTypeProvider/RoundReturnTypeProvider.php @@ -0,0 +1,57 @@ + + */ + public static function getFunctionIds(): array + { + return ['round']; + } + + public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $event): ?Type\Union + { + $call_args = $event->getCallArgs(); + if (count($call_args) === 0) { + return null; + } + + $statements_source = $event->getStatementsSource(); + $nodeTypeProvider = $statements_source->getNodeTypeProvider(); + + $num_arg = $nodeTypeProvider->getType($call_args[0]->value); + + if (count($call_args) > 1) { + $precision_val = $call_args[1]->value; + } else { + $precision_val = 0; + } + + if ($num_arg !== null && $num_arg->isSingle()) { + $num_type = array_values($num_arg->getAtomicTypes())[0]; + if ($num_type instanceof Type\Atomic\TFloat) { + if ($precision_val > 0) { + return new Type\Union([new Type\Atomic\TFloat()]); + } + + return new Type\Union([new Type\Atomic\TInt()]); + } + + if ($num_type instanceof Type\Atomic\TInt) { + return new Type\Union([new Type\Atomic\TInt()]); + } + } + + return new Type\Union([new Type\Atomic\TInt(), new Type\Atomic\TFloat()]); + } +} diff --git a/tests/ReturnTypeProvider/RoundTest.php b/tests/ReturnTypeProvider/RoundTest.php new file mode 100644 index 00000000000..74b6eed64a9 --- /dev/null +++ b/tests/ReturnTypeProvider/RoundTest.php @@ -0,0 +1,45 @@ + [ + '