Failed Conditions
Pull Request — master (#47)
by Jordan
07:14 queued 03:12
created

ImmutableDecimal::setValue()   B

Complexity

Conditions 7
Paths 24

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 7

Importance

Changes 0
Metric Value
cc 7
eloc 12
nc 24
nop 3
dl 0
loc 23
ccs 13
cts 13
cp 1
crap 7
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 16
    public function continuousModulo($mod)
12
    {
13
14 16
        if (is_object($mod) && method_exists($mod, 'getPrecision')) {
15 15
            $precision = ($this->getPrecision() < $mod->getPrecision()) ? $mod->getPrecision() : $this->getPrecision();
16
        } else {
17 4
            $precision = $this->getPrecision();
18
        }
19
20 16
        $oldPrecision = $this->precision;
21 16
        $newPrecision = $precision+1;
22
23 16
        $this->precision = $newPrecision;
24
25 16
        if (is_object($mod) && method_exists($mod, 'truncateToPrecision')) {
26 15
            $mod = $mod->truncateToPrecision($newPrecision);
27
        } else {
28 4
            $mod = Numbers::make(Numbers::IMMUTABLE, $mod, $newPrecision);
29
        }
30
31 16
        $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 16
        $remainder = $this->subtract($mod->multiply($multiple));
34
35 16
        $this->precision = $oldPrecision;
36
37 16
        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 53
    public function isComplex(): bool
45
    {
46 53
        return false;
47
    }
48
49
    /**
50
     * @param string $value
51
     * @param int $precision
52
     * @param int $base
53
     *
54
     * @return ImmutableDecimal
55
     */
56 69
    protected function setValue(string $value, int $precision = null, int $base = 10)
57
    {
58 69
        $imaginary = false;
59
60 69
        if (strpos($value, 'i') !== false) {
61 1
            $value = str_replace('i', '', $value);
62 1
            $imaginary = true;
63
        }
64
65 69
        if ($base != 10 || $this->getBase() != 10) {
66 3
            $base = $base == 10 ? $this->getBase() : $base;
67 3
            $value = $this->convertValue($value, 10, $base);
68
        }
69
70 69
        if ($imaginary) {
71 1
            $value .= 'i';
72
        }
73
74 69
        if (is_null($precision)) {
75 62
            $precision = $this->getPrecision();
76
        }
77
78 69
        return new ImmutableDecimal($value, $precision, $base);
79
    }
80
81
}