Skip to content

Commit

Permalink
Switch to injecting the PHP version logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
johnbillion authored and ondrejmirtes committed Feb 2, 2022
1 parent 3a486a9 commit d916dd3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/Php/PhpVersion.php
Expand Up @@ -156,6 +156,11 @@ public function supportsCaseInsensitiveConstantNames(): bool
return $this->versionId < 80000;
}

public function hasStricterRoundFunctions(): bool
{
return $this->versionId >= 80000;
}

public function hasTentativeReturnTypes(): bool
{
return $this->versionId >= 80100;
Expand Down
9 changes: 6 additions & 3 deletions src/Type/Php/RoundFunctionReturnTypeExtension.php
Expand Up @@ -4,6 +4,7 @@

use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\Php\PhpVersion;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Type\BenevolentUnionType;
use PHPStan\Type\Constant\ConstantBooleanType;
Expand All @@ -17,10 +18,12 @@
use PHPStan\Type\TypeCombinator;
use function count;
use function in_array;
use const PHP_VERSION_ID;

class RoundFunctionReturnTypeExtension implements DynamicFunctionReturnTypeExtension
{
public function __construct(private PhpVersion $phpVersion)
{
}

public function isFunctionSupported(FunctionReflection $functionReflection): bool
{
Expand All @@ -37,7 +40,7 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo

public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type
{
if (PHP_VERSION_ID >= 80000) {
if ($this->phpVersion->hasStricterRoundFunctions()) {
// PHP 8 fatals with a missing parameter.
$noArgsReturnType = new NeverType(true);
// PHP 8 can either return a float or fatal.
Expand Down Expand Up @@ -65,7 +68,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
return $defaultReturnType;
}

if (PHP_VERSION_ID >= 80000) {
if ($this->phpVersion->hasStricterRoundFunctions()) {
$allowed = TypeCombinator::union(
new IntegerType(),
new FloatType(),
Expand Down

0 comments on commit d916dd3

Please sign in to comment.