Skip to content

Commit 33af274

Browse files
authored
support unary-minus on IntegerRangeType
1 parent 05c5ad3 commit 33af274

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/Analyser/MutatingScope.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,6 +1006,7 @@ private function resolveType(Expr $node): Type
10061006
if ($node instanceof Node\Expr\UnaryMinus) {
10071007
$type = $this->getType($node->expr)->toNumber();
10081008
$scalarValues = TypeUtils::getConstantScalars($type);
1009+
10091010
if (count($scalarValues) > 0) {
10101011
$newTypes = [];
10111012
foreach ($scalarValues as $scalarValue) {
@@ -1019,6 +1020,15 @@ private function resolveType(Expr $node): Type
10191020
return TypeCombinator::union(...$newTypes);
10201021
}
10211022

1023+
if ($type instanceof IntegerRangeType) {
1024+
$negativeRange = $this->resolveType(new Node\Expr\BinaryOp\Mul($node->expr, new LNumber(-1)));
1025+
1026+
if ( $negativeRange instanceof IntegerRangeType && ($negativeRange->getMin() === null || $negativeRange->getMax() === null)) {
1027+
return IntegerRangeType::fromInterval($negativeRange->getMax(), $negativeRange->getMin());
1028+
}
1029+
return $negativeRange;
1030+
}
1031+
10221032
return $type;
10231033
}
10241034

tests/PHPStan/Analyser/data/integer-range-types.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,4 +278,21 @@ public function math($i, $j, $z, $pi, $r1, $r2, $rMin, $rMax, $x, $y) {
278278

279279
assertType('5|10|15|20|30', $x / $y);
280280
}
281+
282+
/**
283+
* @param int<1, 10> $r1
284+
* @param int<-5, 10> $r2
285+
* @param int<min, 5> $rMin
286+
* @param int<5, max> $rMax
287+
* @param int<0, 50> $rZero
288+
*/
289+
public function unaryMinus($r1, $r2, $rMin, $rMax, $rZero) {
290+
291+
assertType('int<-10, -1>', -$r1);
292+
assertType('int<-10, 5>', -$r2);
293+
assertType('int<-5, max>', -$rMin);
294+
assertType('int<min, -5>', -$rMax);
295+
assertType('int<-50, 0>', -$rZero);
296+
}
297+
281298
}

0 commit comments

Comments
 (0)