Passed
Push — version-4 ( 844ef0...ae0658 )
by Sebastian
02:51 queued 11s
created

ListOperationsTrait   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 75%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 44
ccs 15
cts 20
cp 0.75
rs 10
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A plus() 0 5 1
A minus() 0 15 5
A intersect() 0 9 3
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
    public function plus(iterable $other): ListInterface
15
    {
16
        $list = listOf(...$this->array);
17
        $list->addAll($other);
18
        return $list;
19
    }
20
21
    /**
22
     * @inheritDoc
23
     */
24 3
    public function minus(iterable $values): ListInterface
25
    {
26 3
        $valuesList = emptyList();
27 3
        if (!$values instanceof ListInterface && is_array($values)) {
28
            $valuesList->setArray($values);
29
        } else {
30 3
            $valuesList = $values;
31
        }
32 3
        $newInstance = emptyList();
33 3
        foreach ($this->array as $value) {
34 3
            if (!$valuesList->contains($value)) {
35 3
                $newInstance->add($value);
36
            }
37
        }
38 3
        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
}