Skip to content

Commit

Permalink
Fix rounding functions for maybe-null types
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Feb 6, 2022
1 parent 5162bcf commit 3e014c2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
7 changes: 2 additions & 5 deletions src/Type/Php/RoundFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
// PHP 8 fatals with a missing parameter.
$noArgsReturnType = new NeverType(true);
// PHP 8 can either return a float or fatal.
$defaultReturnType = new BenevolentUnionType([
new FloatType(),
new NeverType(true),
]);
$defaultReturnType = new FloatType();
} else {
// PHP 7 returns null with a missing parameter.
$noArgsReturnType = new NullType();
Expand All @@ -74,7 +71,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
new IntegerType(),
new FloatType(),
);
if (!$allowed->accepts($firstArgType, true)->yes()) {
if ($allowed->isSuperTypeOf($firstArgType)->no()) {
// PHP 8 fatals if the parameter is not an integer or float.
return new NeverType(true);
}
Expand Down
14 changes: 11 additions & 3 deletions tests/PHPStan/Analyser/data/round-php8.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,27 @@

use function PHPStan\Testing\assertType;

$maybeNull = null;
if (rand(0, 1)) {
$maybeNull = 1.0;
}

// Round
assertType('float', round(123));
assertType('float', round(123.456));
assertType('float', round($_GET['foo'] / 60));
assertType('*NEVER*', round('123'));
assertType('*NEVER*', round('123.456'));
assertType('*NEVER*', round(null));
assertType('float', round($maybeNull));
assertType('*NEVER*', round(true));
assertType('*NEVER*', round(false));
assertType('*NEVER*', round(new \stdClass));
assertType('*NEVER*', round(''));
assertType('*NEVER*', round(array()));
assertType('*NEVER*', round(array(123)));
assertType('*NEVER*', round());
assertType('(*NEVER*|float)', round($_GET['foo']));
assertType('float', round($_GET['foo']));

// Ceil
assertType('float', ceil(123));
Expand All @@ -27,14 +33,15 @@
assertType('*NEVER*', ceil('123'));
assertType('*NEVER*', ceil('123.456'));
assertType('*NEVER*', ceil(null));
assertType('float', ceil($maybeNull));
assertType('*NEVER*', ceil(true));
assertType('*NEVER*', ceil(false));
assertType('*NEVER*', ceil(new \stdClass));
assertType('*NEVER*', ceil(''));
assertType('*NEVER*', ceil(array()));
assertType('*NEVER*', ceil(array(123)));
assertType('*NEVER*', ceil());
assertType('(*NEVER*|float)', ceil($_GET['foo']));
assertType('float', ceil($_GET['foo']));

// Floor
assertType('float', floor(123));
Expand All @@ -43,11 +50,12 @@
assertType('*NEVER*', floor('123'));
assertType('*NEVER*', floor('123.456'));
assertType('*NEVER*', floor(null));
assertType('float', floor($maybeNull));
assertType('*NEVER*', floor(true));
assertType('*NEVER*', floor(false));
assertType('*NEVER*', floor(new \stdClass));
assertType('*NEVER*', floor(''));
assertType('*NEVER*', floor(array()));
assertType('*NEVER*', floor(array(123)));
assertType('*NEVER*', floor());
assertType('(*NEVER*|float)', floor($_GET['foo']));
assertType('float', floor($_GET['foo']));
8 changes: 8 additions & 0 deletions tests/PHPStan/Analyser/data/round.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@

use function PHPStan\Testing\assertType;

$maybeNull = null;
if (rand(0, 1)) {
$maybeNull = 1.0;
}

// Round
assertType('float', round(123));
assertType('float', round(123.456));
assertType('float', round($_GET['foo'] / 60));
assertType('float', round('123'));
assertType('float', round('123.456'));
assertType('float', round(null));
assertType('float', round($maybeNull));
assertType('float', round(true));
assertType('float', round(false));
assertType('float', round(new \stdClass));
Expand All @@ -27,6 +33,7 @@
assertType('float', ceil('123'));
assertType('float', ceil('123.456'));
assertType('float', ceil(null));
assertType('float', ceil($maybeNull));
assertType('float', ceil(true));
assertType('float', ceil(false));
assertType('float', ceil(new \stdClass));
Expand All @@ -43,6 +50,7 @@
assertType('float', floor('123'));
assertType('float', floor('123.456'));
assertType('float', floor(null));
assertType('float', floor($maybeNull));
assertType('float', floor(true));
assertType('float', floor(false));
assertType('float', floor(new \stdClass));
Expand Down

0 comments on commit 3e014c2

Please sign in to comment.