From dc1132a9155cf8879072ebad0bb7053674e6f0f7 Mon Sep 17 00:00:00 2001 From: orklah Date: Wed, 12 Jan 2022 01:00:30 +0100 Subject: [PATCH 1/3] Better Handling of TPositiveInt combined with TIntRange --- src/Psalm/Internal/Type/TypeCombiner.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/Psalm/Internal/Type/TypeCombiner.php b/src/Psalm/Internal/Type/TypeCombiner.php index 99fbeffff30..9e7579c3d3e 100644 --- a/src/Psalm/Internal/Type/TypeCombiner.php +++ b/src/Psalm/Internal/Type/TypeCombiner.php @@ -1181,6 +1181,13 @@ function ($int): bool { } } elseif (!isset($combination->value_types['int'])) { $combination->value_types['int'] = $type; + } elseif ($combination->value_types['int'] instanceof TIntRange) { + //if we already had a range, we ensure the min is no higher than 1 + $combination->value_types['int']->min_bound = TIntRange::getNewLowestBound( + $combination->value_types['int']->min_bound, + 1 + ); + $combination->value_types['int']->max_bound = null; } elseif (get_class($combination->value_types['int']) !== get_class($type)) { $combination->value_types['int'] = new TInt(); } From 4344c8ec3f2b58729ae3b416b51ceda222645166 Mon Sep 17 00:00:00 2001 From: orklah Date: Sat, 15 Jan 2022 21:29:08 +0100 Subject: [PATCH 2/3] add test --- tests/IntRangeTest.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/IntRangeTest.php b/tests/IntRangeTest.php index 60d799af2b9..7939c15a0a2 100644 --- a/tests/IntRangeTest.php +++ b/tests/IntRangeTest.php @@ -684,6 +684,19 @@ function doAnalysis(): void '$length===' => '1', ], ], + 'PositiveIntCombinedWithIntRange' => [ + 'code' => ', int<0, max>> */ + $_arr = []; + + $_arr[1] = $int; + $_arr[$int] = 2;', + 'assertions' => [ + '$_arr===' => 'non-empty-array, int<0, max>>', + ], + ], ]; } From 0beb8e7a4ec3804a99b9026b039d99dd4defcfee Mon Sep 17 00:00:00 2001 From: orklah Date: Sat, 15 Jan 2022 21:39:29 +0100 Subject: [PATCH 3/3] fix test --- tests/IntRangeTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/IntRangeTest.php b/tests/IntRangeTest.php index 7939c15a0a2..33cb1339b35 100644 --- a/tests/IntRangeTest.php +++ b/tests/IntRangeTest.php @@ -685,7 +685,7 @@ function doAnalysis(): void ], ], 'PositiveIntCombinedWithIntRange' => [ - 'code' => ', int<0, max>> */