Test Failed
Pull Request — master (#132)
by Jordan
18:05
created

MutableDecimal::setValue()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 26
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 6.1384

Importance

Changes 0
Metric Value
cc 5
eloc 13
nc 8
nop 4
dl 0
loc 26
ccs 9
cts 14
cp 0.6429
crap 6.1384
rs 9.5222
c 0
b 0
f 0
1
<?php
2
3
namespace Samsara\Fermat\Values;
4
5
use Samsara\Exceptions\SystemError\PlatformError\MissingPackage;
6
use Samsara\Exceptions\UsageError\IntegrityConstraint;
7
use Samsara\Fermat\Enums\NumberBase;
8
use Samsara\Fermat\Provider\BaseConversionProvider;
9
use Samsara\Fermat\Types\Base\Interfaces\Numbers\NumberInterface;
10
use Samsara\Fermat\Types\Decimal;
11
use Samsara\Fermat\Numbers;
12
use Samsara\Fermat\Types\Base\Interfaces\Numbers\DecimalInterface;
13
14
/**
15
 *
16
 */
17
class MutableDecimal extends Decimal
18
{
19
20
    /**
21
     * @throws IntegrityConstraint
22
     * @throws MissingPackage
23
     */
24 1
    public function continuousModulo(NumberInterface|string|int|float $mod): DecimalInterface
25
    {
26
27 1
        $mod = Numbers::makeOrDont(Numbers::IMMUTABLE, $mod, $this->scale+1);
28 1
        $oldNum = Numbers::make(Numbers::IMMUTABLE, $this->getValue(NumberBase::Ten), $this->scale+1);
29
30 1
        $multiple = $oldNum->divide($mod)->floor();
31
32 1
        $remainder = $oldNum->subtract($mod->multiply($multiple));
33
34 1
        return Numbers::make(Numbers::MUTABLE, $remainder->truncate($this->scale-1)->getValue(NumberBase::Ten), $this->scale-1, $this->getBase());
35
36
    }
37
38
    /**
39
     * @param string $value
40
     * @param int|null $scale
41
     * @param NumberBase|null $base
42
     * @param bool $setToNewBase
43
     * @return MutableDecimal
44
     */
45 10
    protected function setValue(string $value, ?int $scale = null, ?NumberBase $base = null, bool $setToNewBase = false): self
46
    {
47 10
        $imaginary = false;
48
49 10
        if (str_contains($value, 'i')) {
50
            $value = str_replace('i', '', $value);
51
            $imaginary = true;
52
        }
53
54 10
        if (!is_null($base) && $base != NumberBase::Ten) {
55
            $value = BaseConversionProvider::convertStringToBaseTen($value, $base);
56
        }
57
58 10
        $this->imaginary = $imaginary;
59
60
        if ($setToNewBase) {
61
            $this->base = $base ?? $this->getBase();
62 10
        }
63 9
64
        $this->value = $this->translateValue($value);
65
66 10
        $scale = $scale ?? $this->getScale();
67
68
        $this->scale = $this->determineScale($this->getDecimalPart(), $scale);
69
70 10
        return $this;
71
    }
72
73
}