From 68f6ba873e7f6fdae9786af69db0c9f9b7649a43 Mon Sep 17 00:00:00 2001 From: Steven Dickinson Date: Tue, 11 Oct 2022 14:11:58 +0100 Subject: [PATCH] Fix MinMaxReturnTypeProvider when handling TDependentListKeys --- .../MinMaxReturnTypeProvider.php | 10 ++-- .../MinMaxReturnTypeProviderTest.php | 53 +++++++++++++++++++ 2 files changed, 58 insertions(+), 5 deletions(-) create mode 100644 tests/ReturnTypeProvider/MinMaxReturnTypeProviderTest.php diff --git a/src/Psalm/Internal/Provider/ReturnTypeProvider/MinMaxReturnTypeProvider.php b/src/Psalm/Internal/Provider/ReturnTypeProvider/MinMaxReturnTypeProvider.php index 1a24e22b3..e23ff30b1 100644 --- a/src/Psalm/Internal/Provider/ReturnTypeProvider/MinMaxReturnTypeProvider.php +++ b/src/Psalm/Internal/Provider/ReturnTypeProvider/MinMaxReturnTypeProvider.php @@ -8,17 +8,16 @@ use Psalm\Internal\Type\ArrayType; use Psalm\Plugin\EventHandler\Event\FunctionReturnTypeProviderEvent; use Psalm\Plugin\EventHandler\FunctionReturnTypeProviderInterface; use Psalm\Type; +use Psalm\Type\Atomic\TDependentListKey; use Psalm\Type\Atomic\TInt; use Psalm\Type\Atomic\TIntRange; use Psalm\Type\Atomic\TLiteralInt; use Psalm\Type\Atomic\TPositiveInt; use Psalm\Type\Union; -use UnexpectedValueException; use function array_filter; use function assert; use function count; -use function get_class; use function in_array; use function max; use function min; @@ -72,11 +71,12 @@ class MinMaxReturnTypeProvider implements FunctionReturnTypeProviderInterface } elseif ($atomic_type instanceof TPositiveInt) { $min_bounds[] = 1; $max_bounds[] = null; - } elseif (get_class($atomic_type) === TInt::class) { + } elseif ($atomic_type instanceof TDependentListKey) { + $min_bounds[] = 0; + $max_bounds[] = null; + } else {//already guarded by the `instanceof TInt` check above $min_bounds[] = null; $max_bounds[] = null; - } else { - throw new UnexpectedValueException('Unexpected type'); } } } else { diff --git a/tests/ReturnTypeProvider/MinMaxReturnTypeProviderTest.php b/tests/ReturnTypeProvider/MinMaxReturnTypeProviderTest.php new file mode 100644 index 000000000..1bbdff97d --- /dev/null +++ b/tests/ReturnTypeProvider/MinMaxReturnTypeProviderTest.php @@ -0,0 +1,53 @@ + [ + ' 'int', + '$max' => 'int', + ], + ]; + yield 'nonInt' => [ + ' 'string', + '$max' => 'string', + ], + ]; + yield 'maxIntRange' => [ + ' $v) { + if ($v === "") $h0 = $i; + if ($v === "") $h1 = $i; + } + if ($h0 === null || $h1 === null) throw new \Exception(); + + $min = min($h0, $h1); + $max = max($h0, $h1); + ', + [ + '$min' => 'int<0, max>', + '$max' => 'int<0, max>', + ], + ]; + } +}