Skip to content

Commit ce82a05

Browse files
committed
Operands in arithmetic operations - allow numeric-string
1 parent 63cec25 commit ce82a05

10 files changed

+8
-64
lines changed

src/Rules/Operators/OperatorRuleHelper.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
55
use PhpParser\Node\Expr;
66
use PHPStan\Analyser\Scope;
77
use PHPStan\Rules\RuleLevelHelper;
8+
use PHPStan\Type\Accessory\AccessoryNumericStringType;
89
use PHPStan\Type\ErrorType;
910
use PHPStan\Type\FloatType;
1011
use PHPStan\Type\IntegerType;
12+
use PHPStan\Type\IntersectionType;
1113
use PHPStan\Type\MixedType;
14+
use PHPStan\Type\StringType;
1215
use PHPStan\Type\Type;
1316
use PHPStan\Type\UnionType;
1417

@@ -50,7 +53,7 @@ public function isValidForIncrementOrDecrement(Scope $scope, Expr $expr): bool
5053

5154
private function isSubtypeOfNumber(Scope $scope, Expr $expr): bool
5255
{
53-
$acceptedType = new UnionType([new IntegerType(), new FloatType()]);
56+
$acceptedType = new UnionType([new IntegerType(), new FloatType(), new IntersectionType([new StringType(), new AccessoryNumericStringType()])]);
5457

5558
$type = $this->ruleLevelHelper->findTypeToCheck(
5659
$scope,

tests/Levels/data/arithmeticOperators-2.json

-10
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,9 @@
44
"line": 26,
55
"ignorable": true
66
},
7-
{
8-
"message": "Only numeric types are allowed in +, string given on the left side.",
9-
"line": 27,
10-
"ignorable": true
11-
},
127
{
138
"message": "Binary operation \"+\" between stdClass|string and int results in an error.",
149
"line": 36,
1510
"ignorable": true
16-
},
17-
{
18-
"message": "Only numeric types are allowed in +, string given on the left side.",
19-
"line": 43,
20-
"ignorable": true
2111
}
2212
]

tests/Levels/data/arithmeticOperators-7.json

-5
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,5 @@
33
"message": "Binary operation \"+\" between int|string and int results in an error.",
44
"line": 28,
55
"ignorable": true
6-
},
7-
{
8-
"message": "Only numeric types are allowed in +, int|string given on the left side.",
9-
"line": 29,
10-
"ignorable": true
116
}
127
]

tests/Rules/Operators/OperandsInArithmeticAdditionRuleTest.php

-8
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@ protected function getRule(): Rule
2121
public function testRule(): void
2222
{
2323
$messages = [
24-
[
25-
'Only numeric types are allowed in +, string given on the right side.',
26-
25,
27-
],
2824
[
2925
'Only numeric types are allowed in +, null given on the right side.',
3026
28,
@@ -33,10 +29,6 @@ public function testRule(): void
3329
'Only numeric types are allowed in +, null given on the right side.',
3430
29,
3531
],
36-
[
37-
'Only numeric types are allowed in +, string given on the right side.',
38-
29,
39-
],
4032
];
4133
if (PHP_VERSION_ID < 80000) {
4234
$messages[] = [

tests/Rules/Operators/OperandsInArithmeticDivisionRuleTest.php

-8
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@ protected function getRule(): Rule
2020
public function testRule(): void
2121
{
2222
$this->analyse([__DIR__ . '/data/operators.php'], [
23-
[
24-
'Only numeric types are allowed in /, string given on the right side.',
25-
64,
26-
],
2723
[
2824
'Only numeric types are allowed in /, null given on the right side.',
2925
67,
@@ -32,10 +28,6 @@ public function testRule(): void
3228
'Only numeric types are allowed in /, null given on the right side.',
3329
68,
3430
],
35-
[
36-
'Only numeric types are allowed in /, string given on the right side.',
37-
68,
38-
],
3931
]);
4032
}
4133

tests/Rules/Operators/OperandsInArithmeticExponentiationRuleTest.php

-8
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,10 @@ protected function getRule(): Rule
2020
public function testRule(): void
2121
{
2222
$this->analyse([__DIR__ . '/data/operators.php'], [
23-
[
24-
'Only numeric types are allowed in **, string given on the right side.',
25-
77,
26-
],
2723
[
2824
'Only numeric types are allowed in **, null given on the right side.',
2925
80,
3026
],
31-
[
32-
'Only numeric types are allowed in **, string given on the left side.',
33-
81,
34-
],
3527
[
3628
'Only numeric types are allowed in **, null given on the right side.',
3729
81,

tests/Rules/Operators/OperandsInArithmeticModuloRuleTest.php

-8
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@ protected function getRule(): Rule
2020
public function testRule(): void
2121
{
2222
$this->analyse([__DIR__ . '/data/operators.php'], [
23-
[
24-
'Only numeric types are allowed in %, string given on the right side.',
25-
90,
26-
],
2723
[
2824
'Only numeric types are allowed in %, null given on the right side.',
2925
93,
@@ -32,10 +28,6 @@ public function testRule(): void
3228
'Only numeric types are allowed in %, null given on the right side.',
3329
94,
3430
],
35-
[
36-
'Only numeric types are allowed in %, string given on the right side.',
37-
94,
38-
],
3931
]);
4032
}
4133

tests/Rules/Operators/OperandsInArithmeticMultiplicationRuleTest.php

-8
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@ protected function getRule(): Rule
2020
public function testRule(): void
2121
{
2222
$this->analyse([__DIR__ . '/data/operators.php'], [
23-
[
24-
'Only numeric types are allowed in *, string given on the right side.',
25-
51,
26-
],
2723
[
2824
'Only numeric types are allowed in *, null given on the right side.',
2925
54,
@@ -32,10 +28,6 @@ public function testRule(): void
3228
'Only numeric types are allowed in *, null given on the right side.',
3329
55,
3430
],
35-
[
36-
'Only numeric types are allowed in *, string given on the right side.',
37-
55,
38-
],
3931
]);
4032
}
4133

tests/Rules/Operators/OperandsInArithmeticSubtractionRuleTest.php

-8
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@ protected function getRule(): Rule
2020
public function testRule(): void
2121
{
2222
$this->analyse([__DIR__ . '/data/operators.php'], [
23-
[
24-
'Only numeric types are allowed in -, string given on the right side.',
25-
38,
26-
],
2723
[
2824
'Only numeric types are allowed in -, null given on the right side.',
2925
41,
@@ -32,10 +28,6 @@ public function testRule(): void
3228
'Only numeric types are allowed in -, null given on the right side.',
3329
42,
3430
],
35-
[
36-
'Only numeric types are allowed in -, string given on the right side.',
37-
42,
38-
],
3931
]);
4032
}
4133

tests/Rules/Operators/data/operators.php

+4
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,7 @@ function (array $array, int $int, $mixed) {
109109

110110
explode($mixed, $mixed) + $int;
111111
};
112+
113+
/** @var numeric-string $numericString */
114+
$numericString = doFoo();
115+
$numericString + 1;

0 commit comments

Comments
 (0)