CoreRandomizer::getFloat()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace DummyGenerator\Core\Randomizer;
6
7
use DummyGenerator\Definitions\Extension\Exception\ExtensionArgumentException;
8
use DummyGenerator\Definitions\Randomizer\RandomizerInterface;
9
use Random\IntervalBoundary;
0 ignored issues
show
Bug introduced by
The type Random\IntervalBoundary 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 Random\Randomizer as PhpRandomizer;
0 ignored issues
show
Bug introduced by
The type Random\Randomizer 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
12
abstract class CoreRandomizer implements RandomizerInterface
13
{
14
    protected PhpRandomizer $randomizer;
15
16 90
    public function getInt(int $min, int $max): int
17
    {
18 90
        return $this->randomizer->getInt($min, $max);
19
    }
20
21 7
    public function getFloat(float $min, float $max): float
22
    {
23 7
        return $this->randomizer->getFloat($min, $max, IntervalBoundary::ClosedClosed);
24
    }
25
26 8
    public function getBool(int $chanceOfTrue = 50): bool
27
    {
28 8
        return $this->randomizer->getInt(1, 100) <= $chanceOfTrue;
29
    }
30
31 3
    public function getBytes(int $length = 16): string
32
    {
33 3
        return $this->randomizer->getBytes($length);
34
    }
35
36 3
    public function randomLetter(): string
37
    {
38 3
        return chr($this->randomizer->getInt(97, 122));
39
    }
40
41 77
    public function randomElement(array $array): mixed
42
    {
43 77
        $array = $this->shuffleElements($array);
44
45 77
        return reset($array);
46
    }
47
48 3
    public function randomKey(array $array = []): int|string|null
49
    {
50 3
        return $this->randomElement(array_keys($array));
51
    }
52
53 79
    public function shuffleElements(array $array): array
54
    {
55 79
        return $this->randomizer->shuffleArray($array);
56
    }
57
58 3
    public function randomElements(array $array, int $count = 1, bool $unique = false): array
59
    {
60 3
        $arrayUnique = array_unique($array, SORT_REGULAR);
61
62 3
        if ($unique && $count > count($arrayUnique)) {
63 1
            throw new ExtensionArgumentException('Number of unique elements is lower than requested number of elements.');
64
        }
65
66 2
        if ($unique) {
67 1
            $array = $arrayUnique;
68
        }
69
70 2
        $result = [];
71
72 2
        for ($i = 0; $i < $count; $i++) {
73 2
            $array = $this->shuffleElements($array);
74 2
            $element = $this->randomElement($array);
75
76 2
            $result[] = $element;
77 2
            if ($unique && ($key = array_search($element, $array, true)) !== false) {
78 1
                unset($array[$key]);
79
            }
80
        }
81
82 2
        return $result;
83
    }
84
}
85