Passed
Push — dev ( 3795a8...3f4d81 )
by Jordan
14:20
created

ImmutableNumber   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 56
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A modulo() 0 5 1
A continuousModulo() 0 27 6
A isComplex() 0 3 1
A setValue() 0 3 1
1
<?php
2
3
namespace Samsara\Fermat\Values;
4
5
use Samsara\Fermat\Numbers;
6
use Samsara\Fermat\Types\Number;
7
use Samsara\Fermat\Types\Base\DecimalInterface;
8
use Samsara\Fermat\Types\Base\NumberInterface;
9
10
class ImmutableNumber extends Number implements NumberInterface, DecimalInterface
11
{
12
13 5
    public function modulo($mod)
14
    {
15 5
        $oldBase = $this->convertForModification();
16
17 5
        return (new ImmutableNumber(bcmod($this->getValue(), $mod), $this->getPrecision(), $this->getBase()))->convertFromModification($oldBase);
18
    }
19
20 7
    public function continuousModulo($mod)
21
    {
22
23 7
        if (is_object($mod) && method_exists($mod, 'getPrecision')) {
24 6
            $precision = ($this->getPrecision() < $mod->getPrecision()) ? $mod->getPrecision() : $this->getPrecision();
25
        } else {
26 1
            $precision = $this->getPrecision();
27
        }
28
29 7
        $oldPrecision = $this->precision;
30 7
        $newPrecision = $precision+1;
31
32 7
        $this->precision = $newPrecision;
33
34 7
        if (is_object($mod) && method_exists($mod, 'truncateToPrecision')) {
35 6
            $mod = $mod->truncateToPrecision($newPrecision);
36
        } else {
37 1
            $mod = Numbers::make(Numbers::IMMUTABLE, $mod, $newPrecision);
38
        }
39
40 7
        $multiple = $this->divide($mod)->floor();
41
42 7
        $remainder = $this->subtract($mod->multiply($multiple));
43
44 7
        $this->precision = $oldPrecision;
45
46 7
        return $remainder->truncateToPrecision($oldPrecision);
47
48
    }
49
50
    /**
51
     * @return bool
52
     */
53
    public function isComplex(): bool
54
    {
55 69
        return false;
56
    }
57 69
58
    /**
59
     * @param $value
60
     *
61
     * @return ImmutableNumber
62
     */
63
    protected function setValue($value)
64
    {
65
        return new ImmutableNumber($value, $this->getPrecision(), $this->getBase());
66
    }
67
68
}