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

FisherYatesShuffle::__invoke()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 6
c 1
b 0
f 1
dl 0
loc 10
rs 10
cc 2
nc 2
nop 1
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