Passed
Branch master (fb2e38)
by Sebastian
02:23
created

ListOperationsTrait::minus()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 10
c 1
b 0
f 0
nc 6
nop 1
dl 0
loc 15
ccs 10
cts 10
cp 1
crap 5
rs 9.6111
1
<?php
2
3
namespace Seboettg\Collection\Lists\ListFeatures;
4
5
use Seboettg\Collection\Lists\ListInterface;
6
use function Seboettg\Collection\Lists\emptyList;
7
use function Seboettg\Collection\Lists\listOf;
8
9
trait ListOperationsTrait
10
{
11
12
13
14 2
    public function plus(iterable $other): ListInterface
15
    {
16 2
        $list = listOf(...$this->array);
17 2
        $list->addAll($other);
18 2
        return $list;
19
    }
20
21
    /**
22
     * @inheritDoc
23
     */
24 4
    public function minus(iterable $values): ListInterface
25
    {
26 4
        $valuesList = emptyList();
27 4
        if (!$values instanceof ListInterface && is_array($values)) {
28 1
            $valuesList->setArray($values);
29
        } else {
30 3
            $valuesList = $values;
31
        }
32 4
        $newInstance = emptyList();
33 4
        foreach ($this->array as $value) {
34 4
            if (!$valuesList->contains($value)) {
35 4
                $newInstance->add($value);
36
            }
37
        }
38 4
        return $newInstance;
39
    }
40
41
    /**
42
     * @inheritDoc
43
     */
44 2
    public function intersect(ListInterface $list): ListInterface
45
    {
46 2
        $result = emptyList();
47 2
        foreach ($list as $item) {
48 2
            if ($this->contains($item)) {
0 ignored issues
show
Bug introduced by
It seems like contains() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

48
            if ($this->/** @scrutinizer ignore-call */ contains($item)) {
Loading history...
49 2
                $result->add($item);
50
            }
51
        }
52 2
        return $result;
53
    }
54
}