Skip to content

Commit

Permalink
Fix #4093 - prevent redundant condition in presence of positive-int
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Sep 1, 2020
1 parent 2160396 commit c7c0493
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Psalm/Internal/Type/NegatedAssertionReconciler.php
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,12 @@ private static function handleLiteralNegatedEquality(

if ($scalar_type === 'int') {
if ($existing_var_type->hasInt()) {
$scalar_value = substr($assertion, $bracket_pos + 1, -1);

if ($existing_int_types = $existing_var_type->getLiteralInts()) {
$did_match_literal_type = true;
if (!$existing_var_type->hasPositiveInt()) {
$did_match_literal_type = true;
}

if (isset($existing_int_types[$assertion])) {
$existing_var_type->removeType($assertion);
Expand Down
8 changes: 8 additions & 0 deletions src/Psalm/Type/Union.php
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,14 @@ public function hasInt()
return isset($this->types['int']) || isset($this->types['array-key']) || $this->literal_int_types;
}

/**
* @return bool
*/
public function hasPositiveInt()
{
return isset($this->types['int']) && $this->types['int'] instanceof Type\Atomic\TPositiveInt;
}

/**
* @return bool
*/
Expand Down
10 changes: 10 additions & 0 deletions tests/TypeReconciliation/ValueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,16 @@ public static function getValues() {
echo strlen($data);'
],
'negateValueInUnion' => [
'<?php
function f(): int {
$ret = 0;
for ($i = 20; $i >= 0; $i--) {
$ret = ($ret === 10) ? 1 : $ret + 1;
}
return $ret;
}'
],
];
}

Expand Down

0 comments on commit c7c0493

Please sign in to comment.