Passed
Push — master ( 162929...df6b58 )
by Chris
07:31
created

AbstractCollectionComparator   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Test Coverage

Coverage 71.43%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
eloc 10
c 2
b 0
f 0
dl 0
loc 36
ccs 10
cts 14
cp 0.7143
rs 10

5 Methods

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