Failed Conditions
Push — dev ( 51d7f8...8abba3 )
by Jordan
13:12
created

ImmutableDecimal::setValue()   B

Complexity

Conditions 7
Paths 24

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 12
nc 24
nop 3
dl 0
loc 23
rs 8.8333
c 0
b 0
f 0
1
<?php
2
3
namespace Samsara\Fermat\Values;
4
5
use Samsara\Fermat\Numbers;
6
use Samsara\Fermat\Types\Decimal;
7
8
class ImmutableDecimal extends Decimal
9
{
10
11
    public function continuousModulo($mod)
12
    {
13
14
        if (is_object($mod) && method_exists($mod, 'getPrecision')) {
15
            $precision = ($this->getPrecision() < $mod->getPrecision()) ? $mod->getPrecision() : $this->getPrecision();
16
        } else {
17
            $precision = $this->getPrecision();
18
        }
19
20
        $oldPrecision = $this->precision;
21
        $newPrecision = $precision+1;
22
23
        $this->precision = $newPrecision;
24
25
        if (is_object($mod) && method_exists($mod, 'truncateToPrecision')) {
26
            $mod = $mod->truncateToPrecision($newPrecision);
27
        } else {
28
            $mod = Numbers::make(Numbers::IMMUTABLE, $mod, $newPrecision);
29
        }
30
31
        $multiple = $this->divide($mod)->floor();
0 ignored issues
show
Bug introduced by
The method floor() 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

31
        $multiple = $this->divide($mod)->/** @scrutinizer ignore-call */ floor();

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...
32
33
        $remainder = $this->subtract($mod->multiply($multiple));
34
35
        $this->precision = $oldPrecision;
36
37
        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

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