Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
orklah committed Nov 24, 2022
1 parent 8f39de9 commit ea4b099
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 8 deletions.
10 changes: 8 additions & 2 deletions src/Psalm/Internal/Type/SimpleAssertionReconciler.php
Expand Up @@ -1705,9 +1705,12 @@ private static function reconcileIsGreaterThan(

$did_remove_type = false;

if ($existing_var_type->hasType('null') && $assertion->doesFilterNull()) {
if ($assertion->doesFilterNullOrFalse() &&
($existing_var_type->hasType('null') || $existing_var_type->hasType('false'))
) {
$did_remove_type = true;
$existing_var_type->removeType('null');
$existing_var_type->removeType('false');
}

foreach ($existing_var_type->getAtomicTypes() as $atomic_type) {
Expand Down Expand Up @@ -1815,9 +1818,12 @@ private static function reconcileIsLessThan(

$did_remove_type = false;

if ($existing_var_type->hasType('null') && $assertion->doesFilterNull()) {
if ($assertion->doesFilterNullOrFalse() &&
($existing_var_type->hasType('null') || $existing_var_type->hasType('false'))
) {
$did_remove_type = true;
$existing_var_type->removeType('null');
$existing_var_type->removeType('false');
}

foreach ($existing_var_type->getAtomicTypes() as $atomic_type) {
Expand Down
10 changes: 8 additions & 2 deletions src/Psalm/Internal/Type/SimpleNegatedAssertionReconciler.php
Expand Up @@ -1719,9 +1719,12 @@ private static function reconcileIsLessThanOrEqualTo(

$did_remove_type = false;

if ($existing_var_type->hasType('null') && $assertion->doesFilterNull()) {
if ($assertion->doesFilterNullOrFalse() &&
($existing_var_type->hasType('null') || $existing_var_type->hasType('false'))
) {
$did_remove_type = true;
$existing_var_type->removeType('null');
$existing_var_type->removeType('false');
}

foreach ($existing_var_type->getAtomicTypes() as $atomic_type) {
Expand Down Expand Up @@ -1827,9 +1830,12 @@ private static function reconcileIsGreaterThanOrEqualTo(

$did_remove_type = false;

if ($existing_var_type->hasType('null') && $assertion->doesFilterNull()) {
if ($assertion->doesFilterNullOrFalse() &&
($existing_var_type->hasType('null') || $existing_var_type->hasType('false'))
) {
$did_remove_type = true;
$existing_var_type->removeType('null');
$existing_var_type->removeType('false');
}

foreach ($existing_var_type->getAtomicTypes() as $atomic_type) {
Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Storage/Assertion/IsGreaterThan.php
Expand Up @@ -31,7 +31,7 @@ public function isNegationOf(Assertion $assertion): bool
return $assertion instanceof IsLessThanOrEqualTo && $this->value === $assertion->value;
}

public function doesFilterNull(): bool
public function doesFilterNullOrFalse(): bool
{
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Storage/Assertion/IsGreaterThanOrEqualTo.php
Expand Up @@ -36,7 +36,7 @@ public function isNegationOf(Assertion $assertion): bool
return $assertion instanceof IsLessThan && $this->value === $assertion->value;
}

public function doesFilterNull(): bool
public function doesFilterNullOrFalse(): bool
{
return $this->value !== 0;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Storage/Assertion/IsLessThan.php
Expand Up @@ -31,7 +31,7 @@ public function isNegationOf(Assertion $assertion): bool
return $assertion instanceof IsGreaterThanOrEqualTo && $this->value === $assertion->value;
}

public function doesFilterNull(): bool
public function doesFilterNullOrFalse(): bool
{
return $this->value === 0;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Storage/Assertion/IsLessThanOrEqualTo.php
Expand Up @@ -36,7 +36,7 @@ public function isNegationOf(Assertion $assertion): bool
return $assertion instanceof IsGreaterThan && $this->value === $assertion->value;
}

public function doesFilterNull(): bool
public function doesFilterNullOrFalse(): bool
{
return false;
}
Expand Down
43 changes: 43 additions & 0 deletions tests/TypeReconciliation/ConditionalTest.php
Expand Up @@ -2756,6 +2756,49 @@ function getIntOrNull(): ?int{return null;}
}
',
],
'falseErasureWithSmallerAndGreater' => [
'code' => '<?php
/** @return int|false */
function getIntOrFalse(): int|bool {return false;}
$a = getIntOrFalse();
if ($a < 0) {
echo $a + 3;
}
if ($a <= 0) {
/** @psalm-suppress PossiblyFalseOperand */
echo $a + 3;
}
if ($a > 0) {
echo $a + 3;
}
if ($a >= 0) {
/** @psalm-suppress PossiblyFalseOperand */
echo $a + 3;
}
if (0 < $a) {
echo $a + 3;
}
if (0 <= $a) {
/** @psalm-suppress PossiblyFalseOperand */
echo $a + 3;
}
if (0 > $a) {
echo $a + 3;
}
if (0 >= $a) {
/** @psalm-suppress PossiblyFalseOperand */
echo $a + 3;
}
',
],
'SimpleXMLElementNotAlwaysTruthy' => [
'code' => '<?php
$lilstring = "";
Expand Down

0 comments on commit ea4b099

Please sign in to comment.