Passed
Push — main ( 2b8b68...c2075e )
by Anatolyi
05:44
created

FisherYatesShuffle   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 17
Duplicated Lines 0 %

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 10 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gambling\Tech;
6
7
use Exception;
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 Exception
19
     */
20
    public function __invoke(array $array): array
21
    {
22
        foreach ($array as $i => $iValue) {
23
            $r = Random::getInteger(0, $i);
24
            $tmp = $iValue;
25
            $array[$i] = $array[$r];
26
            $array[$r] = $tmp;
27
        }
28
29
        return $array;
30
    }
31
}
32