-
Notifications
You must be signed in to change notification settings - Fork 483
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
Handle BcMath\Number operators for simple cases #3787
base: 2.1.x
Are you sure you want to change the base?
Changes from 4 commits
af1eef3
e8d0051
b551de8
873450c
81b7706
81b60c6
90193e2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type\Php; | ||
|
||
use PHPStan\Php\PhpVersion; | ||
use PHPStan\Type\ErrorType; | ||
use PHPStan\Type\NeverType; | ||
use PHPStan\Type\ObjectType; | ||
use PHPStan\Type\OperatorTypeSpecifyingExtension; | ||
use PHPStan\Type\Type; | ||
use function in_array; | ||
|
||
final class BcMathNumberOperatorTypeSpecifyingExtension implements OperatorTypeSpecifyingExtension | ||
{ | ||
|
||
public function __construct(private PhpVersion $phpVersion) | ||
{ | ||
} | ||
|
||
public function isOperatorSupported(string $operatorSigil, Type $leftSide, Type $rightSide): bool | ||
{ | ||
if (!$this->phpVersion->supportsBcMathNumberOperatorOverloading() || $leftSide instanceof NeverType || $rightSide instanceof NeverType) { | ||
return false; | ||
} | ||
|
||
$bcMathNumberType = new ObjectType('BcMath\Number'); | ||
|
||
return in_array($operatorSigil, ['-', '+', '*', '/', '**', '%'], true) | ||
&& ( | ||
$bcMathNumberType->isSuperTypeOf($leftSide)->yes() | ||
|| $bcMathNumberType->isSuperTypeOf($rightSide)->yes() | ||
); | ||
} | ||
|
||
public function specifyType(string $operatorSigil, Type $leftSide, Type $rightSide): Type | ||
{ | ||
$bcMathNumberType = new ObjectType('BcMath\Number'); | ||
$otherSide = $bcMathNumberType->isSuperTypeOf($leftSide)->yes() | ||
? $rightSide | ||
: $leftSide; | ||
|
||
if ( | ||
$otherSide->isInteger()->yes() | ||
|| $otherSide->isNumericString()->yes() | ||
|| $bcMathNumberType->isSuperTypeOf($otherSide)->yes() | ||
) { | ||
return $bcMathNumberType; | ||
} | ||
|
||
return new ErrorType(); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,11 +47,11 @@ function (): void { | |
$x = 4; | ||
} | ||
|
||
assertType('int<4, 27>|int<16, 81>', pow($range, $x)); | ||
assertType('int<4, 27>|int<16, 81>', $range ** $x); | ||
assertType('int<4, 81>', pow($range, $x)); | ||
assertType('int<4, 81>', $range ** $x); | ||
|
||
assertType('int<4, 27>|int<16, 64>', pow($x, $range)); | ||
assertType('int<4, 27>|int<16, 64>', $x ** $range); | ||
assertType('int<4, 64>', pow($x, $range)); | ||
assertType('int<4, 64>', $x ** $range); | ||
Comment on lines
-50
to
+54
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a result of EDIT: I had to fix it, because it broke the test on PHP <8.4 due to the extension only running on 8.4 and above. |
||
|
||
assertType('int<4, 27>', pow($range, $range)); | ||
assertType('int<4, 27>', $range ** $range); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I copied this from the original PR. But I'm not sure whether it's correct. I assume that the reason we even need this is because earlier PHP versions might use a polyfill (which wouldn't have the operators overloaded). But PHP 8.4 can also use a polyfill, because bcmath is an optional extension.