Skip to content

Commit c7c0493

Browse files
committed
Fix #4093 - prevent redundant condition in presence of positive-int
1 parent 2160396 commit c7c0493

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

src/Psalm/Internal/Type/NegatedAssertionReconciler.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -315,8 +315,12 @@ private static function handleLiteralNegatedEquality(
315315

316316
if ($scalar_type === 'int') {
317317
if ($existing_var_type->hasInt()) {
318+
$scalar_value = substr($assertion, $bracket_pos + 1, -1);
319+
318320
if ($existing_int_types = $existing_var_type->getLiteralInts()) {
319-
$did_match_literal_type = true;
321+
if (!$existing_var_type->hasPositiveInt()) {
322+
$did_match_literal_type = true;
323+
}
320324

321325
if (isset($existing_int_types[$assertion])) {
322326
$existing_var_type->removeType($assertion);

src/Psalm/Type/Union.php

+8
Original file line numberDiff line numberDiff line change
@@ -857,6 +857,14 @@ public function hasInt()
857857
return isset($this->types['int']) || isset($this->types['array-key']) || $this->literal_int_types;
858858
}
859859

860+
/**
861+
* @return bool
862+
*/
863+
public function hasPositiveInt()
864+
{
865+
return isset($this->types['int']) && $this->types['int'] instanceof Type\Atomic\TPositiveInt;
866+
}
867+
860868
/**
861869
* @return bool
862870
*/

tests/TypeReconciliation/ValueTest.php

+10
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,16 @@ public static function getValues() {
756756
757757
echo strlen($data);'
758758
],
759+
'negateValueInUnion' => [
760+
'<?php
761+
function f(): int {
762+
$ret = 0;
763+
for ($i = 20; $i >= 0; $i--) {
764+
$ret = ($ret === 10) ? 1 : $ret + 1;
765+
}
766+
return $ret;
767+
}'
768+
],
759769
];
760770
}
761771

0 commit comments

Comments
 (0)