Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,11 @@ class EnforceReadonlyPublicPropertyRule {
```

### forbidArithmeticOperationOnNonNumber
- Disallows using [arithmetic operators](https://www.php.net/manual/en/language.operators.arithmetic.php) with non-numeric types (only float and int is allowed)
- Disallows using [arithmetic operators](https://www.php.net/manual/en/language.operators.arithmetic.php) with non-numeric types (only `float`, `int` and `BcMath\Number` is allowed)
- You can allow numeric-string by using `allowNumericString: true` configuration
- Modulo operator (`%`) allows only integers as it [emits deprecation otherwise](https://3v4l.org/VpVoq)
- Plus operator is allowed for merging arrays
- `float` and `BcMath\Number` cannot be combined as it emits deprecations

```php
function add(string $a, string $b) {
Expand Down
41 changes: 35 additions & 6 deletions src/Rule/ForbidArithmeticOperationOnNonNumberRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\FloatType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeTraverser;
use PHPStan\Type\UnionType;
use PHPStan\Type\VerbosityLevel;
use function sprintf;
Expand Down Expand Up @@ -113,6 +115,12 @@ private function processBinary(
return []; // array merge syntax
}

if (($this->containsBcMathNumber($leftType) && $this->isFloat($rightType))
|| ($this->isFloat($leftType) && $this->containsBcMathNumber($rightType))
) {
return $this->buildBinaryErrors($operator, 'BcMath\\Number and float', $leftType, $rightType);
}

if (
$operator === '%' &&
(!$leftType->isInteger()->yes() || !$rightType->isInteger()->yes())
Expand All @@ -129,13 +137,13 @@ private function processBinary(

private function isNumeric(Type $type): bool
{
$int = new IntegerType();
$float = new FloatType();
$intOrFloat = new UnionType([$int, $float]);
$numericUnion = new UnionType([
new IntegerType(),
new FloatType(),
new ObjectType('BcMath\Number'),
]);

return $int->isSuperTypeOf($type)->yes()
|| $float->isSuperTypeOf($type)->yes()
|| $intOrFloat->isSuperTypeOf($type)->yes()
return $numericUnion->isSuperTypeOf($type)->yes()
|| ($this->allowNumericString && $type->isNumericString()->yes());
}

Expand Down Expand Up @@ -164,4 +172,25 @@ private function buildBinaryErrors(
return [$error];
}

private function isFloat(Type $type): bool
{
$float = new FloatType();

return $type->isSuperTypeOf($float)->yes();
}

private function containsBcMathNumber(Type $type): bool
{
$bcMathFound = false;
$bcMathNumber = new ObjectType('BcMath\Number');

TypeTraverser::map($type, static function (Type $traversedTyped, callable $traverse) use (&$bcMathFound, $bcMathNumber): Type {
if ($bcMathNumber->isSuperTypeOf($traversedTyped)->yes()) {
$bcMathFound = true;
}
return $traverse($traversedTyped);
});
return $bcMathFound;
}

}
12 changes: 12 additions & 0 deletions tests/Rule/ForbidArithmeticOperationOnNonNumberRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ public function testNoNumericString(): void
$this->analyseFile(__DIR__ . '/data/ForbidArithmeticOperationOnNonNumberRule/no-numeric-string.php');
}

public function testBcMathNumber(): void
{
$this->allowNumericString = true;
$this->analyseFile(__DIR__ . '/data/ForbidArithmeticOperationOnNonNumberRule/bcmath-number.php');
}

public function testBcMathNumberNoNumeric(): void
{
$this->allowNumericString = false;
$this->analyseFile(__DIR__ . '/data/ForbidArithmeticOperationOnNonNumberRule/bcmath-number-no-numeric.php');
}

protected function shouldFailOnPhpErrors(): bool
{
return false; // https://github.com/phpstan/phpstan-src/pull/3031
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php declare(strict_types = 1);

namespace ShipMonk\PHPStan\Rule\data\ForbidArithmeticOperationOnNonNumberRule;
use BcMath\Number;

class BcMathNumberNoNumeric {

/**
* @param numeric-string $ns
*/
public function testBcMathNumbers(Number $n, string $ns, int $int, float $float, int|float $intFloat): void {
+$n;
-$n;

$x = $n + $n;
$x = $n - $n;
$x = $n * $n;
$x = $n / $n;

$x = $n + $int;
$x = $int + $n;
$x = $n + $ns; // error: Using + over non-number (BcMath\Number + string)
$x = $ns + $n; // error: Using + over non-number (string + BcMath\Number)
$x = $n - $int;
$x = $int - $n;
$x = $n - $ns; // error: Using - over non-number (BcMath\Number - string)
$x = $ns - $n; // error: Using - over non-number (string - BcMath\Number)
$x = $n * $int;
$x = $int * $n;
$x = $n * $ns; // error: Using * over non-number (BcMath\Number * string)
$x = $ns * $n; // error: Using * over non-number (string * BcMath\Number)
$x = $n / $int;
$x = $int / $n;
$x = $n / $ns; // error: Using / over non-number (BcMath\Number / string)
$x = $ns / $n; // error: Using / over non-number (string / BcMath\Number)
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
<?php declare(strict_types = 1);

namespace ForbidArithmeticOperationOnNonNumberRule;

use BcMath\Number;

class BcMathNumber {

/**
* @param numeric-string $ns
*/
public function testBcMathNumbers(
object $object,
Number $number,
string $ns,
int $int,
float $float,
bool|float $boolFloat,
int|float $intFloat,
Number|int $nInt,
Number|float $nFloat,
Number|int|float $nIntFloat,
Number|int|float|bool $nIntFloatBool
): void {
+$number;
-$number;

$x = $object + $float; // error: Using + over non-number (object + float)
$x = $object + $number; // error: Using + over non-number (object + BcMath\Number)

$x = $number + $number;
$x = $number - $number;
$x = $number * $number;
$x = $number / $number;

$x = $number / $boolFloat; // error: Using / over BcMath\Number and float (BcMath\Number / bool|float)

$x = $number + $int;
$x = $int + $number;
$x = $number + $ns;
$x = $ns + $number;
$x = $number - $int;
$x = $int - $number;
$x = $number - $ns;
$x = $ns - $number;
$x = $number * $int;
$x = $int * $number;
$x = $number * $ns;
$x = $ns * $number;
$x = $number / $int;
$x = $int / $number;
$x = $number / $ns;
$x = $ns / $number;

$x = $nInt + $int;
$x = $int + $nInt;
$x = $nInt + $ns;
$x = $ns + $nInt;
$x = $nInt - $int;
$x = $int - $nInt;
$x = $nInt - $ns;
$x = $ns - $nInt;
$x = $nInt * $int;
$x = $int * $nInt;
$x = $nInt * $ns;
$x = $ns * $nInt;
$x = $nInt / $int;
$x = $int / $nInt;
$x = $nInt / $ns;
$x = $ns / $nInt;

$x = $nFloat + $int;
$x = $int + $nFloat;
$x = $nFloat + $ns;
$x = $ns + $nFloat;
$x = $nFloat - $int;
$x = $int - $nFloat;
$x = $nFloat - $ns;
$x = $ns - $nFloat;
$x = $nFloat * $int;
$x = $int * $nFloat;
$x = $nFloat * $ns;
$x = $ns * $nFloat;
$x = $nFloat / $int;
$x = $int / $nFloat;
$x = $nFloat / $ns;
$x = $ns / $nFloat;

$x = $nIntFloat + $int;
$x = $int + $nIntFloat;
$x = $nIntFloat + $ns;
$x = $ns + $nIntFloat;
$x = $nIntFloat - $int;
$x = $int - $nIntFloat;
$x = $nIntFloat - $ns;
$x = $ns - $nIntFloat;
$x = $nIntFloat * $int;
$x = $int * $nIntFloat;
$x = $nIntFloat * $ns;
$x = $ns * $nIntFloat;
$x = $nIntFloat / $int;
$x = $int / $nIntFloat;
$x = $nIntFloat / $ns;
$x = $ns / $nIntFloat;

$x = $number + $float; // error: Using + over BcMath\Number and float (BcMath\Number + float)
$x = $float + $number; // error: Using + over BcMath\Number and float (float + BcMath\Number)
$x = $number - $float; // error: Using - over BcMath\Number and float (BcMath\Number - float)
$x = $float - $number; // error: Using - over BcMath\Number and float (float - BcMath\Number)
$x = $number * $float; // error: Using * over BcMath\Number and float (BcMath\Number * float)
$x = $float * $number; // error: Using * over BcMath\Number and float (float * BcMath\Number)
$x = $number / $float; // error: Using / over BcMath\Number and float (BcMath\Number / float)
$x = $float / $number; // error: Using / over BcMath\Number and float (float / BcMath\Number)
$x = $number % $float; // error: Using % over BcMath\Number and float (BcMath\Number % float)
$x = $float % $number; // error: Using % over BcMath\Number and float (float % BcMath\Number)
$x = $number ** $float; // error: Using ** over BcMath\Number and float (BcMath\Number ** float)
$x = $float ** $number; // error: Using ** over BcMath\Number and float (float ** BcMath\Number)
$x = $number + $intFloat; // error: Using + over BcMath\Number and float (BcMath\Number + float|int)
$x = $intFloat + $number; // error: Using + over BcMath\Number and float (float|int + BcMath\Number)

$x = $nInt + $float; // error: Using + over BcMath\Number and float (BcMath\Number|int + float)
$x = $float + $nInt; // error: Using + over BcMath\Number and float (float + BcMath\Number|int)
$x = $nInt - $float; // error: Using - over BcMath\Number and float (BcMath\Number|int - float)
$x = $float - $nInt; // error: Using - over BcMath\Number and float (float - BcMath\Number|int)
$x = $nInt * $float; // error: Using * over BcMath\Number and float (BcMath\Number|int * float)
$x = $float * $nInt; // error: Using * over BcMath\Number and float (float * BcMath\Number|int)
$x = $nInt / $float; // error: Using / over BcMath\Number and float (BcMath\Number|int / float)
$x = $float / $nInt; // error: Using / over BcMath\Number and float (float / BcMath\Number|int)
$x = $nInt % $float; // error: Using % over BcMath\Number and float (BcMath\Number|int % float)
$x = $float % $nInt; // error: Using % over BcMath\Number and float (float % BcMath\Number|int)
$x = $nInt ** $float; // error: Using ** over BcMath\Number and float (BcMath\Number|int ** float)
$x = $float ** $nInt; // error: Using ** over BcMath\Number and float (float ** BcMath\Number|int)
$x = $nInt + $intFloat; // error: Using + over BcMath\Number and float (BcMath\Number|int + float|int)
$x = $intFloat + $nInt; // error: Using + over BcMath\Number and float (float|int + BcMath\Number|int)

$x = $nFloat + $float; // error: Using + over BcMath\Number and float (BcMath\Number|float + float)
$x = $float + $nFloat; // error: Using + over BcMath\Number and float (float + BcMath\Number|float)
$x = $nFloat - $float; // error: Using - over BcMath\Number and float (BcMath\Number|float - float)
$x = $float - $nFloat; // error: Using - over BcMath\Number and float (float - BcMath\Number|float)
$x = $nFloat * $float; // error: Using * over BcMath\Number and float (BcMath\Number|float * float)
$x = $float * $nFloat; // error: Using * over BcMath\Number and float (float * BcMath\Number|float)
$x = $nFloat / $float; // error: Using / over BcMath\Number and float (BcMath\Number|float / float)
$x = $float / $nFloat; // error: Using / over BcMath\Number and float (float / BcMath\Number|float)
$x = $nFloat % $float; // error: Using % over BcMath\Number and float (BcMath\Number|float % float)
$x = $float % $nFloat; // error: Using % over BcMath\Number and float (float % BcMath\Number|float)
$x = $nFloat ** $float; // error: Using ** over BcMath\Number and float (BcMath\Number|float ** float)
$x = $float ** $nFloat; // error: Using ** over BcMath\Number and float (float ** BcMath\Number|float)
$x = $nFloat + $intFloat; // error: Using + over BcMath\Number and float (BcMath\Number|float + float|int)
$x = $intFloat + $nFloat; // error: Using + over BcMath\Number and float (float|int + BcMath\Number|float)

$x = $nIntFloat + $float; // error: Using + over BcMath\Number and float (BcMath\Number|float|int + float)
$x = $float + $nIntFloat; // error: Using + over BcMath\Number and float (float + BcMath\Number|float|int)
$x = $nIntFloat - $float; // error: Using - over BcMath\Number and float (BcMath\Number|float|int - float)
$x = $float - $nIntFloat; // error: Using - over BcMath\Number and float (float - BcMath\Number|float|int)
$x = $nIntFloat * $float; // error: Using * over BcMath\Number and float (BcMath\Number|float|int * float)
$x = $float * $nIntFloat; // error: Using * over BcMath\Number and float (float * BcMath\Number|float|int)
$x = $nIntFloat / $float; // error: Using / over BcMath\Number and float (BcMath\Number|float|int / float)
$x = $float / $nIntFloat; // error: Using / over BcMath\Number and float (float / BcMath\Number|float|int)
$x = $nIntFloat % $float; // error: Using % over BcMath\Number and float (BcMath\Number|float|int % float)
$x = $float % $nIntFloat; // error: Using % over BcMath\Number and float (float % BcMath\Number|float|int)
$x = $nIntFloat ** $float; // error: Using ** over BcMath\Number and float (BcMath\Number|float|int ** float)
$x = $float ** $nIntFloat; // error: Using ** over BcMath\Number and float (float ** BcMath\Number|float|int)
$x = $nIntFloat + $intFloat; // error: Using + over BcMath\Number and float (BcMath\Number|float|int + float|int)
$x = $intFloat + $nIntFloat; // error: Using + over BcMath\Number and float (float|int + BcMath\Number|float|int)

$x = $nIntFloatBool + $float; // error: Using + over BcMath\Number and float (BcMath\Number|bool|float|int + float)
$x = $float + $nIntFloatBool; // error: Using + over BcMath\Number and float (float + BcMath\Number|bool|float|int)
$x = $nIntFloatBool - $float; // error: Using - over BcMath\Number and float (BcMath\Number|bool|float|int - float)
$x = $float - $nIntFloatBool; // error: Using - over BcMath\Number and float (float - BcMath\Number|bool|float|int)
$x = $nIntFloatBool * $float; // error: Using * over BcMath\Number and float (BcMath\Number|bool|float|int * float)
$x = $float * $nIntFloatBool; // error: Using * over BcMath\Number and float (float * BcMath\Number|bool|float|int)
$x = $nIntFloatBool / $float; // error: Using / over BcMath\Number and float (BcMath\Number|bool|float|int / float)
$x = $float / $nIntFloatBool; // error: Using / over BcMath\Number and float (float / BcMath\Number|bool|float|int)
$x = $nIntFloatBool % $float; // error: Using % over BcMath\Number and float (BcMath\Number|bool|float|int % float)
$x = $float % $nIntFloatBool; // error: Using % over BcMath\Number and float (float % BcMath\Number|bool|float|int)
$x = $nIntFloatBool ** $float; // error: Using ** over BcMath\Number and float (BcMath\Number|bool|float|int ** float)
$x = $float ** $nIntFloatBool; // error: Using ** over BcMath\Number and float (float ** BcMath\Number|bool|float|int)
$x = $nIntFloatBool + $intFloat; // error: Using + over BcMath\Number and float (BcMath\Number|bool|float|int + float|int)
$x = $intFloat + $nIntFloatBool; // error: Using + over BcMath\Number and float (float|int + BcMath\Number|bool|float|int)
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ public function testUnions(
-$intFloat;
-$intFloatString; // error: Using - over non-number (float|int|string)
-$intArray; // error: Using - over non-number (array|int)

$intString - $intArray; // error: Using - over non-number (int|string - array|int)
}

/**
Expand Down