Passed
Pull Request — master (#132)
by Jordan
05:46
created

MutableDecimal::setValue()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 26
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5.0729

Importance

Changes 0
Metric Value
cc 5
eloc 13
nc 8
nop 4
dl 0
loc 26
ccs 12
cts 14
cp 0.8571
crap 5.0729
rs 9.5222
c 0
b 0
f 0
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\Provider\BaseConversionProvider;
9
use Samsara\Fermat\Types\Base\Interfaces\Numbers\NumberInterface;
10
use Samsara\Fermat\Types\Decimal;
11
use Samsara\Fermat\Numbers;
12
use Samsara\Fermat\Types\Base\Interfaces\Numbers\DecimalInterface;
13
14
/**
15
 *
16
 */
17
class MutableDecimal extends Decimal
18
{
19
20
    /**
21
     * @throws IntegrityConstraint
22
     * @throws MissingPackage
23
     */
24 1
    public function continuousModulo(NumberInterface|string|int|float $mod): DecimalInterface
25
    {
26
27 1
        $mod = Numbers::makeOrDont(Numbers::IMMUTABLE, $mod, $this->scale+1);
28 1
        $oldNum = Numbers::make(Numbers::IMMUTABLE, $this->getValue(NumberBase::Ten), $this->scale+1);
29
30 1
        $multiple = $oldNum->divide($mod)->floor();
0 ignored issues
show
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

30
        $multiple = $oldNum->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...
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

30
        $multiple = $oldNum->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...
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

30
        $multiple = $oldNum->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...
31
32 1
        $remainder = $oldNum->subtract($mod->multiply($multiple));
33
34 1
        return Numbers::make(Numbers::MUTABLE, $remainder->truncate($this->scale-1)->getValue(NumberBase::Ten), $this->scale-1, $this->getBase());
0 ignored issues
show
Unused Code introduced by
The call to Samsara\Fermat\Types\Bas...erInterface::getValue() has too many arguments starting with Samsara\Fermat\Enums\NumberBase::Ten. ( Ignorable by Annotation )

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

34
        return Numbers::make(Numbers::MUTABLE, $remainder->truncate($this->scale-1)->/** @scrutinizer ignore-call */ getValue(NumberBase::Ten), $this->scale-1, $this->getBase());

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
Bug introduced by
The method truncate() 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

34
        return Numbers::make(Numbers::MUTABLE, $remainder->/** @scrutinizer ignore-call */ truncate($this->scale-1)->getValue(NumberBase::Ten), $this->scale-1, $this->getBase());

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 truncate() 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

34
        return Numbers::make(Numbers::MUTABLE, $remainder->/** @scrutinizer ignore-call */ truncate($this->scale-1)->getValue(NumberBase::Ten), $this->scale-1, $this->getBase());

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 truncate() 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

34
        return Numbers::make(Numbers::MUTABLE, $remainder->/** @scrutinizer ignore-call */ truncate($this->scale-1)->getValue(NumberBase::Ten), $this->scale-1, $this->getBase());

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...
35
36
    }
37
38
    /**
39
     * @param string $value
40
     * @param int|null $scale
41
     * @param NumberBase|null $base
42
     * @param bool $setToNewBase
43
     * @return MutableDecimal
44
     */
45 366
    protected function setValue(string $value, ?int $scale = null, ?NumberBase $base = null, bool $setToNewBase = false): self
46
    {
47 366
        $imaginary = false;
48
49 366
        if (str_contains($value, 'i')) {
50 20
            $value = str_replace('i', '', $value);
51 20
            $imaginary = true;
52
        }
53
54 366
        if (!is_null($base) && $base != NumberBase::Ten) {
55
            $value = BaseConversionProvider::convertStringToBaseTen($value, $base);
56
        }
57
58 366
        $this->imaginary = $imaginary;
59
60 366
        if ($setToNewBase) {
61
            $this->base = $base ?? $this->getBase();
62
        }
63
64 366
        $this->value = $this->translateValue($value);
65
66 366
        $scale = $scale ?? $this->getScale();
67
68 366
        $this->scale = $this->determineScale($this->getDecimalPart(), $scale);
69
70 366
        return $this;
71
    }
72
73
}