Skip to content

Commit 01b67d1

Browse files
committed
Allow BcMath\Number increment/decrement
1 parent 2945453 commit 01b67d1

14 files changed

+35
-26
lines changed

src/Rules/Operators/OperatorRuleHelper.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PhpParser\Node\Expr;
66
use PHPStan\Analyser\Scope;
7+
use PHPStan\Php\PhpVersion;
78
use PHPStan\Rules\RuleLevelHelper;
89
use PHPStan\Type\Accessory\AccessoryNumericStringType;
910
use PHPStan\Type\BenevolentUnionType;
@@ -12,6 +13,7 @@
1213
use PHPStan\Type\IntegerType;
1314
use PHPStan\Type\IntersectionType;
1415
use PHPStan\Type\MixedType;
16+
use PHPStan\Type\ObjectType;
1517
use PHPStan\Type\StringType;
1618
use PHPStan\Type\Type;
1719
use PHPStan\Type\UnionType;
@@ -21,9 +23,12 @@ class OperatorRuleHelper
2123

2224
private RuleLevelHelper $ruleLevelHelper;
2325

24-
public function __construct(RuleLevelHelper $ruleLevelHelper)
26+
private PhpVersion $phpVersion;
27+
28+
public function __construct(RuleLevelHelper $ruleLevelHelper, PhpVersion $phpVersion)
2529
{
2630
$this->ruleLevelHelper = $ruleLevelHelper;
31+
$this->phpVersion = $phpVersion;
2732
}
2833

2934
public function isValidForArithmeticOperation(Scope $scope, Expr $expr): bool
@@ -68,7 +73,13 @@ public function isValidForDecrement(Scope $scope, Expr $expr): bool
6873

6974
private function isSubtypeOfNumber(Scope $scope, Expr $expr): bool
7075
{
71-
$acceptedType = new UnionType([new IntegerType(), new FloatType(), new IntersectionType([new StringType(), new AccessoryNumericStringType()])]);
76+
$acceptedTypes = [new IntegerType(), new FloatType(), new IntersectionType([new StringType(), new AccessoryNumericStringType()])];
77+
78+
if ($this->phpVersion->supportsBcMathNumberOperatorOverloading()) {
79+
$acceptedTypes[] = new ObjectType('BcMath\Number');
80+
}
81+
82+
$acceptedType = new UnionType($acceptedTypes);
7283

7384
$type = $this->ruleLevelHelper->findTypeToCheck(
7485
$scope,

tests/Rules/Operators/OperandInArithmeticIncrementOrDecrementRuleTestCase.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace PHPStan\Rules\Operators;
44

5+
use PHPStan\Php\PhpVersion;
56
use PHPStan\Rules\Rule;
67
use PHPStan\Rules\RuleLevelHelper;
78
use PHPStan\Testing\RuleTestCase;
@@ -18,6 +19,7 @@ protected function getRule(): Rule
1819
return $this->createRule(
1920
new OperatorRuleHelper(
2021
self::getContainer()->getByType(RuleLevelHelper::class),
22+
self::getContainer()->getByType(PhpVersion::class),
2123
),
2224
);
2325
}

tests/Rules/Operators/OperandInArithmeticPostDecrementRuleTest.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,7 @@ protected function getExpectedErrors(): array
4949
*/
5050
protected function getExpectedErrorsWithBcMath(): array
5151
{
52-
return [
53-
[
54-
'Only numeric types are allowed in post-decrement, BcMath\Number given.',
55-
8,
56-
],
57-
];
52+
return [];
5853
}
5954

6055
}

tests/Rules/Operators/OperandInArithmeticPostIncrementRuleTest.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,7 @@ protected function getExpectedErrors(): array
4545
*/
4646
protected function getExpectedErrorsWithBcMath(): array
4747
{
48-
return [
49-
[
50-
'Only numeric types are allowed in post-increment, BcMath\Number given.',
51-
12,
52-
],
53-
];
48+
return [];
5449
}
5550

5651
}

tests/Rules/Operators/OperandInArithmeticPreDecrementRuleTest.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,7 @@ protected function getExpectedErrors(): array
4949
*/
5050
protected function getExpectedErrorsWithBcMath(): array
5151
{
52-
return [
53-
[
54-
'Only numeric types are allowed in pre-decrement, BcMath\Number given.',
55-
16,
56-
],
57-
];
52+
return [];
5853
}
5954

6055
}

tests/Rules/Operators/OperandInArithmeticPreIncrementRuleTest.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,7 @@ protected function getExpectedErrors(): array
4545
*/
4646
protected function getExpectedErrorsWithBcMath(): array
4747
{
48-
return [
49-
[
50-
'Only numeric types are allowed in pre-increment, BcMath\Number given.',
51-
20,
52-
],
53-
];
48+
return [];
5449
}
5550

5651
}

tests/Rules/Operators/OperandInArithmeticUnaryMinusRuleTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace PHPStan\Rules\Operators;
44

5+
use PHPStan\Php\PhpVersion;
56
use PHPStan\Rules\Rule;
67
use PHPStan\Rules\RuleLevelHelper;
78
use PHPStan\Testing\RuleTestCase;
@@ -17,6 +18,7 @@ protected function getRule(): Rule
1718
return new OperandInArithmeticUnaryMinusRule(
1819
new OperatorRuleHelper(
1920
self::getContainer()->getByType(RuleLevelHelper::class),
21+
self::getContainer()->getByType(PhpVersion::class),
2022
),
2123
);
2224
}

tests/Rules/Operators/OperandInArithmeticUnaryPlusRuleTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace PHPStan\Rules\Operators;
44

5+
use PHPStan\Php\PhpVersion;
56
use PHPStan\Rules\Rule;
67
use PHPStan\Rules\RuleLevelHelper;
78
use PHPStan\Testing\RuleTestCase;
@@ -17,6 +18,7 @@ protected function getRule(): Rule
1718
return new OperandInArithmeticUnaryPlusRule(
1819
new OperatorRuleHelper(
1920
self::getContainer()->getByType(RuleLevelHelper::class),
21+
self::getContainer()->getByType(PhpVersion::class),
2022
),
2123
);
2224
}

tests/Rules/Operators/OperandsInArithmeticAdditionRuleTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace PHPStan\Rules\Operators;
44

5+
use PHPStan\Php\PhpVersion;
56
use PHPStan\Rules\Rule;
67
use PHPStan\Rules\RuleLevelHelper;
78
use PHPStan\Testing\RuleTestCase;
@@ -19,6 +20,7 @@ protected function getRule(): Rule
1920
return new OperandsInArithmeticAdditionRule(
2021
new OperatorRuleHelper(
2122
self::getContainer()->getByType(RuleLevelHelper::class),
23+
self::getContainer()->getByType(PhpVersion::class),
2224
),
2325
);
2426
}

tests/Rules/Operators/OperandsInArithmeticDivisionRuleTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace PHPStan\Rules\Operators;
44

5+
use PHPStan\Php\PhpVersion;
56
use PHPStan\Rules\Rule;
67
use PHPStan\Rules\RuleLevelHelper;
78
use PHPStan\Testing\RuleTestCase;
@@ -17,6 +18,7 @@ protected function getRule(): Rule
1718
return new OperandsInArithmeticDivisionRule(
1819
new OperatorRuleHelper(
1920
self::getContainer()->getByType(RuleLevelHelper::class),
21+
self::getContainer()->getByType(PhpVersion::class),
2022
),
2123
);
2224
}

0 commit comments

Comments
 (0)