Repository navigation
Type min() / max() of two arguments as the union of both comparison orders #7050
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8ef92fc
Type min() / max() of two arguments as the union of both comparison o…
zonuexe eefc51e
Test min() / max() with NAN and int|float arguments in the comparison…
zonuexe 30c6874
Declare int|float in the min() / max() fixtures with PHPDoc so they l…
zonuexe 021679f
Do not narrow constant arguments of min() / max() by the comparison i…
zonuexe 14e39d6
Keep literal arguments of min() / max() out of AlwaysRememberedExpr
zonuexe 48e94fc
Decide the comparison operands of min() / max() by the type, not the …
zonuexe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace MinMaxNan; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| class Foo | ||
| { | ||
|
|
||
| public const NEG = -1; | ||
|
|
||
| public function floats(float $f, float $g): void | ||
| { | ||
| assertType('NAN|float<0.0, max>', max(0.0, $f)); | ||
| assertType('NAN|float<0.0, max>', max($f, 0.0)); | ||
| assertType('NAN|float<min, 1.0>', min(1.0, $f)); | ||
| assertType('NAN|float<min, 1.0>', min($f, 1.0)); | ||
| assertType('float', max($f, $g)); | ||
| assertType('float', min($f, $g)); | ||
|
|
||
| assertType('NAN|float<0.0, 1.0>', max(0.0, min(1.0, $f))); | ||
| assertType('NAN|float<0.0, 1.0>', min(1.0, max(0.0, $f))); | ||
| assertType('NAN|float<0.0, 1.0>', max(min($f, 1.0), 0.0)); | ||
| } | ||
|
|
||
| /** | ||
| * @param float<0.0, 1.0> $unit | ||
| * @param float<10.0, 20.0> $high | ||
| */ | ||
| public function ranges(float $unit, float $high, float $f): void | ||
| { | ||
| assertType('float<10.0, 20.0>', max($unit, $high)); | ||
| assertType('float<10.0, 20.0>', max($high, $unit)); | ||
| assertType('float<0.0, 1.0>', min($unit, $high)); | ||
| assertType('float<0.0, 1.0>', min($high, $unit)); | ||
| assertType('float<0.0, 1.0>', max(0.0, min(1.0, $unit))); | ||
|
|
||
| assertType('NAN|float<0.0, max>', max($unit, $f)); | ||
| assertType('NAN|float<0.0, max>', max($f, $unit)); | ||
| assertType('NAN|float<min, 20.0>', min($high, $f)); | ||
| assertType('NAN|float<min, 20.0>', min($f, $high)); | ||
| } | ||
|
|
||
| public function nan(bool $b): void | ||
| { | ||
| assertType('1|NAN', max(1, NAN)); | ||
| assertType('1|NAN', max(NAN, 1)); | ||
| assertType('1|NAN', min(1, NAN)); | ||
| assertType('1|NAN', min(NAN, 1)); | ||
| assertType('NAN', max(NAN, NAN)); | ||
|
|
||
| $n = $b ? NAN : 2.0; | ||
| assertType('1.0|2.0|NAN', max($n, 1.0)); | ||
| assertType('1.0|2.0|NAN', max(1.0, $n)); | ||
| assertType('2.0|3.0|NAN', min($n, 3.0)); | ||
| assertType('2.0|3.0|NAN', min(3.0, $n)); | ||
| } | ||
|
|
||
| public function mixedNumbers(int $i, float $f, string $s): void | ||
| { | ||
| assertType('0|NAN|float<5.0E-324, max>', max(0, $f)); | ||
| assertType('0|NAN|float<0.0, max>', max($f, 0)); | ||
| assertType('float|int', max($i, $f)); | ||
| assertType('float|int', min($f, $i)); | ||
| assertType('float|string', max($f, $s)); | ||
| assertType("NAN|'1'", max('1', NAN)); | ||
| assertType("NAN|'1'", min(NAN, '1')); | ||
| } | ||
|
|
||
| /** @param int<0, max> $count */ | ||
| public function ties(int $count): void | ||
| { | ||
| assertType('0', max(0, 0.0)); | ||
| assertType('0.0', max(0.0, 0)); | ||
| assertType('0|0.0', min(0, 0.0)); | ||
| assertType('0|0.0', min(0.0, 0)); | ||
| assertType('0.0', max(0.0, -0.0)); | ||
| assertType('-0.0|0.0', min(0.0, -0.0)); | ||
| assertType("1|'1'", min(1, '1')); | ||
| assertType('1', max(1, '1')); | ||
| assertType('0|0.0', min($count, 0.0)); | ||
| assertType('0|0.0', min(0.0, $count)); | ||
| } | ||
|
|
||
| /** | ||
| * @param int<1, 10> $i | ||
| * @param int<5, 20> $j | ||
| */ | ||
| public function ints(int $i, int $j, int $k): void | ||
| { | ||
| assertType('int<5, 20>', max($i, $j)); | ||
| assertType('int<1, 10>', min($i, $j)); | ||
| assertType('int<1, max>', max($i, $k)); | ||
| assertType('int<min, 10>', min($i, $k)); | ||
| assertType('2', max(1, 2)); | ||
| assertType('1', min(1, 2)); | ||
| } | ||
|
|
||
| public function negativeConstants(): void | ||
| { | ||
| assertType('-1|NAN', max(NAN, -1)); | ||
| assertType('-1|NAN', max(-1, NAN)); | ||
| assertType('-1|NAN', min(NAN, -1)); | ||
| assertType('-1|NAN', min(-1, NAN)); | ||
| } | ||
|
|
||
| public function constantsNotWrittenAsLiterals(): void | ||
| { | ||
| assertType('-1|NAN', max(NAN, Foo::NEG)); | ||
| assertType('-1|NAN', max(Foo::NEG, NAN)); | ||
| assertType('-1|NAN', min(NAN, Foo::NEG)); | ||
| } | ||
|
|
||
| /** | ||
| * @param 1|10 $x | ||
| */ | ||
| public function unionOfConstants(int $x): void | ||
| { | ||
| assertType('5|10', max($x, 5)); | ||
| assertType('1|5', min($x, 5)); | ||
| } | ||
|
|
||
| /** @param int|float $n */ | ||
| public function intOrFloat($n, int $i): void | ||
| { | ||
| assertType('NAN|float<min, 100.0>|int<min, 100>', min($n, 100)); | ||
| assertType('NAN|float<min, 100.0>|int<min, 100>', min(100, $n)); | ||
| assertType('NAN|float<100.0, max>|int<100, max>', max($n, 100)); | ||
| assertType('NAN|float<100.00000000000001, max>|int<100, max>', max(100, $n)); | ||
| assertType('float|int', min($n, $i)); | ||
| assertType('float|int', min($i, $n)); | ||
| } | ||
|
|
||
| public function callables(float $f): void | ||
| { | ||
| assertType('NAN|float<0.0, max>', call_user_func('max', 0.0, $f)); | ||
| assertType('NAN|float<0.0, max>', call_user_func('max', $f, 0.0)); | ||
| assertType('NAN|float<min, 1.0>', call_user_func('min', 1.0, $f)); | ||
| assertType('NAN|float<min, 1.0>', call_user_func('min', $f, 1.0)); | ||
| assertType('non-empty-list<NAN|float<0.0, max>>', array_map('max', [0.0], [$f])); | ||
| assertType('non-empty-list<NAN|float<0.0, max>>', array_map('max', [$f], [0.0])); | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace MinMaxNanComparison; | ||
|
|
||
| class Foo | ||
| { | ||
|
|
||
| public function max(float $f): bool | ||
| { | ||
| return is_nan(max(0.0, $f)); | ||
| } | ||
|
|
||
| public function clamp(float $f): bool | ||
| { | ||
| return is_nan(\max(0.0, \min(1.0, $f))); | ||
| } | ||
|
|
||
| /** @param int|float $n */ | ||
| public function min($n): bool | ||
| { | ||
| return \min($n, 100) === 100.0; | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.