Failed Conditions
Pull Request — master (#47)
by Jordan
47:12 queued 32:11
created

ImmutableDecimal   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 75
rs 10
c 0
b 0
f 0
wmc 13

4 Methods

Rating   Name   Duplication   Size   Complexity  
A continuousModulo() 0 27 6
A modulo() 0 3 1
A isComplex() 0 3 1
A setValue() 0 22 5
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 modulo($mod)
12
    {
13
        return $this->setValue(bcmod($this->getAsBaseTenRealNumber(), $mod), $this->getPrecision(), $this->getBase());
14
    }
15
16
    public function continuousModulo($mod)
17
    {
18
19
        if (is_object($mod) && method_exists($mod, 'getPrecision')) {
20
            $precision = ($this->getPrecision() < $mod->getPrecision()) ? $mod->getPrecision() : $this->getPrecision();
21
        } else {
22
            $precision = $this->getPrecision();
23
        }
24
25
        $oldPrecision = $this->precision;
26
        $newPrecision = $precision+1;
27
28
        $this->precision = $newPrecision;
29
30
        if (is_object($mod) && method_exists($mod, 'truncateToPrecision')) {
31
            $mod = $mod->truncateToPrecision($newPrecision);
32
        } else {
33
            $mod = Numbers::make(Numbers::IMMUTABLE, $mod, $newPrecision);
34
        }
35
36
        $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

36
        $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...
37
38
        $remainder = $this->subtract($mod->multiply($multiple));
39
40
        $this->precision = $oldPrecision;
41
42
        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

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