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

IntegerRangeMath: cover more maxima cases #702

Merged
merged 4 commits into from
Oct 11, 2021
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
9 changes: 9 additions & 0 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -5300,6 +5300,15 @@ private function integerRangeMath(Type $range, Expr $node, Type $operand): Type
$max = $rangeMax !== null && $operand->getMax() !== null && $operand->getMax() !== 0 ? $rangeMax / $operand->getMax() : null;
}

if ($range instanceof IntegerRangeType && $operand instanceof IntegerRangeType) {
if ($rangeMax === null && $operand->getMax() === null) {
$min = 0;
} elseif ($rangeMin === null && $operand->getMin() === null) {
$min = null;
$max = null;
}
}

if ($operand instanceof IntegerRangeType
&& ($operand->getMin() === null || $operand->getMax() === null)
|| ($rangeMin === null || $rangeMax === null)
Expand Down
10 changes: 10 additions & 0 deletions tests/PHPStan/Analyser/data/integer-range-types.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,16 @@ public function math($i, $j, $z, $pi, $r1, $r2, $r3, $rMin, $rMax, $x, $y) {

assertType('5|10|15|20|30', $x / $y);

assertType('float|int<0, max>', $rMax / $rMax);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

In Psalm I chose not to go this route and just inferred int|float. There's a point where the line is too blurry. In this case, whatever operations or expectation you have that would justify knowing the range will get invalidated by float. You can't deduce the result is an int, nor a float, you can't deduce it's positive.

Copy link
Member

Choose a reason for hiding this comment

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

I think we'd have to fight current way of working of PHPStan's typesystem to make the result less precise like that. I don't think it's worth it (unless there's a bug stemming from that).

Copy link
Contributor

Choose a reason for hiding this comment

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

@ondrejmirtes I'm not sure I understood what you meant by that

Copy link
Contributor Author

Choose a reason for hiding this comment

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

to sum up: the assertion for this line should be changed to float|int ?

Copy link
Member

Choose a reason for hiding this comment

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

@orklah It's harder for this to make float|int instead of the current result. I'd leave it the same.

Copy link
Contributor Author

@staabm staabm Oct 7, 2021

Choose a reason for hiding this comment

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

@orklah I mentioned the cast-cases in phpstan/phpstan#5731 (comment)

not sure how hard it would be to get the remaing failing cast-case work though

Copy link
Contributor

Choose a reason for hiding this comment

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

well, the current type is also wrong https://phpstan.org/r/b6b8648f-b0a3-4c37-97cf-12ee838d3336

Copy link
Contributor Author

@staabm staabm Oct 7, 2021

Choose a reason for hiding this comment

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

yep, this case is fixed with this PR, see assertion in line 284

Copy link
Contributor Author

Choose a reason for hiding this comment

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

back to the initial question: IMO this is the most correct type we can expect atm (until phpstan supports float-ranges).

since its also the one which is mathematically correct and we need the implementation lines anayway for the other expectations to pass with this PR, we should merge as is IMO.

assertType('(float|int)', $rMin / $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.

}

/**
* @param int<0, max> $a
* @param int<0, max> $b
*/
function divisionLoosesInformation(int $a, int $b): void {
Copy link
Contributor Author

@staabm staabm Oct 7, 2021

Choose a reason for hiding this comment

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

thats the actual case/feature reported/requested in phpstan/phpstan#5731

assertType('float|int<0, max>',$a/$b);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

}

/**
Expand Down