Skip to content

Commit eded554

Browse files
committed
Fix BcMath\Number increment/decrement
1 parent 106fc93 commit eded554

4 files changed

Lines changed: 52 additions & 0 deletions

File tree

‎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: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,4 +234,31 @@ public function testIncNonNumericString(): void
234234
$this->analyse([__DIR__ . '/data/inc-non-numeric-string.php'], $errors);
235235
}
236236

237+
public function testBcMathNumber(): void
238+
{
239+
$errors = [];
240+
if (PHP_VERSION_ID < 80400) {
241+
$errors = [
242+
[
243+
'Cannot use ++ on BcMath\Number.',
244+
9,
245+
],
246+
[
247+
'Cannot use ++ on BcMath\Number.',
248+
10,
249+
],
250+
[
251+
'Cannot use -- on BcMath\Number.',
252+
11,
253+
],
254+
[
255+
'Cannot use -- on BcMath\Number.',
256+
12,
257+
],
258+
];
259+
}
260+
261+
$this->analyse([__DIR__ . '/data/inc-dec-bcmath-number.php'], $errors);
262+
}
263+
237264
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace IncDecBcMathNumber;
4+
5+
use BcMath\Number;
6+
7+
function test(Number $x): void
8+
{
9+
++$x;
10+
$x++;
11+
$x--;
12+
--$x;
13+
}

0 commit comments

Comments
 (0)