Skip to content

Commit 80e4805

Browse files
committed
Corrected bcdiv and bcmul return types for PHP 8.
1 parent 42c72f3 commit 80e4805

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

src/Type/Php/BcMathStringOrNullReturnTypeExtension.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PhpParser\Node\Expr\FuncCall;
66
use PhpParser\Node\Expr\UnaryMinus;
77
use PHPStan\Analyser\Scope;
8+
use PHPStan\Php\PhpVersion;
89
use PHPStan\Reflection\FunctionReflection;
910
use PHPStan\Type\Accessory\AccessoryNumericStringType;
1011
use PHPStan\Type\Constant\ConstantBooleanType;
@@ -22,6 +23,14 @@
2223
class BcMathStringOrNullReturnTypeExtension implements \PHPStan\Type\DynamicFunctionReturnTypeExtension
2324
{
2425

26+
/** @var PhpVersion */
27+
private $phpVersion;
28+
29+
public function __construct(PhpVersion $phpVersion)
30+
{
31+
$this->phpVersion = $phpVersion;
32+
}
33+
2534
public function isFunctionSupported(FunctionReflection $functionReflection): bool
2635
{
2736
return in_array($functionReflection->getName(), ['bcdiv', 'bcmod', 'bcpowmod', 'bcsqrt'], true);
@@ -41,7 +50,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
4150

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

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

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

128137
/**
129-
* bcpowmod()
138+
* @see bcpowmod()
130139
* https://www.php.net/manual/en/function.bcpowmod.php
131140
* > Returns the result as a string, or FALSE if modulus is 0 or exponent is negative.
132141
* @param FuncCall $functionCall

tests/PHPStan/Analyser/NodeScopeResolverTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,11 @@ public function dataFileAsserts(): iterable
122122
yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/Comparison/data/bug-2550.php');
123123
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-2899.php');
124124
yield from $this->gatherAssertTypes(__DIR__ . '/data/preg_split.php');
125-
yield from $this->gatherAssertTypes(__DIR__ . '/data/bcmath-dynamic-return.php');
125+
126+
if (PHP_VERSION_ID < 80000) {
127+
yield from $this->gatherAssertTypes(__DIR__ . '/data/bcmath-dynamic-return.php');
128+
}
129+
126130
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-3875.php');
127131
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-2611.php');
128132
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-3548.php');

0 commit comments

Comments
 (0)