Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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/Type/Php/BcMathStringOrNullReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\UnaryMinus;
use PHPStan\Analyser\Scope;
use PHPStan\Php\PhpVersion;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Type\Accessory\AccessoryNumericStringType;
use PHPStan\Type\Constant\ConstantBooleanType;
Expand All @@ -22,6 +23,14 @@
class BcMathStringOrNullReturnTypeExtension implements \PHPStan\Type\DynamicFunctionReturnTypeExtension
{

/** @var PhpVersion */
private $phpVersion;

public function __construct(PhpVersion $phpVersion)
{
$this->phpVersion = $phpVersion;
}

public function isFunctionSupported(FunctionReflection $functionReflection): bool
{
return in_array($functionReflection->getName(), ['bcdiv', 'bcmod', 'bcpowmod', 'bcsqrt'], true);
Expand All @@ -41,7 +50,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,

$defaultReturnType = new UnionType([$stringAndNumericStringType, new NullType()]);

if (isset($functionCall->args[1]) === false) {
if (isset($functionCall->args[1]) === false || $this->phpVersion->getVersionId() >= 80000) {
return $stringAndNumericStringType;
}

Expand Down Expand Up @@ -126,7 +135,7 @@ private function getTypeForBcSqrt(FuncCall $functionCall, Scope $scope): Type
}

/**
* bcpowmod()
* @see bcpowmod()
* https://www.php.net/manual/en/function.bcpowmod.php
* > Returns the result as a string, or FALSE if modulus is 0 or exponent is negative.
* @param FuncCall $functionCall
Expand Down
6 changes: 5 additions & 1 deletion tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,11 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/Comparison/data/bug-2550.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-2899.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/preg_split.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bcmath-dynamic-return.php');

if (PHP_VERSION_ID < 80000) {
yield from $this->gatherAssertTypes(__DIR__ . '/data/bcmath-dynamic-return.php');
Copy link
Member

Choose a reason for hiding this comment

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

What happens on PHP 8 also needs to be tested.

}

yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-3875.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-2611.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-3548.php');
Expand Down