Passed
Push — master ( b1f149...d7488c )
by Tony Karavasilev (Тони
01:27
created

ElementPicker::pickCharacterElement()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 16
ccs 0
cts 10
cp 0
crap 12
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Utility class for random element picking.
5
 */
6
7
namespace CryptoManana\Utilities;
8
9
use \CryptoManana\Core\Abstractions\Containers\AbstractRandomnessInjectable as RandomnessContainer;
10
use \CryptoManana\Core\Interfaces\Randomness\ElementPickingInterface as ElementChoosing;
11
use \CryptoManana\Core\StringBuilder as StringBuilder;
12
13
/**
14
 * Class ElementPicker - Utility class for random element picking.
15
 *
16
 * @package CryptoManana\Utilities
17
 */
18
class ElementPicker extends RandomnessContainer implements ElementChoosing
19
{
20
    /**
21
     * Pick a random character from string.
22
     *
23
     * @param string $string The string with characters for choosing from.
24
     *
25
     * @return string The chosen character string.
26
     */
27
    public function pickCharacterElement($string = '')
28
    {
29
        // Validate input
30
        if (!is_string($string)) {
31
            throw new \InvalidArgumentException('The supplied argument is not of type string.');
32
        }
33
34
        if (empty($string)) {
35
            return $string;
36
        }
37
38
        // Convert the string to an array
39
        $array = StringBuilder::stringSplit($string, 1);
40
41
        // Reuse the code for array element choosing
42
        return static::pickArrayElement($array);
0 ignored issues
show
Bug Best Practice introduced by
The method CryptoManana\Utilities\E...ker::pickArrayElement() is not static, but was called statically. ( Ignorable by Annotation )

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

42
        return static::/** @scrutinizer ignore-call */ pickArrayElement($array);
Loading history...
Bug introduced by
It seems like $array can also be of type false; however, parameter $array of CryptoManana\Utilities\E...ker::pickArrayElement() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

42
        return static::pickArrayElement(/** @scrutinizer ignore-type */ $array);
Loading history...
43
    }
44
45
    /**
46
     * Pick a random element from array.
47
     *
48
     * @param array $array The array with elements for choosing from.
49
     *
50
     * @return mixed The chosen element from the array.
51
     */
52
    public function pickArrayElement(array $array = [])
53
    {
54
        // Validate input
55
        if (empty($array)) {
56
            return $array;
57
        }
58
59
        $count = count($array);
60
61
        $iterator = 0;
62
63
        // Choose random index
64
        $elementIndex = ($count === 1) ? 0 : $this->randomnessSource->getInt(0, $count - 1);
0 ignored issues
show
Bug introduced by
The method getInt() does not exist on null. ( Ignorable by Annotation )

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

64
        $elementIndex = ($count === 1) ? 0 : $this->randomnessSource->/** @scrutinizer ignore-call */ getInt(0, $count - 1);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
65
66
        // Reset pointer to begging
67
        reset($array);
68
69
        $tmpElement = null;
70
71
        // Iterate through array elements
72
        foreach ($array as $keyName => $value) {
73
            // If element is found
74
            if ($elementIndex == $iterator) {
75
                $tmpElement = $value;
76
77
                break;
78
            }
79
80
            // Update the index
81
            $iterator++;
82
        }
83
84
        // Return the chosen element
85
        return $tmpElement;
86
    }
87
}
88