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

Fixed integer range minus bugs #690

Merged
merged 10 commits into from
Oct 5, 2021
Merged
Show file tree
Hide file tree
Changes from 8 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
13 changes: 11 additions & 2 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -5241,7 +5241,11 @@ private function integerRangeMath(Type $range, Expr $node, Type $operand): Type
if ($operand->getMin() === null) {
$min = null;
} elseif ($rangeMin !== null) {
$min = $rangeMin - $operand->getMin();
if ($operand->getMax() !== null) {
$min = $rangeMin - $operand->getMax();
} else {
$min = $rangeMin - $operand->getMin();
}
} else {
$min = null;
}
Expand All @@ -5250,7 +5254,12 @@ private function integerRangeMath(Type $range, Expr $node, Type $operand): Type
$min = null;
$max = null;
} elseif ($rangeMax !== null) {
$max = $rangeMax - $operand->getMax();
if ($operand->getMin() === null) {
$min = $rangeMin - $operand->getMax();
$max = null;
} else {
$max = $rangeMax - $operand->getMin();
}
} else {
$max = null;
}
Expand Down
13 changes: 8 additions & 5 deletions tests/PHPStan/Analyser/data/integer-range-types.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,14 @@ public function supportsPhpdocIntegerRange() {
* @param positive-int $pi
* @param int<1, 10> $r1
* @param int<5, 10> $r2
* @param int<-9, 100> $r3
* @param int<min, 5> $rMin
* @param int<5, max> $rMax
*
* @param 20|40|60 $x
* @param 2|4 $y
*/
public function math($i, $j, $z, $pi, $r1, $r2, $rMin, $rMax, $x, $y) {
public function math($i, $j, $z, $pi, $r1, $r2, $r3, $rMin, $rMax, $x, $y) {
assertType('int', $r1 + $i);
assertType('int', $r1 - $i);
assertType('int', $r1 * $i);
Expand Down Expand Up @@ -254,16 +255,18 @@ public function math($i, $j, $z, $pi, $r1, $r2, $rMin, $rMax, $x, $y) {
assertType('float|int<1, max>', $rMax / 4);

assertType('int<6, 20>', $r1 + $r2);
assertType('int<-4, 0>', $r1 - $r2);
assertType('int<-9, 5>', $r1 - $r2);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assertType('int<5, 100>', $r1 * $r2);
assertType('float|int<0, 1>', $r1 / $r2);

assertType('int<-99, 19>', $r1 - $r3);
Copy link
Contributor Author

@staabm staabm Sep 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this line is covering https://phpstan.org/r/0f2bf43b-e5a6-436b-ac44-e62a2c60ae94 from "Wrong inference with Minus" of phpstan/phpstan#5614


assertType('int<min, 15>', $r1 + $rMin);
assertType('int<min, 5>', $r1 - $rMin);
assertType('int<-4, max>', $r1 - $rMin);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assertType('int<min, 50>', $r1 * $rMin);
assertType('float|int<min, 2>', $r1 / $rMin);
assertType('int<min, 15>', $rMin + $r1);
assertType('int<min, -5>', $rMin - $r1);
assertType('int<min, 4>', $rMin - $r1);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assertType('int<min, 50>', $rMin * $r1);
assertType('float|int<min, 0>', $rMin / $r1);

Expand All @@ -272,7 +275,7 @@ public function math($i, $j, $z, $pi, $r1, $r2, $rMin, $rMax, $x, $y) {
assertType('int<5, max>', $r1 * $rMax);
assertType('float|int<0, max>', $r1 / $rMax);
assertType('int<6, max>', $rMax + $r1);
assertType('int<4, max>', $rMax - $r1);
assertType('int<-5, max>', $rMax - $r1);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assertType('int<5, max>', $rMax * $r1);
assertType('float|int<5, max>', $rMax / $r1);

Expand Down
8 changes: 4 additions & 4 deletions tests/PHPStan/Analyser/data/math.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,19 @@ public function doBaz(int $rangeFiveBoth, int $rangeFiveLeft, int $rangeFiveRigh
assertType('int', $rangeFiveRight - $rangeFiveLeft);

assertType('int<-10, 10>', $rangeFiveBoth + $rangeFiveBoth);
assertType('0', $rangeFiveBoth - $rangeFiveBoth);
assertType('int<-10, 10>', $rangeFiveBoth - $rangeFiveBoth);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


assertType('int<-10, max>', $rangeFiveBoth + $rangeFiveLeft);
assertType('int', $rangeFiveBoth - $rangeFiveLeft);

assertType('int<min, 10>', $rangeFiveBoth + $rangeFiveRight);
assertType('int<min, 0>', $rangeFiveBoth - $rangeFiveRight);
assertType('int<-10, max>', $rangeFiveBoth - $rangeFiveRight);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


assertType('int<-10, max>', $rangeFiveLeft + $rangeFiveBoth);
assertType('int<0, max>', $rangeFiveLeft - $rangeFiveBoth);
assertType('int<-10, max>', $rangeFiveLeft - $rangeFiveBoth);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


assertType('int<min, 10>', $rangeFiveRight + $rangeFiveBoth);
assertType('int<min, 0>', $rangeFiveRight - $rangeFiveBoth);
assertType('int<min, 10>', $rangeFiveRight - $rangeFiveBoth);
Copy link
Contributor Author

@staabm staabm Sep 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

public function doLorem($a, $b): void
Expand Down