Passed
Push — main ( 6d01df...c28659 )
by Breno
02:03
created

CollectionTrait::indexOf()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace BrenoRoosevelt\Validation\DataStructure;
5
6
trait CollectionTrait
7
{
8
    protected array $elements = [];
9
10
    protected function insert(mixed $value, $key = null): void
11
    {
12
        if ($key !== null) {
13
            $this->elements[$key] = $value;
14
        } else {
15
            $this->elements[] = $value;
16
        }
17
    }
18
19
    protected function deleteElement(mixed $element): bool
20
    {
21
        $index = $this->indexOf($element);
22
        if ($index === false) {
23
            return false;
24
        }
25
26
        $this->elements = array_slice($this->elements, $index, 1, true);
0 ignored issues
show
Bug introduced by
It seems like $index can also be of type string; however, parameter $offset of array_slice() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

26
        $this->elements = array_slice($this->elements, /** @scrutinizer ignore-type */ $index, 1, true);
Loading history...
27
        return true;
28
    }
29
30
    protected function deleteByIndex($index): bool
31
    {
32
        if (!array_key_exists($index, $this->elements)) {
33
            return false;
34
        }
35
36
        unset($this->elements[$index]);
37
        return true;
38
    }
39
40
    public function indexOf(mixed $element): bool|int|string
41
    {
42
        return array_search($element, $this->elements, true);
43
    }
44
45
    public function clear(): static
46
    {
47
        $this->elements = [];
48
        return $this;
49
    }
50
51
    public function values(): array
52
    {
53
        return array_values($this->elements);
54
    }
55
56
    public function keys(): array
57
    {
58
        return array_keys($this->elements);
59
    }
60
61
    public function accept(callable $callback): static
62
    {
63
        foreach ($this->elements as $index => $element) {
64
            if (true !== call_user_func_array($callback, [$element, $index])) {
65
                unset($this->elements[$index]);
66
            }
67
        }
68
69
        return $this;
70
    }
71
72
    public function reject(callable $callback): static
73
    {
74
        foreach ($this->elements as $index => $element) {
75
            if (true === call_user_func_array($callback, [$element, $index])) {
76
                unset($this->elements[$index]);
77
            }
78
        }
79
80
        return $this;
81
    }
82
83
    public function forEach(callable $callback, bool $stopable = false): static
84
    {
85
        foreach ($this->elements as $index => $element) {
86
            $result = call_user_func_array($callback, [$element, $index]);
87
            if ($result === false && $stopable) {
88
                break;
89
            }
90
        }
91
92
        return $this;
93
    }
94
95
    public function length(): int
96
    {
97
        return count($this->elements);
98
    }
99
100
    public function isEmpty(): bool
101
    {
102
        return $this->length() === 0;
103
    }
104
105
    public function entries(): array
106
    {
107
        return $this->elements;
108
    }
109
}
110