Completed
Push — master ( 3be46a...97cb14 )
by Woody
02:36
created

ShuffleMutator   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

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
ccs 11
cts 11
cp 1
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 1
    public function __invoke($collection)
17
    {
18 1
        $values = $this->shuffleAssoc($collection->toArray());
19
20 1
        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 1
    private function shuffleAssoc(array $values)
31
    {
32 1
        $keys = array_keys($values);
33 1
        shuffle($keys);
34
35 1
        $output = [];
36 1
        foreach ($keys as $key) {
37 1
            $output[$key] = $values[$key];
38 1
        }
39
40 1
        return $output;
41
    }
42
}
43