Completed
Pull Request — master (#65)
by Woody
14:18
created

DifferenceMutator   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 20 3
A isAssoc() 0 5 1
1
<?php
2
3
namespace Underscore\Mutator;
4
5
use Underscore\Collection;
6
use Underscore\Mutator;
7
8
class DifferenceMutator extends Mutator
9
{
10
    /**
11
     * Finds the difference of two collections.
12
     *
13
     * @param Collection $collection
14
     * @param \Iterator  $values
15
     * @return Collection
16
     */
17
    public function __invoke($collection, $values)
18
    {
19
        $collection = clone $collection;
20
21
        if (!is_array($values)) {
22
            $values = iterator_to_array($values);
23
        }
24
25
        $current = $collection->getArrayCopy();
26
27
        if ($this->isAssoc($current)) {
28
            $values = array_diff_assoc($current, $values);
29
        } else {
30
            $values = array_diff($current, $values);
31
        }
32
33
        $collection->exchangeArray($values);
34
35
        return $collection;
36
    }
37
38
    /**
39
     * Check if an array is associative.
40
     *
41
     * @param array $values
42
     *
43
     * @return boolean
44
     */
45
    private function isAssoc(array $values)
46
    {
47
        $keys = array_keys($values);
48
        return $keys !== array_keys($keys);
49
    }
50
}
51