Passed
Push — main ( ebe7df...9489b3 )
by Peter
02:53
created

FakeCarHelper::getRangeWithUnit()   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
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Faker\Provider;
4
5
use Exception;
6
use InvalidArgumentException;
7
use Random\RandomException;
0 ignored issues
show
Bug introduced by
The type Random\RandomException 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...
8
9
class FakeCarHelper
10
{
11
    /**
12
     * @throws Exception
13
     */
14 7
    public static function getArrayData(array $arrayData, int $count = 1)
15
    {
16 7
        $data = static::isWeighted($arrayData)
17 4
            ? static::getWeighted($arrayData, $count)
18 3
            : static::getRandomElementsFromArray($arrayData, $count);
19
20 7
        if (is_array($data) && $count === 1) {
21 2
            return array_values($data)[0];
22
        }
23
24 5
        return $data;
25
    }
26
27
    /**
28
     * Determines if an array is weighted(associative).
29
     *
30
     * An array is "associative" if it doesn't have sequential numerical keys beginning with zero.
31
     *
32
     * @param  array  $array
33
     * @return bool
34
     */
35 7
    public static function isWeighted(array $array): bool
36
    {
37 7
        $keys = array_keys($array);
38
39 7
        return array_keys($keys) !== $keys;
40
    }
41
42
    /**
43
     * Returns a random element from a passed array
44
     *
45
     * @param array $values
46
     *
47
     * @throws Exception
48
     */
49 5
    public static function getRandomElementFromArray($values)
50
    {
51 5
        $elements = static::getRandomElementsFromArray($values, 1);
52
53 5
        return array_values($elements)[0];
54
    }
55
56
    /**
57
     * Get random elements from input array
58
     *
59
     * @param array $values The input array
60
     * @param int $count The number of random elements you want to get in your response.
61
     * Leave out or set to 0 for random.
62
     *
63
     * @return mixed
64
     * @throws InvalidArgumentException|Exception
65
     */
66 8
    public static function getRandomElementsFromArray(array $values, ?int $count = 1): array
67
    {
68
        //dd('getRandomElementsFromArray', $values,$count);
69 8
        $valuesLength = count($values);
70
71 8
        if ($count > $valuesLength) {
72 1
            throw new InvalidArgumentException('Count larger than array length.');
73
        }
74
75 8
        if ($count === 0) {
76 2
            return [];
77
        }
78
79 8
        if (!$count) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $count of type integer|null is loosely compared to false; this is ambiguous if the integer can be 0. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
80 1
            $count = random_int(1, $valuesLength);
81
        }
82
83 8
        return array_intersect_key(
84 8
            $values,
85 8
            array_flip(
86 8
                (array) array_rand($values, $count)
87 8
            )
88 8
        );
89
    }
90
91
    /**
92
     * Get one element out of an input array with specified weights to get the distribution
93
     * of the generated elements as you want them.
94
     *
95
     * @param array $values Input array with values as key and weight as value. ['value 1' => 30, 'value 7' => 70]
96
     * @param int $count
97
     *
98
     * @return string
99
     * @throws Exception
100
     */
101 5
    public static function getWeighted(array $values, int $count = 1): string
0 ignored issues
show
Unused Code introduced by
The parameter $count is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

101
    public static function getWeighted(array $values, /** @scrutinizer ignore-unused */ int $count = 1): string

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
102
    {
103 5
        $currentTotal = 0;
104 5
        $firstRand = random_int(1, 100);
105
106 5
        $total = array_sum($values);
107
108 5
        $rand = ($firstRand / 100) * $total;
109
110 5
        foreach ($values as $key => $weight) {
111 5
            $currentTotal += $weight;
112
113 5
            if ($rand <= $currentTotal) {
114 5
                return $key;
115
            }
116
        }
117
118 1
        return '';
119
    }
120
121
    /**
122
     * @param array<int> $range
123
     * @param string $unit
124
     *
125
     * @return string
126
     * @throws RandomException
127
     */
128 1
    public static function getRangeWithUnit(array $range, string $unit): string
129
    {
130 1
        return random_int($range[0], $range[1]) . ' ' . $unit;
131
    }
132
}
133