Passed
Push — main ( edc1f7...93cd7b )
by Johny
02:33
created

CoreRandomizer::getBytesFromString()   A

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 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
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 92
    public function getInt(int $min, int $max): int
17
    {
18 92
        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 7
    public function getBool(int $chanceOfTrue = 50): bool
27
    {
28 7
        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 4
    public function getBytesFromString(string $string, int $length = 8): string
37
    {
38 4
        return $this->randomizer->getBytesFromString($string, $length);
39
    }
40
41 3
    public function randomLetter(): string
42
    {
43 3
        return chr($this->randomizer->getInt(97, 122));
44
    }
45
46 80
    public function randomElement(array $array): mixed
47
    {
48 80
        $array = $this->shuffleElements($array);
49
50 80
        return reset($array);
51
    }
52
53 3
    public function randomKey(array $array = []): int|string|null
54
    {
55 3
        return $this->randomElement(array_keys($array));
56
    }
57
58 82
    public function shuffleElements(array $array): array
59
    {
60 82
        return $this->randomizer->shuffleArray($array);
61
    }
62
63 3
    public function randomElements(array $array, int $count = 1, bool $unique = false): array
64
    {
65 3
        $arrayUnique = array_unique($array, SORT_REGULAR);
66
67 3
        if ($unique && $count > count($arrayUnique)) {
68 1
            throw new ExtensionArgumentException('Number of unique elements is lower than requested number of elements.');
69
        }
70
71 2
        if ($unique) {
72 1
            $array = $arrayUnique;
73
        }
74
75 2
        $result = [];
76
77 2
        for ($i = 0; $i < $count; $i++) {
78 2
            $array = $this->shuffleElements($array);
79 2
            $element = $this->randomElement($array);
80
81 2
            $result[] = $element;
82 2
            if ($unique && ($key = array_search($element, $array, true)) !== false) {
83 1
                unset($array[$key]);
84
            }
85
        }
86
87 2
        return $result;
88
    }
89
}
90