Completed
Push — master ( 1eed51...38666c )
by Tony Karavasilev (Тони
04:53 queued 02:00
created

DataShuffler::shuffleArray()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 32
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 14
nc 6
nop 1
dl 0
loc 32
ccs 14
cts 14
cp 1
crap 5
rs 9.4888
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Utility class for shuffling information.
5
 */
6
7
namespace CryptoManana\Utilities;
8
9
use \CryptoManana\Core\Abstractions\Containers\AbstractRandomnessInjectable as RandomnessContainer;
10
use \CryptoManana\Core\Interfaces\Randomness\DataShufflingInterface as Shuffling;
11
use \CryptoManana\Core\StringBuilder as StringBuilder;
12
13
/**
14
 * Class DataShuffler - Utility class for data shuffling.
15
 *
16
 * @package CryptoManana\Utilities
17
 */
18
class DataShuffler extends RandomnessContainer implements Shuffling
19
{
20
    /**
21
     * Shuffle a string.
22
     *
23
     * @param string $string The string for shuffling.
24
     *
25
     * @return string The output shuffled/scrambled string.
26
     */
27 5
    public function shuffleString($string = '')
28
    {
29
        // Validate input
30 5
        if (!is_string($string)) {
31 1
            throw new \InvalidArgumentException('The supplied argument is not of type string.');
32
        }
33
34 4
        if (empty($string)) {
35 1
            return $string;
36
        }
37
38
        // Convert the string to an array
39 3
        $array = StringBuilder::stringSplit($string, 1);
40
41
        // Reuse the code for array shuffling and convert result to string
42 3
        return implode('', static::shuffleArray($array));
0 ignored issues
show
Bug Best Practice introduced by
The method CryptoManana\Utilities\D...huffler::shuffleArray() 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 implode('', static::/** @scrutinizer ignore-call */ shuffleArray($array));
Loading history...
43
    }
44
45
    /**
46
     * Shuffle an array.
47
     *
48
     * @param array $array The array for shuffling.
49
     *
50
     * @return array The output shuffled/scrambled array.
51
     */
52 5
    public function shuffleArray(array $array = [])
53
    {
54
        // Validate input
55 5
        if (empty($array)) {
56 1
            return $array;
57
        }
58
59 4
        $shuffled = [];
60 4
        $iterator = 0;
61
62
        // Reset pointer to the begging
63 4
        reset($array);
64
65
        // Iterate through array elements
66 4
        foreach ($array as $keyName => $value) {
67
            // Choose random index
68 4
            $newIndex = ($iterator === 0) ? 0 : $this->randomnessSource->getInt(0, $iterator);
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

68
            $newIndex = ($iterator === 0) ? 0 : $this->randomnessSource->/** @scrutinizer ignore-call */ getInt(0, $iterator);

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...
69
70
            // Shuffling logic
71 4
            if ($newIndex === $iterator) {
72 4
                $shuffled[] = $value;
73
            } else {
74 4
                $shuffled[] = $shuffled[$newIndex];
75 4
                $shuffled[$newIndex] = $value;
76
            }
77
78
            // Update the index
79 4
            $iterator++;
80
        }
81
82
        // Return the shuffled array
83 4
        return $shuffled;
84
    }
85
}
86