Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -987,19 +987,23 @@ private function resolveType(string $exprString, Expr $node): Type
}

if ($node instanceof Expr\BinaryOp\Smaller) {
return $this->getType($node->left)->isSmallerThan($this->getType($node->right), $this->phpVersion)->toBooleanType();
return $this->resolveComplementaryComparison($node, new Expr\BinaryOp\GreaterOrEqual($node->left, $node->right))
?? $this->getType($node->left)->isSmallerThan($this->getType($node->right), $this->phpVersion)->toBooleanType();
}

if ($node instanceof Expr\BinaryOp\SmallerOrEqual) {
return $this->getType($node->left)->isSmallerThanOrEqual($this->getType($node->right), $this->phpVersion)->toBooleanType();
return $this->resolveComplementaryComparison($node, new Expr\BinaryOp\Greater($node->left, $node->right))
?? $this->getType($node->left)->isSmallerThanOrEqual($this->getType($node->right), $this->phpVersion)->toBooleanType();
}

if ($node instanceof Expr\BinaryOp\Greater) {
return $this->getType($node->right)->isSmallerThan($this->getType($node->left), $this->phpVersion)->toBooleanType();
return $this->resolveComplementaryComparison($node, new Expr\BinaryOp\SmallerOrEqual($node->left, $node->right))
?? $this->getType($node->right)->isSmallerThan($this->getType($node->left), $this->phpVersion)->toBooleanType();
}

if ($node instanceof Expr\BinaryOp\GreaterOrEqual) {
return $this->getType($node->right)->isSmallerThanOrEqual($this->getType($node->left), $this->phpVersion)->toBooleanType();
return $this->resolveComplementaryComparison($node, new Expr\BinaryOp\Smaller($node->left, $node->right))
?? $this->getType($node->right)->isSmallerThanOrEqual($this->getType($node->left), $this->phpVersion)->toBooleanType();
}

if ($node instanceof Expr\BinaryOp\Equal) {
Expand Down Expand Up @@ -1609,6 +1613,24 @@ private function getNullsafeShortCircuitingType(Expr $expr, Type $type): Type
return $type;
}

private function resolveComplementaryComparison(Expr\BinaryOp $node, Expr\BinaryOp $complement): ?Type
{
$complementKey = $this->getNodeKey($complement);
if (!array_key_exists($complementKey, $this->expressionTypes)) {
return null;
}

$complementType = $this->expressionTypes[$complementKey]->getType();
if ($complementType->isTrue()->yes()) {
return new ConstantBooleanType(false);
}
if ($complementType->isFalse()->yes()) {
return new ConstantBooleanType(true);
}

return null;
}

private function transformVoidToNull(Type $type, Node $node): Type
{
if ($node->getAttribute(self::KEEP_VOID_ATTRIBUTE_NAME) === true) {
Expand Down
6 changes: 6 additions & 0 deletions tests/PHPStan/Rules/Comparison/MatchExpressionRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,12 @@ public function testBug9534(): void
]);
}

#[RequiresPhp('>= 8.0')]
public function testBug5610(): void
{
$this->analyse([__DIR__ . '/data/bug-5610.php'], []);
}

#[RequiresPhp('>= 8.0')]
public function testBug11310(): void
{
Expand Down
33 changes: 33 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/bug-5610.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php // lint >= 8.0

declare(strict_types = 1);

namespace Bug5610;

function foo(int $bar, int $baz): int {
return match (true) {
$bar < $baz => 1,
$bar >= $baz => 2,
};
}

function foo3(int $bar, int $baz): int {
return match (true) {
$bar > $baz => 1,
$bar <= $baz => 2,
};
}

function foo4(int $bar, int $baz): int {
return match (true) {
$bar <= $baz => 1,
$bar > $baz => 2,
};
}

function foo5(int $bar, int $baz): int {
return match (true) {
$bar >= $baz => 1,
$bar < $baz => 2,
};
}
Loading