ListOperationsTrait::plus()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
1
<?php
2
/*
3
 * Copyright (C) 2022 Sebastian Böttger <[email protected]>
4
 * You may use, distribute and modify this code under the
5
 * terms of the MIT license.
6
 *
7
 * You should have received a copy of the MIT license with
8
 * this file. If not, please visit: https://opensource.org/licenses/mit-license.php
9
 */
10
11
namespace Seboettg\Collection\Lists\ListFeatures;
12
13
use Seboettg\Collection\Lists\ListInterface;
14
use function Seboettg\Collection\Lists\emptyList;
15
use function Seboettg\Collection\Lists\listOf;
16
17
/**
18
 * @property array $array
19
 */
20
trait ListOperationsTrait
21
{
22
    /**
23
     * @inheritDoc
24
     */
25 2
    public function plus(iterable $other): ListInterface
26
    {
27 2
        $list = listOf(...$this->array);
28 2
        $list->addAll($other);
29 2
        return $list;
30
    }
31
32
    /**
33
     * @inheritDoc
34
     */
35 4
    public function minus(iterable $values): ListInterface
36
    {
37 4
        $valuesList = emptyList();
38 4
        if (!$values instanceof ListInterface && is_array($values)) {
39 1
            $valuesList->setArray($values);
40
        } else {
41 3
            $valuesList = $values;
42
        }
43 4
        $newInstance = emptyList();
44 4
        foreach ($this->array as $value) {
45 4
            if (!$valuesList->contains($value)) {
46 4
                $newInstance->add($value);
47
            }
48
        }
49 4
        return $newInstance;
50
    }
51
52
    /**
53
     * @inheritDoc
54
     */
55 2
    public function intersect(ListInterface $list): ListInterface
56
    {
57 2
        $result = emptyList();
58 2
        foreach ($list as $item) {
59 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

59
            if ($this->/** @scrutinizer ignore-call */ contains($item)) {
Loading history...
60 2
                $result->add($item);
61
            }
62
        }
63 2
        return $result;
64
    }
65
}
66