Completed
Push — master ( e85a68...b6ead1 )
by Jesse
03:13
created

Differentiating   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 10
c 0
b 0
f 0
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A differenceBetween() 0 12 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stratadox\ImmutableCollection;
6
7
use Stratadox\Collection\Collection;
8
use Stratadox\Collection\Differentiable;
9
10
/**
11
 * Behaviour to find the difference between multiple collections.
12
 *
13
 * Provides access to differentiating behaviour in the form of a method that
14
 * returns a modified copy of the original (immutable) collection with all
15
 * values omitted that also appear in one of the other collections.
16
 *
17
 * @package Stratadox\Collection
18
 * @author Stratadox
19
 */
20
trait Differentiating
21
{
22
    /**
23
     * @param Collection[] ...$others
24
     * @return Differentiable|static
25
     */
26
    public function differenceBetween(Collection ...$others) : Differentiable
27
    {
28
        return $this->newCopy(array_filter(
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->newCopy(ar...on(...) { /* ... */ })) returns the type Stratadox\Collection\Collection which is incompatible with the type-hinted return Stratadox\Collection\Differentiable.
Loading history...
29
            $this->items(),
30
            function ($item) use ($others) : bool
31
            {
32
                foreach ($others as $otherCollection) {
33
                    if (in_array($item, $otherCollection->items())) {
34
                        return false;
35
                    }
36
                }
37
                return true;
38
            }
39
        ));
40
    }
41
42
    /** @see Collection::items() */
43
    abstract public function items() : array;
44
45
    /**
46
     * @see ImmutableCollection::newCopy()
47
     * @param array $items
48
     * @return Collection|static
49
     */
50
    abstract protected function newCopy(array $items) : Collection;
51
}