6
6
use PHPStan \Analyser \MutatingScope ;
7
7
use PHPStan \Analyser \Scope ;
8
8
use PHPStan \Node \Printer \ExprPrinter ;
9
+ use PHPStan \Php \PhpVersion ;
9
10
use PHPStan \Rules \Rule ;
10
11
use PHPStan \Rules \RuleErrorBuilder ;
11
12
use PHPStan \Rules \RuleLevelHelper ;
12
13
use PHPStan \ShouldNotHappenException ;
13
14
use PHPStan \TrinaryLogic ;
14
15
use PHPStan \Type \ErrorType ;
16
+ use PHPStan \Type \ObjectType ;
15
17
use PHPStan \Type \Type ;
16
18
use PHPStan \Type \VerbosityLevel ;
17
19
use function sprintf ;
@@ -26,6 +28,7 @@ final class InvalidBinaryOperationRule implements Rule
26
28
27
29
public function __construct (
28
30
private ExprPrinter $ exprPrinter ,
31
+ private PhpVersion $ phpVersion ,
29
32
private RuleLevelHelper $ ruleLevelHelper ,
30
33
)
31
34
{
@@ -70,9 +73,13 @@ public function processNode(Node $node, Scope $scope): array
70
73
if ($ node instanceof Node \Expr \AssignOp \Concat || $ node instanceof Node \Expr \BinaryOp \Concat) {
71
74
$ callback = static fn (Type $ type ): bool => !$ type ->toString () instanceof ErrorType;
72
75
} elseif ($ node instanceof Node \Expr \AssignOp \Plus || $ node instanceof Node \Expr \BinaryOp \Plus) {
73
- $ callback = static fn (Type $ type ): bool => !$ type ->toNumber () instanceof ErrorType || $ type ->isArray ()->yes ();
76
+ $ callback = $ this ->phpVersion ->supportsBcMathNumberOperatorOverloading ()
77
+ ? static fn (Type $ type ): bool => !$ type ->toNumber () instanceof ErrorType || $ type ->isArray ()->yes () || $ type ->isSuperTypeOf (new ObjectType ('BcMath\Number ' ))->yes ()
78
+ : static fn (Type $ type ): bool => !$ type ->toNumber () instanceof ErrorType || $ type ->isArray ()->yes ();
74
79
} else {
75
- $ callback = static fn (Type $ type ): bool => !$ type ->toNumber () instanceof ErrorType;
80
+ $ callback = $ this ->phpVersion ->supportsBcMathNumberOperatorOverloading ()
81
+ ? static fn (Type $ type ): bool => !$ type ->toNumber () instanceof ErrorType || $ type ->isSuperTypeOf (new ObjectType ('BcMath\Number ' ))->yes ()
82
+ : static fn (Type $ type ): bool => !$ type ->toNumber () instanceof ErrorType;
76
83
}
77
84
78
85
$ leftType = $ this ->ruleLevelHelper ->findTypeToCheck (
0 commit comments