Passed
Push — master ( 621c3c...4216b1 )
by Jordan
23:02 queued 16:19
created

ImmutableDecimal   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Test Coverage

Coverage 89.66%

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 83
ccs 26
cts 29
cp 0.8966
rs 10
c 0
b 0
f 0
wmc 11

2 Methods

Rating   Name   Duplication   Size   Complexity  
A continuousModulo() 0 24 4
B setValue() 0 39 7
1
<?php
2
3
namespace Samsara\Fermat\Values;
4
5
use Samsara\Exceptions\SystemError\PlatformError\MissingPackage;
6
use Samsara\Exceptions\UsageError\IntegrityConstraint;
7
use Samsara\Fermat\Enums\NumberBase;
8
use Samsara\Fermat\Numbers;
9
use Samsara\Fermat\Provider\ArithmeticProvider;
10
use Samsara\Fermat\Provider\BaseConversionProvider;
11
use Samsara\Fermat\Types\Base\Interfaces\Numbers\NumberInterface;
12
use Samsara\Fermat\Types\Decimal;
13
use Samsara\Fermat\Types\Base\Interfaces\Numbers\DecimalInterface;
14
15
/**
16
 *
17
 */
18
class ImmutableDecimal extends Decimal
19
{
20
21
    /**
22
     * @param NumberInterface|string|int|float $mod
23
     * @return DecimalInterface
24
     * @throws IntegrityConstraint
25
     * @throws MissingPackage
26
     */
27 9
    public function continuousModulo(NumberInterface|string|int|float $mod): DecimalInterface
28
    {
29
30 9
        $mod = Numbers::makeOrDont(Numbers::IMMUTABLE, $mod);
31
32 9
        $scale = ($this->getScale() < $mod->getScale()) ? $mod->getScale() : $this->getScale();
33
34 9
        $newScale = $scale+2;
35 9
        $thisNum = Numbers::make(Numbers::IMMUTABLE, $this->getValue(NumberBase::Ten), $newScale);
36
37 9
        if (is_object($mod) && method_exists($mod, 'truncateToScale')) {
38 9
            $mod = $mod->truncateToScale($newScale);
39
        } else {
40
            $mod = Numbers::make(Numbers::IMMUTABLE, $mod, $newScale);
41
        }
42
43
44 9
        $multiple = $thisNum->divide($mod, $newScale)->floor();
0 ignored issues
show
Bug introduced by
The method floor() does not exist on Samsara\Fermat\Types\Fraction. ( Ignorable by Annotation )

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

44
        $multiple = $thisNum->divide($mod, $newScale)->/** @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...
Bug introduced by
The method floor() does not exist on Samsara\Fermat\Values\ImmutableFraction. ( Ignorable by Annotation )

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

44
        $multiple = $thisNum->divide($mod, $newScale)->/** @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...
Bug introduced by
The method floor() does not exist on Samsara\Fermat\Values\MutableFraction. ( Ignorable by Annotation )

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

44
        $multiple = $thisNum->divide($mod, $newScale)->/** @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...
45
46 9
        $subtract = $mod->multiply($multiple);
47
48 9
        $remainder = $thisNum->subtract($subtract);
49
50 9
        return $remainder->truncateToScale($this->getScale()-1);
0 ignored issues
show
Bug introduced by
The method truncateToScale() does not exist on Samsara\Fermat\Types\Fraction. ( Ignorable by Annotation )

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

50
        return $remainder->/** @scrutinizer ignore-call */ truncateToScale($this->getScale()-1);

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...
Bug introduced by
The method truncateToScale() does not exist on Samsara\Fermat\Values\ImmutableFraction. ( Ignorable by Annotation )

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

50
        return $remainder->/** @scrutinizer ignore-call */ truncateToScale($this->getScale()-1);

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...
Bug introduced by
The method truncateToScale() does not exist on Samsara\Fermat\Values\MutableFraction. ( Ignorable by Annotation )

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

50
        return $remainder->/** @scrutinizer ignore-call */ truncateToScale($this->getScale()-1);

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...
51
52
    }
53
54
    /**
55
     * @param string $value
56
     * @param int|null $scale
57
     * @param NumberBase|null $base
58
     * @param bool $setToNewBase
59
     * @return ImmutableDecimal
60
     * @throws IntegrityConstraint
61
     */
62 93
    protected function setValue(string $value, ?int $scale = null, ?NumberBase $base = null, bool $setToNewBase = false): ImmutableDecimal
63
    {
64
        //echo '>>START SET VALUE [From: '.debug_backtrace()[1]['function'].' > '.debug_backtrace()[2]['function'].']<<'.PHP_EOL;
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
65
        //echo 'Input Value: '.$value.PHP_EOL;
66
        //echo 'Input Base: '.($base ? $base->value : 'null').PHP_EOL;
67 93
        $imaginary = false;
68
69 93
        $scale = $scale ?? $this->getScale();
70
71 93
        if (str_contains($value, 'i')) {
72 7
            $value = str_replace('i', '', $value);
73 7
            $imaginary = true;
74
        }
75
76 93
        if ((!is_null($base) && $base != NumberBase::Ten)) {
77
            $value = BaseConversionProvider::convertStringToBaseTen($value, $base);
78
        }
79
80 93
        if ($setToNewBase) {
81
            $base = $base ?? NumberBase::Ten;
82
        } else {
83 93
            $base = $this->getBase();
84
        }
85
86 93
        if ($imaginary) {
87 7
            $value .= 'i';
88
        }
89
90
        //echo 'Creating Decimal With: V['.$value.'] B['.$base->value.']'.PHP_EOL;
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
91
92 93
        $return = new ImmutableDecimal($value, $scale, $base, true);
93
94 93
        if (isset($this->calcMode)) {
95 15
            $return->setMode($this->calcMode);
0 ignored issues
show
Bug introduced by
It seems like $this->calcMode can also be of type null; however, parameter $mode of Samsara\Fermat\Types\Base\Number::setMode() does only seem to accept Samsara\Fermat\Enums\CalcMode, maybe add an additional type check? ( Ignorable by Annotation )

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

95
            $return->setMode(/** @scrutinizer ignore-type */ $this->calcMode);
Loading history...
96
        }
97
98
        //echo '>>END SET VALUE<<'.PHP_EOL;
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
99
100 93
        return $return;
101
    }
102
103
}