FisherYatesShuffle   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 8
c 2
b 0
f 1
dl 0
loc 19
rs 10
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 12 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gambling\Tech;
6
7
use Gambling\Tech\Exception\GamblingTechException;
8
9
/**
10
 * The Fisher Yates shuffle, read more about it here
11
 * https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
12
 */
13
class FisherYatesShuffle
14
{
15
    /**
16
     * @param array $array
17
     * @return array
18
     * @throws GamblingTechException
19
     */
20
    public function __invoke(array $array): array
21
    {
22
        $count = count($array);
23
24
        for ($i = 0; $i < $count - 1; $i++) {
25
            $r = Random::getInteger(0, $count - 1);
26
            $tmp = $array[$i];
27
            $array[$i] = $array[$r];
28
            $array[$r] = $tmp;
29
        }
30
31
        return $array;
32
    }
33
}
34