Issues (9)

src/Object/Collection.php (1 issue)

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 *  This file is part of the Micro framework package.
7
 *
8
 *  (c) Stanislau Komar <[email protected]>
9
 *
10
 *  For the full copyright and license information, please view the LICENSE
11
 *  file that was distributed with this source code.
12
 */
13
14
namespace Micro\Library\DTO\Object;
15
16
/**
17
 * @template-implements \IteratorAggregate<mixed>
18
 * @template-implements \ArrayAccess<string, mixed>
19
 */
20
class Collection implements \ArrayAccess, \IteratorAggregate, \Countable
21
{
22
    /**
23
     * @var mixed[]
24
     */
25
    private array $items;
26
27 6
    public function __construct()
28
    {
29 6
        $this->items = [];
30
    }
31
32
    /**
33
     * @param mixed $item
34
     *
35
     * @return $this
36
     */
37 6
    public function add(mixed $item): self
38
    {
39 6
        $this->validateItem($item);
40
41 6
        $this->items[] = $item;
42
43 6
        return $this;
44
    }
45
46
    /**
47
     * @param mixed $item
48
     *
49
     * @return self
50
     */
51
    public function remove(mixed $item): self
52
    {
53
        foreach ($this as $pos => $currItem) {
54
            if ($currItem === $item) {
55
                $this->offsetUnset($pos);
56
57
                break;
58
            }
59
        }
60
61
        return $this;
62
    }
63
64
    /**
65
     * {@inheritDoc}
66
     */
67 6
    public function getIterator(): \Traversable
68
    {
69 6
        return (function () {
70 6
            foreach ($this->items as $item) {
71 6
                yield $item;
72
            }
73 6
        })();
74
    }
75
76
    /**
77
     * {@inheritDoc}
78
     */
79 1
    public function offsetExists(mixed $offset): bool
80
    {
81 1
        return isset($this->items[$offset]);
82
    }
83
84
    /**
85
     * {@inheritDoc}
86
     */
87 1
    public function offsetGet(mixed $offset): mixed
88
    {
89 1
        return $this->offsetExists($offset) ? $this->items[$offset] : null;
90
    }
91
92
    /**
93
     * {@inheritDoc}
94
     */
95
    public function offsetSet(mixed $offset, mixed $value): void
96
    {
97
        if (null === $offset) {
98
            throw new \InvalidArgumentException();
99
        }
100
101
        $this->items[$offset] = $value;
102
    }
103
104
    /**
105
     * {@inheritDoc}
106
     */
107
    public function offsetUnset(mixed $offset): void
108
    {
109
        unset($this->items[$offset]);
110
    }
111
112
    /**
113
     * {@inheritDoc}
114
     */
115
    public function count(): int
116
    {
117
        return \count($this->items);
118
    }
119
120 6
    protected function validateItem(mixed $item): void
0 ignored issues
show
The parameter $item is not used and could be removed. ( Ignorable by Annotation )

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

120
    protected function validateItem(/** @scrutinizer ignore-unused */ mixed $item): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
121
    {
122 6
    }
123
}
124