Passed
Pull Request — dev (#56)
by Jordan
08:50
created

ImmutableDecimal::setValue()   A

Complexity

Conditions 6
Paths 12

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 11
c 0
b 0
f 0
nc 12
nop 3
dl 0
loc 21
ccs 12
cts 12
cp 1
crap 6
rs 9.2222
1
<?php
2
3
namespace Samsara\Fermat\Values;
4
5
use Samsara\Fermat\Numbers;
6
use Samsara\Fermat\Types\Decimal;
7
use Samsara\Fermat\Types\Base\Interfaces\Numbers\DecimalInterface;
8
9
class ImmutableDecimal extends Decimal
10
{
11
12 23
    public function continuousModulo($mod): DecimalInterface
13
    {
14
15 23
        if (is_object($mod) && method_exists($mod, 'getPrecision')) {
16 22
            $precision = ($this->getPrecision() < $mod->getPrecision()) ? $mod->getPrecision() : $this->getPrecision();
17
        } else {
18 11
            $precision = $this->getPrecision();
19
        }
20
21 23
        $oldPrecision = $this->precision;
22 23
        $newPrecision = $precision+1;
23
24 23
        $this->precision = $newPrecision;
25
26 23
        if (is_object($mod) && method_exists($mod, 'truncateToPrecision')) {
27 22
            $mod = $mod->truncateToPrecision($newPrecision);
28
        } else {
29 11
            $mod = Numbers::make(Numbers::IMMUTABLE, $mod, $newPrecision);
30
        }
31
32 23
        $multiple = $this->divide($mod)->floor();
33
34 23
        $remainder = $this->subtract($mod->multiply($multiple));
35
36 23
        $this->precision = $oldPrecision;
37
38 23
        return $remainder->truncateToPrecision($oldPrecision);
0 ignored issues
show
Bug introduced by
The method truncateToPrecision() does not exist on Samsara\Fermat\Values\ImmutableComplexNumber. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

38
        return $remainder->/** @scrutinizer ignore-call */ truncateToPrecision($oldPrecision);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method truncateToPrecision() does not exist on Samsara\Fermat\Types\Bas...mbers\FractionInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

38
        return $remainder->/** @scrutinizer ignore-call */ truncateToPrecision($oldPrecision);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
39
40
    }
41
42
    /**
43
     * @return bool
44
     */
45 94
    public function isComplex(): bool
46
    {
47 94
        return false;
48
    }
49
50
    /**
51
     * @param string $value
52
     * @param int|null $precision
53
     * @param int $base
54
     *
55
     * @return ImmutableDecimal
56
     */
57 110
    protected function setValue(string $value, int $precision = null, int $base = 10)
58
    {
59 110
        $imaginary = false;
60
61 110
        $precision = $precision ?? $this->getPrecision();
62
63 110
        if (strpos($value, 'i') !== false) {
64 8
            $value = str_replace('i', '', $value);
65 8
            $imaginary = true;
66
        }
67
68 110
        if ($base !== 10 || $this->getBase() !== 10) {
69 3
            $base = $base === 10 ? $this->getBase() : $base;
70 3
            $value = $this->convertValue($value, 10, $base);
71
        }
72
73 110
        if ($imaginary) {
74 8
            $value .= 'i';
75
        }
76
77 110
        return new ImmutableDecimal($value, $precision, $base);
78
    }
79
80
}