Passed
Push — master ( 5ad58b...7269b1 )
by Chris
07:31
created

AbstractCollectionComparator   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 60%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 11
c 1
b 0
f 0
dl 0
loc 41
ccs 9
cts 15
cp 0.6
rs 10

6 Methods

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