Skip to content

Commit 4ff9176

Browse files
committed
Fix up strict_types addition and allow any object for creation.
1 parent b4ea044 commit 4ff9176

File tree

2 files changed

+22
-18
lines changed

2 files changed

+22
-18
lines changed

src/Decimal.php

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
/**
46
* MIT License
57
* For full license information, please view the LICENSE file that was distributed with this source code.
@@ -59,10 +61,10 @@ class Decimal implements JsonSerializable, Stringable
5961
protected int $scale = 0;
6062

6163
/**
62-
* @param static|string|float|int $value
64+
* @param object|string|float|int $value
6365
* @param int|null $scale
6466
*/
65-
public function __construct(self|string|float|int $value, ?int $scale = null)
67+
public function __construct(object|string|float|int $value, ?int $scale = null)
6668
{
6769
$value = $this->parseValue($value);
6870
$value = $this->normalizeValue($value);
@@ -136,12 +138,12 @@ protected function normalizeValue(string $value): string
136138
* Otherwise, create a new Decimal instance from the given value and return
137139
* it.
138140
*
139-
* @param static|string|float|int $value
141+
* @param object|string|float|int $value
140142
* @param int|null $scale
141143
*
142144
* @return static
143145
*/
144-
public static function create(self|string|float|int $value, ?int $scale = null): static
146+
public static function create(object|string|float|int $value, ?int $scale = null): static
145147
{
146148
if ($scale === null && $value instanceof static) {
147149
return clone $value;
@@ -234,7 +236,7 @@ public function compareTo(self|string|float|int $value): int
234236
$decimal = static::create($value);
235237
$scale = max($this->scale(), $decimal->scale());
236238

237-
return bccomp($this, $decimal, $scale);
239+
return bccomp((string)$this, (string)$decimal, $scale);
238240
}
239241

240242
/**
@@ -250,7 +252,7 @@ public function add(self|string|float|int $value, ?int $scale = null): static
250252
$decimal = static::create($value);
251253
$scale = $this->resultScale($this, $decimal, $scale);
252254

253-
return new static(bcadd($this, $decimal, $scale));
255+
return new static(bcadd((string)$this, (string)$decimal, $scale));
254256
}
255257

256258
/**
@@ -284,7 +286,7 @@ public function subtract(self|string|float|int $value, ?int $scale = null): stat
284286
$decimal = static::create($value);
285287
$scale = $this->resultScale($this, $decimal, $scale);
286288

287-
return new static(bcsub($this, $decimal, $scale));
289+
return new static(bcsub((string)$this, (string)$decimal, $scale));
288290
}
289291

290292
/**
@@ -394,7 +396,7 @@ public function multiply(self|string|int|float $value, ?int $scale = null): stat
394396
$scale = $this->scale() + $decimal->scale();
395397
}
396398

397-
return new static(bcmul($this, $decimal, $scale));
399+
return new static(bcmul((string)$this, (string)$decimal, $scale));
398400
}
399401

400402
/**
@@ -414,7 +416,7 @@ public function divide(self|string|int|float $value, int $scale): static
414416
throw new DivisionByZeroError('Cannot divide by zero. Only Chuck Norris can!');
415417
}
416418

417-
return new static(bcdiv($this, $decimal, $scale));
419+
return new static(bcdiv((string)$this, (string)$decimal, $scale));
418420
}
419421

420422
/**
@@ -431,7 +433,7 @@ public function pow(self|string|int $exponent, ?int $scale = null): static
431433
$scale = $this->scale();
432434
}
433435

434-
return new static(bcpow($this, (string)$exponent, $scale));
436+
return new static(bcpow((string)$this, (string)$exponent, $scale));
435437
}
436438

437439
/**
@@ -447,7 +449,7 @@ public function sqrt(?int $scale = null): static
447449
$scale = $this->scale();
448450
}
449451

450-
return new static(bcsqrt($this, $scale));
452+
return new static(bcsqrt((string)$this, $scale));
451453
}
452454

453455
/**
@@ -464,7 +466,7 @@ public function mod(self|string|int $value, ?int $scale = null): static
464466
$scale = $this->scale();
465467
}
466468

467-
return new static(bcmod($this, (string)$value, $scale));
469+
return new static(bcmod((string)$this, (string)$value, $scale));
468470
}
469471

470472
/**
@@ -480,16 +482,16 @@ public function round(int $scale = 0, int $roundMode = self::ROUND_HALF_UP): sta
480482
$e = bcpow('10', (string)$exponent);
481483
switch ($roundMode) {
482484
case static::ROUND_FLOOR:
483-
$v = bcdiv(bcadd(bcmul($this, $e, 0), $this->isNegative() ? '-9' : '0'), $e, 0);
485+
$v = bcdiv(bcadd(bcmul((string)$this, $e, 0), $this->isNegative() ? '-9' : '0'), $e, 0);
484486

485487
break;
486488
case static::ROUND_CEIL:
487-
$v = bcdiv(bcadd(bcmul($this, $e, 0), $this->isNegative() ? '0' : '9'), $e, 0);
489+
$v = bcdiv(bcadd(bcmul((string)$this, $e, 0), $this->isNegative() ? '0' : '9'), $e, 0);
488490

489491
break;
490492
case static::ROUND_HALF_UP:
491493
default:
492-
$v = bcdiv(bcadd(bcmul($this, $e, 0), $this->isNegative() ? '-5' : '5'), $e, $scale);
494+
$v = bcdiv(bcadd(bcmul((string)$this, $e, 0), $this->isNegative() ? '-5' : '5'), $e, $scale);
493495
}
494496

495497
return new static($v);

tests/DecimalTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
/**
46
* MIT License
57
* For full license information, please view the LICENSE file that was distributed with this source code.
@@ -600,7 +602,7 @@ public function testRound(mixed $value, int $scale, string $expected): void
600602
*/
601603
protected function assertNativeRound(string $expected, mixed $value, int $scale, int $roundMode): void
602604
{
603-
$this->assertSame((new Decimal($expected))->trim()->toString(), (string)round($value, $scale, $roundMode));
605+
$this->assertSame((new Decimal($expected))->trim()->toString(), (string)round((float)$value, $scale, $roundMode));
604606
}
605607

606608
/**
@@ -649,7 +651,7 @@ public function testFloor(mixed $value, string $expected): void
649651
*/
650652
protected function assertNativeFloor(string $expected, mixed $value): void
651653
{
652-
$this->assertSame($expected, (string)floor($value));
654+
$this->assertSame($expected, (string)floor((float)$value));
653655
}
654656

655657
/**
@@ -696,7 +698,7 @@ public function testCeil(mixed $value, string $expected): void
696698
*/
697699
protected function assertNativeCeil(string $expected, mixed $value): void
698700
{
699-
$this->assertSame($expected, (string)ceil($value));
701+
$this->assertSame($expected, (string)ceil((float)$value));
700702
}
701703

702704
/**

0 commit comments

Comments
 (0)