Passed
Push — master ( 4c1cb3...4f5a04 )
by Chris
09:14
created

AbstractCollectionComparator   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 64.71%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 7
eloc 13
c 2
b 0
f 0
dl 0
loc 45
ccs 11
cts 17
cp 0.6471
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A whatEvenIsThis() 0 11 2
A intersection() 0 4 1
A stripDuplicates() 0 3 1
A difference() 0 5 1
A matches() 0 3 1
A notIn() 0 4 1
1
<?php
2
3
namespace WebTheory\Collection\Comparison\Abstracts;
4
5
use WebTheory\Collection\Contracts\CollectionComparatorInterface;
6
7
abstract class AbstractCollectionComparator implements CollectionComparatorInterface
8
{
9 33
    public function notIn(array $array, array $values): array
10
    {
11 33
        return array_values(
12 33
            array_udiff($array, $values, $this->getComparisonFunction())
13
        );
14
    }
15
16 21
    public function difference(array $array1, array $array2): array
17
    {
18
        return [
19 21
            ...$this->notIn($array1, $array2),
20 21
            ...$this->notIn($array2, $array1)
21
        ];
22
    }
23
24 15
    public function intersection($array, $values): array
25
    {
26 15
        return array_values(
27 15
            array_uintersect($array, $values, $this->getComparisonFunction())
28
        );
29
    }
30
31 6
    public function matches(array $array1, array $array2): bool
32
    {
33 6
        return empty($this->difference($array1, $array2));
34
    }
35
36
    protected function stripDuplicates(array $array): array
37
    {
38
        return array_unique($array, SORT_REGULAR);
39
    }
40
41
    protected function whatEvenIsThis(array $a, array $b): array
42
    {
43
        /*
44
         if both primary and secondary are empty this will return false
45
         because the "array_diff" family of functions returns an empty array
46
         if the first array provided is empty itself. if both arrays are
47
         empty this will return an empty array as there is no difference.
48
         */
49
        return !empty($a)
50
            ? $this->notIn($a, $b)
51
            : $this->notIn($b, $a);
52
    }
53
}
54