Differentiating::differenceBetween()   A
last analyzed

Complexity

Conditions 3
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 1
nop 1
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stratadox\ImmutableCollection;
6
7
use function in_array;
8
use Stratadox\Collection\Collection;
9
use Stratadox\Collection\Differentiable;
10
11
/**
12
 * Behaviour to find the difference between multiple collections.
13
 *
14
 * Provides access to differentiating behaviour in the form of a method that
15
 * returns a modified copy of the original (immutable) collection with all
16
 * values omitted that also appear in one of the other collections.
17
 *
18
 * @package Stratadox\Collection
19
 * @author  Stratadox
20
 */
21
trait Differentiating
22
{
23
    /**
24
     * @see Differentiable::differenceBetween()
25
     * @param Collection[] ...$others
26
     * @return static
27
     */
28
    public function differenceBetween(Collection ...$others)
29
    {
30
        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 documented return type Stratadox\ImmutableCollection\Differentiating.
Loading history...
31
            $this->items(),
32
            function ($item) use ($others) : bool {
33
                foreach ($others as $otherCollection) {
34
                    if (in_array($item, $otherCollection->items(), false)) {
35
                        return false;
36
                    }
37
                }
38
                return true;
39
            }
40
        ));
41
    }
42
43
    /** @see Collection::items() */
44
    abstract public function items(): array;
45
46
    /**
47
     * @see ImmutableCollection::newCopy()
48
     * @param array $items
49
     * @return static
50
     */
51
    abstract protected function newCopy(array $items): Collection;
52
}