Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #8745 #8753

Merged
merged 2 commits into from Nov 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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() {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