Skip to content
Open
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
24 changes: 23 additions & 1 deletion src/Rule/ForbidArithmeticOperationOnNonNumberRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\FloatType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
use PHPStan\Type\VerbosityLevel;
Expand Down Expand Up @@ -113,6 +114,12 @@ private function processBinary(
return []; // array merge syntax
}

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

if (
$operator === '%' &&
(!$leftType->isInteger()->yes() || !$rightType->isInteger()->yes())
Expand All @@ -136,7 +143,8 @@ private function isNumeric(Type $type): bool
return $int->isSuperTypeOf($type)->yes()
|| $float->isSuperTypeOf($type)->yes()
|| $intOrFloat->isSuperTypeOf($type)->yes()
|| ($this->allowNumericString && $type->isNumericString()->yes());
|| ($this->allowNumericString && $type->isNumericString()->yes())
|| $this->isBcMathNumber($type);
}

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

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

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

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

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

}
21 changes: 21 additions & 0 deletions tests/Rule/ForbidArithmeticOperationOnNonNumberRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use LogicException;
use PHPStan\Rules\Rule;
use ShipMonk\PHPStan\RuleTestCase;
use const PHP_VERSION_ID;

/**
* @extends RuleTestCase<ForbidArithmeticOperationOnNonNumberRule>
Expand Down Expand Up @@ -35,6 +36,26 @@ public function testNoNumericString(): void
$this->analyseFile(__DIR__ . '/data/ForbidArithmeticOperationOnNonNumberRule/no-numeric-string.php');
}

public function testBcMathNumber(): void
{
if (PHP_VERSION_ID < 80_400) {
self::markTestSkipped('Requires PHP 8.4');
}

$this->allowNumericString = true;
$this->analyseFile(__DIR__ . '/data/ForbidArithmeticOperationOnNonNumberRule/bcmath-number.php');
}

public function testBcMathNumberNoNumeric(): void
{
if (PHP_VERSION_ID < 80_400) {
self::markTestSkipped('Requires PHP 8.4');
}

$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,54 @@
<?php declare(strict_types = 1);

namespace ForbidArithmeticOperationOnNonNumberRule;
use BcMath\Number;

class BcMathNumber {

/**
* @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;
$x = $ns + $n;
$x = $n - $int;
$x = $int - $n;
$x = $n - $ns;
$x = $ns - $n;
$x = $n * $int;
$x = $int * $n;
$x = $n * $ns;
$x = $ns * $n;
$x = $n / $int;
$x = $int / $n;
$x = $n / $ns;
$x = $ns / $n;

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

$x = $n + $intFloat; // error: Using + over BcMath\Number and float (BcMath\Number + float|int)
$x = $intFloat + $n; // error: Using + over BcMath\Number and float (float|int + BcMath\Number)
}
}

Loading