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

AbstractCollectionComparator::intersection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 2
crap 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