Test Failed
Pull Request — master (#47)
by Jordan
14:20
created

ImmutableDecimal::continuousModulo()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 15
nc 6
nop 1
dl 0
loc 27
rs 9.2222
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 modulo($mod)
12
    {
13
        $oldBase = $this->convertForModification();
0 ignored issues
show
Bug introduced by
The method convertForModification() does not exist on Samsara\Fermat\Values\ImmutableDecimal. ( Ignorable by Annotation )

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

13
        /** @scrutinizer ignore-call */ $oldBase = $this->convertForModification();

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...
14
15
        return (new ImmutableDecimal(bcmod($this->getValue(), $mod), $this->getPrecision(), $this->getBase()))->convertFromModification($oldBase);
0 ignored issues
show
Bug introduced by
The method convertFromModification() does not exist on Samsara\Fermat\Values\ImmutableDecimal. ( Ignorable by Annotation )

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

15
        return (new ImmutableDecimal(bcmod($this->getValue(), $mod), $this->getPrecision(), $this->getBase()))->/** @scrutinizer ignore-call */ convertFromModification($oldBase);

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...
16
    }
17
18
    public function continuousModulo($mod)
19
    {
20
21
        if (is_object($mod) && method_exists($mod, 'getPrecision')) {
22
            $precision = ($this->getPrecision() < $mod->getPrecision()) ? $mod->getPrecision() : $this->getPrecision();
23
        } else {
24
            $precision = $this->getPrecision();
25
        }
26
27
        $oldPrecision = $this->precision;
28
        $newPrecision = $precision+1;
29
30
        $this->precision = $newPrecision;
31
32
        if (is_object($mod) && method_exists($mod, 'truncateToPrecision')) {
33
            $mod = $mod->truncateToPrecision($newPrecision);
34
        } else {
35
            $mod = Numbers::make(Numbers::IMMUTABLE, $mod, $newPrecision);
36
        }
37
38
        $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

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

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