Passed
Push — master ( c50f33...e75397 )
by Jordan
04:38 queued 12s
created

ImmutableDecimal   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Test Coverage

Coverage 93.55%

Importance

Changes 0
Metric Value
eloc 33
c 0
b 0
f 0
dl 0
loc 86
ccs 29
cts 31
cp 0.9355
rs 10
wmc 13

2 Methods

Rating   Name   Duplication   Size   Complexity  
A continuousModulo() 0 27 6
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\BaseConversionProvider;
10
use Samsara\Fermat\Types\Base\Interfaces\Numbers\NumberInterface;
11
use Samsara\Fermat\Types\Decimal;
12
use Samsara\Fermat\Types\Base\Interfaces\Numbers\DecimalInterface;
13
14
/**
15
 *
16
 */
17
class ImmutableDecimal extends Decimal
18
{
19
20
    /**
21
     * @param NumberInterface|string|int|float $mod
22
     * @return DecimalInterface
23
     * @throws IntegrityConstraint
24
     * @throws MissingPackage
25
     */
26 9
    public function continuousModulo(NumberInterface|string|int|float $mod): DecimalInterface
27
    {
28
29 9
        if (is_object($mod) && method_exists($mod, 'getScale')) {
30 6
            $scale = ($this->getScale() < $mod->getScale()) ? $mod->getScale() : $this->getScale();
31
        } else {
32 3
            $scale = $this->getScale();
33
        }
34
35 9
        $oldScale = $this->scale;
36 9
        $newScale = $scale+1;
37
38 9
        $this->scale = $newScale;
39
40 9
        if (is_object($mod) && method_exists($mod, 'truncateToScale')) {
41 6
            $mod = $mod->truncateToScale($newScale);
42
        } else {
43 3
            $mod = Numbers::make(Numbers::IMMUTABLE, $mod, $newScale);
44
        }
45
46 9
        $multiple = $this->divide($mod)->floor();
0 ignored issues
show
Bug introduced by
It seems like floor() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

46
        $multiple = $this->divide($mod)->/** @scrutinizer ignore-call */ floor();
Loading history...
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

46
        $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...
47
48 9
        $remainder = $this->subtract($mod->multiply($multiple));
49
50 9
        $this->scale = $oldScale;
51
52 9
        return $remainder->truncateToScale($oldScale);
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

52
        return $remainder->/** @scrutinizer ignore-call */ truncateToScale($oldScale);

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
It seems like $oldScale can also be of type null; however, parameter $scale of Samsara\Fermat\Types\Decimal::truncateToScale() does only seem to accept integer, 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

52
        return $remainder->truncateToScale(/** @scrutinizer ignore-type */ $oldScale);
Loading history...
Bug introduced by
It seems like truncateToScale() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

52
        return $remainder->/** @scrutinizer ignore-call */ truncateToScale($oldScale);
Loading history...
53
54
    }
55
56
    /**
57
     * @param string $value
58
     * @param int|null $scale
59
     * @param NumberBase|null $base
60
     * @param bool $setToNewBase
61
     * @return ImmutableDecimal
62
     * @throws IntegrityConstraint
63
     */
64 91
    protected function setValue(string $value, ?int $scale = null, ?NumberBase $base = null, bool $setToNewBase = false): ImmutableDecimal
65
    {
66
        //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...
67
        //echo 'Input Value: '.$value.PHP_EOL;
68
        //echo 'Input Base: '.($base ? $base->value : 'null').PHP_EOL;
69 91
        $imaginary = false;
70
71 91
        $scale = $scale ?? $this->getScale();
72
73 91
        if (str_contains($value, 'i')) {
74 7
            $value = str_replace('i', '', $value);
75 7
            $imaginary = true;
76
        }
77
78 91
        if ((!is_null($base) && $base != NumberBase::Ten)) {
79
            $value = BaseConversionProvider::convertStringToBaseTen($value, $base);
80
        }
81
82 91
        if ($setToNewBase) {
83
            $base = $base ?? NumberBase::Ten;
84
        } else {
85 91
            $base = $this->getBase();
86
        }
87
88 91
        if ($imaginary) {
89 7
            $value .= 'i';
90
        }
91
92
        //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...
93
94 91
        $return = new ImmutableDecimal($value, $scale, $base, true);
95
96 91
        if (isset($this->calcMode)) {
97 16
            $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

97
            $return->setMode(/** @scrutinizer ignore-type */ $this->calcMode);
Loading history...
98
        }
99
100
        //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...
101
102 91
        return $return;
103
    }
104
105
}