Passed
Pull Request — master (#135)
by Jordan
05:57
created

StochasticAdapter::determineCarry()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3.2746

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 15
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 21
ccs 11
cts 16
cp 0.6875
crap 3.2746
rs 9.7666
1
<?php
2
3
namespace Samsara\Fermat\Provider\RoundingModeAdapters\Modes;
4
5
use Samsara\Fermat\Enums\RandomMode;
6
use Samsara\Fermat\Provider\RandomProvider;
7
8
/**
9
 *
10
 */
11
class StochasticAdapter extends BaseAdapter
12
{
13
14
    /**
15
     * @inheritDoc
16
     */
17 1
    public function determineCarry(int $digit, int $nextDigit): int
18
    {
19 1
        $remainder = $this->remainder;
20
21 1
        if (is_null($remainder)) {
22
            $target = $digit;
23
            $rangeMin = 0;
24
            $rangeMax = 9;
25
        } else {
26 1
            $remainder = substr($remainder, 0, 3);
27 1
            $target = (int)($digit.$remainder);
28 1
            $rangeMin = 0;
29 1
            $rangeMax = (int)str_repeat('9', strlen($remainder) + 1);
30
        }
31
32 1
        $random = RandomProvider::randomInt($rangeMin, $rangeMax, RandomMode::Speed)->asInt();
33
34 1
        if ($random < $target) {
35 1
            return 1;
36
        } else {
37 1
            return 0;
38
        }
39
    }
40
}