Purging::delete()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Stratadox\ImmutableCollection;
4
5
use function array_filter;
6
use function in_array;
7
use Stratadox\Collection\Collection;
8
use Stratadox\Collection\Purgeable;
9
10
/**
11
 * Behaviour to allow "purging" the immutable collection.
12
 *
13
 * Provides access to purging behaviour in the form of methods that
14
 * return a modified copy of the original (immutable) collection.
15
 *
16
 * @package Stratadox\Collection
17
 * @author  Stratadox
18
 */
19
trait Purging
20
{
21
    /**
22
     * @see Purgeable::delete()
23
     * @param int $index
24
     * @return static
25
     */
26
    public function delete(int $index)
27
    {
28
        $items = $this->items();
29
        unset($items[$index]);
30
        return $this->newCopy($items);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->newCopy($items) returns the type Stratadox\Collection\Collection which is incompatible with the documented return type Stratadox\ImmutableCollection\Purging.
Loading history...
31
    }
32
33
    /**
34
     * @see Purgeable::remove()
35
     * @param mixed ...$itemsToRemove
36
     * @return static
37
     */
38
    public function remove(...$itemsToRemove)
39
    {
40
        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\Purging.
Loading history...
41
            $this->items(),
42
            function ($item) use ($itemsToRemove) {
43
                return !in_array($item, $itemsToRemove, true);
44
            }
45
        ));
46
    }
47
48
    /** @see Collection::items() */
49
    abstract public function items(): array;
50
51
    /**
52
     * @see ImmutableCollection::newCopy()
53
     * @param array $items
54
     * @return Collection|static
55
     */
56
    abstract protected function newCopy(array $items): Collection;
57
}