Passed
Push — main ( b1fbff...0e72dc )
by Johny
02:35
created

CoreRandomizer::randomKey()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
dl 0
loc 7
ccs 3
cts 4
cp 0.75
rs 10
c 1
b 0
f 0
cc 2
nc 2
nop 1
crap 2.0625
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 84
    public function getInt(int $min, int $max): int
17
    {
18 84
        return $this->randomizer->getInt($min, $max);
19
    }
20
21 6
    public function getFloat(float $min, float $max): float
22
    {
23 6
        return $this->randomizer->getFloat($min, $max, IntervalBoundary::ClosedClosed);
24
    }
25
26 4
    public function getBool(int $chanceOfTrue = 50): bool
27
    {
28 4
        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 1
    public function randomLetter(): string
37
    {
38 1
        return chr($this->randomizer->getInt(97, 122));
39
    }
40
41 74
    public function randomElement(array $array): mixed
42
    {
43 74
        if ($array === []) {
44
            return null;
45
        }
46
47 74
        $array = $this->shuffleElements($array);
48
49 74
        return reset($array);
50
    }
51
52 2
    public function randomKey(array $array = []): int|string|null
53
    {
54 2
        if (!$array) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $array of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
55
            return null;
56
        }
57
58 2
        return $this->randomElement(array_keys($array));
59
    }
60
61 76
    public function shuffleElements(array $array): array
62
    {
63 76
        return $this->randomizer->shuffleArray($array);
64
    }
65
66 3
    public function randomElements(array $array, int $count = 1, bool $unique = false): array
67
    {
68 3
        $arrayUnique = array_unique($array, SORT_REGULAR);
69
70 3
        if ($unique && $count > count($arrayUnique)) {
71 1
            throw new ExtensionArgumentException('Number of unique elements is lower than requested number of elements.');
72
        }
73
74 2
        if ($unique) {
75 1
            $array = $arrayUnique;
76
        }
77
78 2
        $result = [];
79
80 2
        for ($i = 0; $i < $count; $i++) {
81 2
            $array = $this->shuffleElements($array);
82 2
            $element = $this->randomElement($array);
83
84 2
            $result[] = $element;
85 2
            if ($unique && ($key = array_search($element, $array, true)) !== false) {
86 1
                unset($array[$key]);
87
            }
88
        }
89
90 2
        return $result;
91
    }
92
}
93