MutableDecimal::continuousModulo()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2.003

Importance

Changes 0
Metric Value
cc 2
dl 0
loc 19
ccs 10
cts 11
cp 0.9091
crap 2.003
rs 9.9
c 0
b 0
f 0
eloc 11
nc 2
nop 1
1
<?php
2
3
namespace Samsara\Fermat\Core\Values;
4
5
use Samsara\Exceptions\SystemError\PlatformError\MissingPackage;
6
use Samsara\Exceptions\UsageError\IntegrityConstraint;
7
use Samsara\Fermat\Core\Enums\NumberBase;
8
use Samsara\Fermat\Core\Provider\BaseConversionProvider;
9
use Samsara\Fermat\Core\Types\Base\Interfaces\Numbers\NumberInterface;
0 ignored issues
show
Bug introduced by
The type Samsara\Fermat\Core\Type...Numbers\NumberInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Samsara\Fermat\Core\Types\Decimal;
0 ignored issues
show
Bug introduced by
The type Samsara\Fermat\Core\Types\Decimal was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Samsara\Fermat\Core\Numbers;
12
use Samsara\Fermat\Core\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 2
    public function continuousModulo(NumberInterface|string|int|float $mod): DecimalInterface
25
    {
26
27 2
        $mod = Numbers::makeOrDont(Numbers::IMMUTABLE, $mod, $this->scale+1);
28 2
        $oldNum = Numbers::make(Numbers::IMMUTABLE, $this->getValue(NumberBase::Ten), $this->scale+1);
29
30 2
        $multiple = $oldNum->divide($mod)->floor();
31 2
        $multipleCeil = $multiple->ceil();
32 2
        $digits = $multipleCeil->subtract($multiple)->numberOfLeadingZeros();
33
34 2
        if ($digits >= $this->getScale()) {
35
            $multiple = $multipleCeil;
36
        } else {
37 2
            $multiple = $multiple->floor();
38
        }
39
40 2
        $remainder = $oldNum->subtract($mod->multiply($multiple));
41
42 2
        return Numbers::make(Numbers::MUTABLE, $remainder->truncate($this->scale-1)->getValue(NumberBase::Ten), $this->scale-1, $this->getBase());
43
44
    }
45
46
    /**
47
     * @param string $value
48
     * @param int|null $scale
49
     * @param NumberBase|null $base
50
     * @param bool $setToNewBase
51
     * @return MutableDecimal
52
     */
53 1482
    protected function setValue(string $value, ?int $scale = null, ?NumberBase $base = null, bool $setToNewBase = false): self
54
    {
55 1482
        $imaginary = false;
56
57 1482
        if (str_contains($value, 'i')) {
58 30
            $value = str_replace('i', '', $value);
59 30
            $imaginary = true;
60
        }
61
62 1482
        if (!is_null($base) && $base != NumberBase::Ten) {
63
            $value = BaseConversionProvider::convertStringToBaseTen($value, $base);
64
        }
65
66 1482
        $this->imaginary = $imaginary;
0 ignored issues
show
Bug Best Practice introduced by
The property imaginary does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
67
68 1482
        if ($setToNewBase) {
69
            $this->base = $base ?? $this->getBase();
0 ignored issues
show
Bug Best Practice introduced by
The property base does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
70
        }
71
72 1482
        $this->value = $this->translateValue($value);
0 ignored issues
show
Bug Best Practice introduced by
The property value does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
73
74 1482
        $scale = $scale ?? $this->getScale();
75
76 1482
        $this->scale = $this->determineScale($this->getDecimalPart(), $scale);
0 ignored issues
show
Bug Best Practice introduced by
The property scale does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
77
78 1482
        return $this;
79
    }
80
81
}