Skip to content

Commit 40b3f24

Browse files
authored
Fix BcMath\Number increment/decrement (#4957)
1 parent 0c2ab3b commit 40b3f24

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

src/Rules/Operators/InvalidIncDecOperationRule.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ public function processNode(Node $node, Scope $scope): array
132132
$deprecatedBool = true;
133133
}
134134

135+
if ($this->phpVersion->supportsBcMathNumberOperatorOverloading()) {
136+
$allowedTypes[] = new ObjectType('BcMath\Number');
137+
}
138+
135139
$allowedTypes = new UnionType($allowedTypes);
136140

137141
$varType = $this->ruleLevelHelper->findTypeToCheck(

tests/PHPStan/Analyser/nsrt/bcmath-number.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,4 +405,12 @@ public function bcVsNever(Number $a): void
405405
assertType('bool', $a or $b);
406406
}
407407
}
408+
409+
public function bcIncDec(Number $a): void
410+
{
411+
assertType('BcMath\Number', ++$a);
412+
assertType('BcMath\Number', $a++);
413+
assertType('BcMath\Number', --$a);
414+
assertType('BcMath\Number', $a--);
415+
}
408416
}

tests/PHPStan/Rules/Operators/InvalidIncDecOperationRuleTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,4 +234,10 @@ public function testIncNonNumericString(): void
234234
$this->analyse([__DIR__ . '/data/inc-non-numeric-string.php'], $errors);
235235
}
236236

237+
#[RequiresPhp('>= 8.4')]
238+
public function testBcMathNumber(): void
239+
{
240+
$this->analyse([__DIR__ . '/data/inc-dec-bcmath-number.php'], []);
241+
}
242+
237243
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace IncDecBcMathNumber;
4+
5+
use BcMath\Number;
6+
7+
function testPreInc(Number $x): void {
8+
++$x;
9+
}
10+
11+
function testPostInc(Number $x): void {
12+
$x++;
13+
}
14+
15+
function testPreDec(Number $x): void {
16+
--$x;
17+
}
18+
19+
function testPostDec(Number $x): void {
20+
$x--;
21+
}

0 commit comments

Comments
 (0)