From ef1290ae9d147021741beb0859522782d058ff93 Mon Sep 17 00:00:00 2001 From: vansari Date: Mon, 2 Jan 2023 11:10:03 +0100 Subject: [PATCH 1/8] Type Casts added --- src/Decimal.php | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/Decimal.php b/src/Decimal.php index 6d042e5..956bdf0 100644 --- a/src/Decimal.php +++ b/src/Decimal.php @@ -242,7 +242,7 @@ public function compareTo($value): int $decimal = static::create($value); $scale = max($this->scale(), $decimal->scale()); - return bccomp($this, $decimal, $scale); + return bccomp((string)$this, (string)$decimal, $scale); } /** @@ -258,7 +258,7 @@ public function add($value, ?int $scale = null) $decimal = static::create($value); $scale = $this->resultScale($this, $decimal, $scale); - return new static(bcadd($this, $decimal, $scale)); + return new static(bcadd((string)$this, (string)$decimal, $scale)); } /** @@ -296,7 +296,7 @@ public function subtract($value, ?int $scale = null) $decimal = static::create($value); $scale = $this->resultScale($this, $decimal, $scale); - return new static(bcsub($this, $decimal, $scale)); + return new static(bcsub((string)$this, (string)$decimal, $scale)); } /** @@ -406,7 +406,7 @@ public function multiply($value, ?int $scale = null) $scale = $this->scale() + $decimal->scale(); } - return new static(bcmul($this, $decimal, $scale)); + return new static(bcmul((string)$this, (string)$decimal, $scale)); } /** @@ -426,7 +426,7 @@ public function divide($value, int $scale) throw new DivisionByZeroError('Cannot divide by zero. Only Chuck Norris can!'); } - return new static(bcdiv($this, $decimal, $scale)); + return new static(bcdiv((string)$this, (string)$decimal, $scale)); } /** @@ -443,7 +443,7 @@ public function pow($exponent, ?int $scale = null) $scale = $this->scale(); } - return new static(bcpow($this, (string)$exponent, $scale)); + return new static(bcpow((string)$this, (string)$exponent, $scale)); } /** @@ -459,7 +459,7 @@ public function sqrt(?int $scale = null) $scale = $this->scale(); } - return new static(bcsqrt($this, $scale)); + return new static(bcsqrt((string)$this, $scale)); } /** @@ -476,10 +476,10 @@ public function mod($value, ?int $scale = null) $scale = $this->scale(); } if (version_compare(PHP_VERSION, '7.2') < 0) { - return new static(bcmod($this, (string)$value)); + return new static(bcmod((string)$this, (string)$value)); } - return new static(bcmod($this, (string)$value, $scale)); + return new static(bcmod((string)$this, (string)$value, $scale)); } /** @@ -495,16 +495,16 @@ public function round(int $scale = 0, int $roundMode = self::ROUND_HALF_UP) $e = bcpow('10', (string)$exponent); switch ($roundMode) { case static::ROUND_FLOOR: - $v = bcdiv(bcadd(bcmul($this, $e, 0), $this->isNegative() ? '-9' : '0'), $e, 0); + $v = bcdiv(bcadd(bcmul((string)$this, $e, 0), $this->isNegative() ? '-9' : '0'), $e, 0); break; case static::ROUND_CEIL: - $v = bcdiv(bcadd(bcmul($this, $e, 0), $this->isNegative() ? '0' : '9'), $e, 0); + $v = bcdiv(bcadd(bcmul((string)$this, $e, 0), $this->isNegative() ? '0' : '9'), $e, 0); break; case static::ROUND_HALF_UP: default: - $v = bcdiv(bcadd(bcmul($this, $e, 0), $this->isNegative() ? '-5' : '5'), $e, $scale); + $v = bcdiv(bcadd(bcmul((string)$this, $e, 0), $this->isNegative() ? '-5' : '5'), $e, $scale); } return new static($v); From 68cd78934947e160b2cfadd9a7e3c3b8636ebefc Mon Sep 17 00:00:00 2001 From: vansari Date: Mon, 2 Jan 2023 12:20:42 +0100 Subject: [PATCH 2/8] Fixing phpstan --- src/Decimal.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Decimal.php b/src/Decimal.php index 956bdf0..3edacd0 100644 --- a/src/Decimal.php +++ b/src/Decimal.php @@ -816,6 +816,9 @@ protected function fromScientific(string $value, ?int $scale): void $this->negative = $matches[1] === '-'; $value = preg_replace('/\b\.0$/', '', $matches[2]); + if (null === $value) { + throw new \RuntimeException('Unexpected value result.'); + } $exp = (int)$matches[3]; if ($exp < 0) { From c581314108b2610c9b74bc32247cb4695bd935ce Mon Sep 17 00:00:00 2001 From: vansari Date: Mon, 2 Jan 2023 12:25:45 +0100 Subject: [PATCH 3/8] Fixing phpcs --- src/Decimal.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Decimal.php b/src/Decimal.php index 3edacd0..db92048 100644 --- a/src/Decimal.php +++ b/src/Decimal.php @@ -10,6 +10,7 @@ use DivisionByZeroError; use InvalidArgumentException; use JsonSerializable; +use RuntimeException; use TypeError; class Decimal implements JsonSerializable @@ -802,6 +803,7 @@ protected function fromFloat(string $value): void * @param int|null $scale * * @throws \InvalidArgumentException + * @throws \RuntimeException * * @return void */ @@ -816,8 +818,8 @@ protected function fromScientific(string $value, ?int $scale): void $this->negative = $matches[1] === '-'; $value = preg_replace('/\b\.0$/', '', $matches[2]); - if (null === $value) { - throw new \RuntimeException('Unexpected value result.'); + if ($value === null) { + throw new RuntimeException('Unexpected value result.'); } $exp = (int)$matches[3]; From 783fdb008ba58f411bd445abd89972cbd3320588 Mon Sep 17 00:00:00 2001 From: vansari Date: Mon, 2 Jan 2023 12:29:31 +0100 Subject: [PATCH 4/8] Upgraded PHP Version which is required --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 0f49095..db1dc9e 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,7 @@ } ], "require": { - "php": ">=7.3", + "php": ">=8.0", "ext-bcmath": "*", "ext-json": "*" }, From b80fcc5ba9a5fe39b70be05822e3a7a2b0b7b388 Mon Sep 17 00:00:00 2001 From: vansari Date: Mon, 2 Jan 2023 12:32:24 +0100 Subject: [PATCH 5/8] Revert "Upgraded PHP Version which is required" This reverts commit 783fdb008ba58f411bd445abd89972cbd3320588. --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index db1dc9e..0f49095 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,7 @@ } ], "require": { - "php": ">=8.0", + "php": ">=7.3", "ext-bcmath": "*", "ext-json": "*" }, From 0f9dc9e08da6ee839f008f88c1598b1f728cb02a Mon Sep 17 00:00:00 2001 From: vansari Date: Sun, 8 Jan 2023 18:35:42 +0100 Subject: [PATCH 6/8] Could not coverage exception for wrong value in function fromScientific --- src/Decimal.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Decimal.php b/src/Decimal.php index db92048..aef1c86 100644 --- a/src/Decimal.php +++ b/src/Decimal.php @@ -818,9 +818,11 @@ protected function fromScientific(string $value, ?int $scale): void $this->negative = $matches[1] === '-'; $value = preg_replace('/\b\.0$/', '', $matches[2]); - if ($value === null) { + // @codeCoverageIgnoreStart + if (!is_string($value)) { throw new RuntimeException('Unexpected value result.'); } + // @codeCoverageIgnoreEnd $exp = (int)$matches[3]; if ($exp < 0) { From dbc3fc2715230c5e8b149caa26a26e523380aa4f Mon Sep 17 00:00:00 2001 From: vansari Date: Sat, 18 Feb 2023 20:52:56 +0100 Subject: [PATCH 7/8] Removed check, delete unnecessary type cast --- src/Decimal.php | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/Decimal.php b/src/Decimal.php index aef1c86..1505ab7 100644 --- a/src/Decimal.php +++ b/src/Decimal.php @@ -10,7 +10,6 @@ use DivisionByZeroError; use InvalidArgumentException; use JsonSerializable; -use RuntimeException; use TypeError; class Decimal implements JsonSerializable @@ -803,7 +802,6 @@ protected function fromFloat(string $value): void * @param int|null $scale * * @throws \InvalidArgumentException - * @throws \RuntimeException * * @return void */ @@ -817,12 +815,8 @@ protected function fromScientific(string $value, ?int $scale): void } $this->negative = $matches[1] === '-'; + /** @var string $value */ $value = preg_replace('/\b\.0$/', '', $matches[2]); - // @codeCoverageIgnoreStart - if (!is_string($value)) { - throw new RuntimeException('Unexpected value result.'); - } - // @codeCoverageIgnoreEnd $exp = (int)$matches[3]; if ($exp < 0) { @@ -837,7 +831,7 @@ protected function fromScientific(string $value, ?int $scale): void } else { $this->integralPart = bcmul($matches[2], bcpow('10', (string)$exp)); - $pos = strlen((string)$this->integralPart); + $pos = strlen($this->integralPart); if (strpos($value, '.') !== false) { $pos++; } From fc055599b5b6dbbe7e631e69f822d4e13a880c9e Mon Sep 17 00:00:00 2001 From: vansari Date: Sun, 5 Mar 2023 19:37:55 +0100 Subject: [PATCH 8/8] - remove unnecessary cast --- src/Decimal.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Decimal.php b/src/Decimal.php index 1505ab7..87ee833 100644 --- a/src/Decimal.php +++ b/src/Decimal.php @@ -838,7 +838,7 @@ protected function fromScientific(string $value, ?int $scale): void $this->fractionalPart = rtrim(substr($value, $pos), '.'); if ($scale !== null) { - $this->fractionalPart = str_pad($this->fractionalPart, $scale - strlen((string)$this->integralPart), '0'); + $this->fractionalPart = str_pad($this->fractionalPart, $scale - strlen($this->integralPart), '0'); } } }