Biased   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 92.31%

Importance

Changes 0
Metric Value
wmc 6
eloc 12
dl 0
loc 31
ccs 12
cts 13
cp 0.9231
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A linearHigh() 0 3 1
A biasedNumberBetween() 0 12 3
A unbiased() 0 3 1
A linearLow() 0 3 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace DummyGenerator\Core;
6
7
use DummyGenerator\Definitions\Extension\Awareness\RandomizerAwareExtensionInterface;
8
use DummyGenerator\Definitions\Extension\Awareness\RandomizerAwareExtensionTrait;
9
use DummyGenerator\Definitions\Extension\BiasedExtensionInterface;
10
use DummyGenerator\Definitions\Extension\Exception\ExtensionArgumentException;
11
12
class Biased implements BiasedExtensionInterface, RandomizerAwareExtensionInterface
13
{
14
    use RandomizerAwareExtensionTrait;
15
16 4
    public function biasedNumberBetween(int $min = 0, int $max = 100, callable|string $function = 'sqrt'): int
17
    {
18 4
        if (!is_callable($function)) {
19
            throw new ExtensionArgumentException('Given $function must be a callable');
20
        }
21
22
        do {
23 4
            $x = $this->randomizer->getInt(0, PHP_INT_MAX) / PHP_INT_MAX;
24 4
            $y = $this->randomizer->getInt(0, PHP_INT_MAX) / (PHP_INT_MAX + 1);
25 4
        } while ($function($x) < $y);
26
27 4
        return (int) floor($x * ($max - $min + 1) + $min);
28
    }
29
30 1
    public function unbiased(): int
31
    {
32 1
        return 1;
33
    }
34
35 1
    public function linearLow(float $x): float
36
    {
37 1
        return 1 - $x;
38
    }
39
40 1
    public function linearHigh(float $x): float
41
    {
42 1
        return $x;
43
    }
44
}
45