Completed
Push — master ( 2683bd...cf87f7 )
by Jordan
21s queued 13s
created

StochasticAdapter   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Test Coverage

Coverage 62.5%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
dl 0
loc 27
ccs 10
cts 16
cp 0.625
rs 10
c 1
b 0
f 0
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A determineCarry() 0 21 3
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 1
            $target = $digit;
23 1
            $rangeMin = 0;
24 1
            $rangeMax = 9;
25
        } else {
26
            $remainder = substr($remainder, 0, 3);
27
            $target = (int)($digit.$remainder);
28
            $rangeMin = 0;
29
            $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
}