Completed
Pull Request — master (#64)
by Woody
14:01 queued 12:06
created

ShuffleMutator   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
c 1
b 0
f 0
lcom 0
cbo 2
dl 0
loc 35
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 6 1
A shuffleAssoc() 0 12 2
1
<?php
2
3
namespace Underscore\Mutator;
4
5
use Underscore\Collection;
6
use Underscore\Mutator;
7
8
class ShuffleMutator extends Mutator
9
{
10
    /**
11
     * Shuffles the values of a collection while preserving the keys.
12
     *
13
     * @param Collection $collection
14
     * @return Collection
15
     */
16
    public function __invoke($collection)
17
    {
18
        $values = $this->shuffleAssoc($collection->toArray());
19
20
        return $this->copyCollectionWith($collection, $values);
21
    }
22
23
    /**
24
     * Shuffle an array while preserving keys.
25
     *
26
     * @param array $values
27
     *
28
     * @return array
29
     */
30
    private function shuffleAssoc(array $values)
31
    {
32
        $keys = array_keys($values);
33
        shuffle($keys);
34
35
        $output = [];
36
        foreach ($keys as $key) {
37
            $output[$key] = $values[$key];
38
        }
39
40
        return $output;
41
    }
42
}
43