From 4e25325613e5d8b2bd67a87d4bbb86d5acaa40bd Mon Sep 17 00:00:00 2001 From: Mikko Pesari Date: Mon, 16 Feb 2026 18:19:31 +0200 Subject: [PATCH] Fix BcMath\Number increment/decrement --- .../Operators/InvalidIncDecOperationRule.php | 4 ++++ tests/PHPStan/Analyser/nsrt/bcmath-number.php | 8 +++++++ .../InvalidIncDecOperationRuleTest.php | 6 ++++++ .../Operators/data/inc-dec-bcmath-number.php | 21 +++++++++++++++++++ 4 files changed, 39 insertions(+) create mode 100644 tests/PHPStan/Rules/Operators/data/inc-dec-bcmath-number.php diff --git a/src/Rules/Operators/InvalidIncDecOperationRule.php b/src/Rules/Operators/InvalidIncDecOperationRule.php index 44ea25b7e0..8c50854e7f 100644 --- a/src/Rules/Operators/InvalidIncDecOperationRule.php +++ b/src/Rules/Operators/InvalidIncDecOperationRule.php @@ -132,6 +132,10 @@ public function processNode(Node $node, Scope $scope): array $deprecatedBool = true; } + if ($this->phpVersion->supportsBcMathNumberOperatorOverloading()) { + $allowedTypes[] = new ObjectType('BcMath\Number'); + } + $allowedTypes = new UnionType($allowedTypes); $varType = $this->ruleLevelHelper->findTypeToCheck( diff --git a/tests/PHPStan/Analyser/nsrt/bcmath-number.php b/tests/PHPStan/Analyser/nsrt/bcmath-number.php index 2ead45175f..d78887e4ef 100644 --- a/tests/PHPStan/Analyser/nsrt/bcmath-number.php +++ b/tests/PHPStan/Analyser/nsrt/bcmath-number.php @@ -405,4 +405,12 @@ public function bcVsNever(Number $a): void assertType('bool', $a or $b); } } + + public function bcIncDec(Number $a): void + { + assertType('BcMath\Number', ++$a); + assertType('BcMath\Number', $a++); + assertType('BcMath\Number', --$a); + assertType('BcMath\Number', $a--); + } } diff --git a/tests/PHPStan/Rules/Operators/InvalidIncDecOperationRuleTest.php b/tests/PHPStan/Rules/Operators/InvalidIncDecOperationRuleTest.php index 9243a766a3..52ae098afa 100644 --- a/tests/PHPStan/Rules/Operators/InvalidIncDecOperationRuleTest.php +++ b/tests/PHPStan/Rules/Operators/InvalidIncDecOperationRuleTest.php @@ -234,4 +234,10 @@ public function testIncNonNumericString(): void $this->analyse([__DIR__ . '/data/inc-non-numeric-string.php'], $errors); } + #[RequiresPhp('>= 8.4')] + public function testBcMathNumber(): void + { + $this->analyse([__DIR__ . '/data/inc-dec-bcmath-number.php'], []); + } + } diff --git a/tests/PHPStan/Rules/Operators/data/inc-dec-bcmath-number.php b/tests/PHPStan/Rules/Operators/data/inc-dec-bcmath-number.php new file mode 100644 index 0000000000..ea88d7c359 --- /dev/null +++ b/tests/PHPStan/Rules/Operators/data/inc-dec-bcmath-number.php @@ -0,0 +1,21 @@ +