Skip to content

Commit

Permalink
Operands in arithmetic operations - allow numeric-string
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jul 4, 2021
1 parent 63cec25 commit ce82a05
Show file tree
Hide file tree
Showing 10 changed files with 8 additions and 64 deletions.
5 changes: 4 additions & 1 deletion src/Rules/Operators/OperatorRuleHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
use PhpParser\Node\Expr;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\RuleLevelHelper;
use PHPStan\Type\Accessory\AccessoryNumericStringType;
use PHPStan\Type\ErrorType;
use PHPStan\Type\FloatType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\MixedType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;

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

private function isSubtypeOfNumber(Scope $scope, Expr $expr): bool
{
$acceptedType = new UnionType([new IntegerType(), new FloatType()]);
$acceptedType = new UnionType([new IntegerType(), new FloatType(), new IntersectionType([new StringType(), new AccessoryNumericStringType()])]);

$type = $this->ruleLevelHelper->findTypeToCheck(
$scope,
Expand Down
10 changes: 0 additions & 10 deletions tests/Levels/data/arithmeticOperators-2.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,9 @@
"line": 26,
"ignorable": true
},
{
"message": "Only numeric types are allowed in +, string given on the left side.",
"line": 27,
"ignorable": true
},
{
"message": "Binary operation \"+\" between stdClass|string and int results in an error.",
"line": 36,
"ignorable": true
},
{
"message": "Only numeric types are allowed in +, string given on the left side.",
"line": 43,
"ignorable": true
}
]
5 changes: 0 additions & 5 deletions tests/Levels/data/arithmeticOperators-7.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,5 @@
"message": "Binary operation \"+\" between int|string and int results in an error.",
"line": 28,
"ignorable": true
},
{
"message": "Only numeric types are allowed in +, int|string given on the left side.",
"line": 29,
"ignorable": true
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ protected function getRule(): Rule
public function testRule(): void
{
$messages = [
[
'Only numeric types are allowed in +, string given on the right side.',
25,
],
[
'Only numeric types are allowed in +, null given on the right side.',
28,
Expand All @@ -33,10 +29,6 @@ public function testRule(): void
'Only numeric types are allowed in +, null given on the right side.',
29,
],
[
'Only numeric types are allowed in +, string given on the right side.',
29,
],
];
if (PHP_VERSION_ID < 80000) {
$messages[] = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ protected function getRule(): Rule
public function testRule(): void
{
$this->analyse([__DIR__ . '/data/operators.php'], [
[
'Only numeric types are allowed in /, string given on the right side.',
64,
],
[
'Only numeric types are allowed in /, null given on the right side.',
67,
Expand All @@ -32,10 +28,6 @@ public function testRule(): void
'Only numeric types are allowed in /, null given on the right side.',
68,
],
[
'Only numeric types are allowed in /, string given on the right side.',
68,
],
]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,10 @@ protected function getRule(): Rule
public function testRule(): void
{
$this->analyse([__DIR__ . '/data/operators.php'], [
[
'Only numeric types are allowed in **, string given on the right side.',
77,
],
[
'Only numeric types are allowed in **, null given on the right side.',
80,
],
[
'Only numeric types are allowed in **, string given on the left side.',
81,
],
[
'Only numeric types are allowed in **, null given on the right side.',
81,
Expand Down
8 changes: 0 additions & 8 deletions tests/Rules/Operators/OperandsInArithmeticModuloRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ protected function getRule(): Rule
public function testRule(): void
{
$this->analyse([__DIR__ . '/data/operators.php'], [
[
'Only numeric types are allowed in %, string given on the right side.',
90,
],
[
'Only numeric types are allowed in %, null given on the right side.',
93,
Expand All @@ -32,10 +28,6 @@ public function testRule(): void
'Only numeric types are allowed in %, null given on the right side.',
94,
],
[
'Only numeric types are allowed in %, string given on the right side.',
94,
],
]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ protected function getRule(): Rule
public function testRule(): void
{
$this->analyse([__DIR__ . '/data/operators.php'], [
[
'Only numeric types are allowed in *, string given on the right side.',
51,
],
[
'Only numeric types are allowed in *, null given on the right side.',
54,
Expand All @@ -32,10 +28,6 @@ public function testRule(): void
'Only numeric types are allowed in *, null given on the right side.',
55,
],
[
'Only numeric types are allowed in *, string given on the right side.',
55,
],
]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ protected function getRule(): Rule
public function testRule(): void
{
$this->analyse([__DIR__ . '/data/operators.php'], [
[
'Only numeric types are allowed in -, string given on the right side.',
38,
],
[
'Only numeric types are allowed in -, null given on the right side.',
41,
Expand All @@ -32,10 +28,6 @@ public function testRule(): void
'Only numeric types are allowed in -, null given on the right side.',
42,
],
[
'Only numeric types are allowed in -, string given on the right side.',
42,
],
]);
}

Expand Down
4 changes: 4 additions & 0 deletions tests/Rules/Operators/data/operators.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,7 @@ function (array $array, int $int, $mixed) {

explode($mixed, $mixed) + $int;
};

/** @var numeric-string $numericString */
$numericString = doFoo();
$numericString + 1;

0 comments on commit ce82a05

Please sign in to comment.