From a79122256c2bccd681bb3c9453471fb95d6055b3 Mon Sep 17 00:00:00 2001 From: Brown Date: Tue, 7 Apr 2020 19:59:20 -0400 Subject: [PATCH] Use conditional return type for range --- .../Provider/FunctionReturnTypeProvider.php | 1 - .../RangeReturnTypeProvider.php | 82 ------------------- .../Stubs/CoreGenericFunctions.phpstub | 26 ++++++ 3 files changed, 26 insertions(+), 83 deletions(-) delete mode 100644 src/Psalm/Internal/Provider/ReturnTypeProvider/RangeReturnTypeProvider.php diff --git a/src/Psalm/Internal/Provider/FunctionReturnTypeProvider.php b/src/Psalm/Internal/Provider/FunctionReturnTypeProvider.php index f63f6a821..b11bcef9d 100644 --- a/src/Psalm/Internal/Provider/FunctionReturnTypeProvider.php +++ b/src/Psalm/Internal/Provider/FunctionReturnTypeProvider.php @@ -49,7 +49,6 @@ class FunctionReturnTypeProvider $this->registerClass(ReturnTypeProvider\FilterVarReturnTypeProvider::class); $this->registerClass(ReturnTypeProvider\IteratorToArrayReturnTypeProvider::class); $this->registerClass(ReturnTypeProvider\ParseUrlReturnTypeProvider::class); - $this->registerClass(ReturnTypeProvider\RangeReturnTypeProvider::class); $this->registerClass(ReturnTypeProvider\StrReplaceReturnTypeProvider::class); $this->registerClass(ReturnTypeProvider\VersionCompareReturnTypeProvider::class); $this->registerClass(ReturnTypeProvider\MktimeReturnTypeProvider::class); diff --git a/src/Psalm/Internal/Provider/ReturnTypeProvider/RangeReturnTypeProvider.php b/src/Psalm/Internal/Provider/ReturnTypeProvider/RangeReturnTypeProvider.php deleted file mode 100644 index 0dfa3a083..000000000 --- a/src/Psalm/Internal/Provider/ReturnTypeProvider/RangeReturnTypeProvider.php +++ /dev/null @@ -1,82 +0,0 @@ - $call_args - */ - public static function getFunctionReturnType( - StatementsSource $statements_source, - string $function_id, - array $call_args, - Context $context, - CodeLocation $code_location - ) : Type\Union { - if (!$statements_source instanceof \Psalm\Internal\Analyzer\StatementsAnalyzer) { - return Type::getMixed(); - } - - $all_ints = true; - $all_strings = true; - $all_floats = true; - $all_numbers = true; - - foreach ($call_args as $call_arg) { - $is_int = false; - $is_float = false; - $is_string = false; - - if ($call_arg_type = $statements_source->node_data->getType($call_arg->value)) { - if ($call_arg_type->isInt()) { - $is_int = true; - } elseif ($call_arg_type->isFloat()) { - $is_float = true; - } elseif ($call_arg_type->isString()) { - $is_string = true; - } - } - - $all_ints = $all_ints && $is_int; - - $all_floats = $all_floats && $is_float; - - $all_strings = $all_strings && $is_string; - - $all_numbers = $all_numbers && ($is_int || $is_float); - } - - if ($all_ints) { - return new Type\Union([new Type\Atomic\TList(new Type\Union([new Type\Atomic\TInt]))]); - } - - if ($all_strings) { - return new Type\Union([new Type\Atomic\TList(new Type\Union([new Type\Atomic\TString]))]); - } - - if ($all_floats) { - return new Type\Union([new Type\Atomic\TList(new Type\Union([new Type\Atomic\TFloat]))]); - } - - if ($all_numbers) { - return new Type\Union([new Type\Atomic\TList( - new Type\Union([new Type\Atomic\TInt, new Type\Atomic\TFloat]) - )]); - } - - return new Type\Union([new Type\Atomic\TList( - new Type\Union([new Type\Atomic\TInt, new Type\Atomic\TFloat, new Type\Atomic\TString]) - )]); - } -} diff --git a/src/Psalm/Internal/Stubs/CoreGenericFunctions.phpstub b/src/Psalm/Internal/Stubs/CoreGenericFunctions.phpstub index d961f9ece..31255ed07 100644 --- a/src/Psalm/Internal/Stubs/CoreGenericFunctions.phpstub +++ b/src/Psalm/Internal/Stubs/CoreGenericFunctions.phpstub @@ -369,3 +369,29 @@ function gettimeofday(bool $return_float = false) {} * @return ($number is int ? int : ($number is float ? float : int|float)) */ function abs($number) {} + +/** + * @template T as string|int|float + * @template TStep as int|float + * @param T $start + * @param T $end + * @param TStep $step + * @return ( + * T is int + * ? (TStep is int ? list : list) + * : ( + * T is float + * ? list + * : ( + * T is string + * ? list + * : ( + * T is int|float + * ? list + * : list + * ) + * ) + * ) + * ) + */ +function range($start, $end, $step = 1) {}