ArrayList::indexOf()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 1
1
<?php
2
/**
3
 * This file is part of the Cubiche package.
4
 *
5
 * Copyright (c) Cubiche
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Cubiche\Core\Collections\ArrayCollection;
12
13
use Cubiche\Core\Collections\DataSource\ArrayDataSource;
14
use Cubiche\Core\Collections\DataSourceList;
15
use Cubiche\Core\Collections\Exception\InvalidKeyException;
16
use Cubiche\Core\Comparable\Comparator;
17
use Cubiche\Core\Comparable\ComparatorInterface;
18
use Cubiche\Core\Specification\Criteria;
19
use Cubiche\Core\Specification\SpecificationInterface;
20
21
/**
22
 * ArrayList Class.
23
 *
24
 * @author Karel Osorio Ramírez <[email protected]>
25
 * @author Ivannis Suárez Jerez <[email protected]>
26
 */
27
class ArrayList extends ArrayCollection implements ArrayListInterface
28
{
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function add($element)
33
    {
34
        $this->elements[] = $element;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function addAll($elements)
41
    {
42
        $this->validateTraversable($elements);
43
44
        foreach ($elements as $element) {
45
            $this->add($element);
46
        }
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function remove($element)
53
    {
54
        $criteria = Criteria::eq($element);
55
        foreach ($this->elements as $key => $value) {
56
            if ($criteria->evaluate($value)) {
57
                unset($this->elements[$key]);
58
                $this->elements = array_values($this->elements);
59
60
                return true;
61
            }
62
        }
63
64
        return false;
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 View Code Duplication
    public function removeAll($elements)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71
    {
72
        $this->validateTraversable($elements);
73
74
        $changed = false;
75
        foreach ($elements as $element) {
76
            if ($this->remove($element)) {
77
                $changed = true;
78
            }
79
        }
80
81
        return $changed;
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    public function removeAt($key)
88
    {
89
        if ($this->hasKey($key)) {
90
            $removed = $this->elements[$key];
91
            unset($this->elements[$key]);
92
            $this->elements = array_values($this->elements);
93
94
            return $removed;
95
        }
96
97
        return;
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103 View Code Duplication
    public function contains($element)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
104
    {
105
        $criteria = Criteria::eq($element);
106
        foreach ($this->elements as $key => $value) {
107
            if ($criteria->evaluate($value)) {
108
                return true;
109
            }
110
        }
111
112
        return false;
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118
    public function indexOf($element)
119
    {
120
        $criteria = Criteria::eq($element);
121
        foreach ($this->elements as $key => $value) {
122
            if ($criteria->evaluate($value)) {
123
                return $key;
124
            }
125
        }
126
127
        return -1;
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133 View Code Duplication
    public function sort(ComparatorInterface $criteria = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
134
    {
135
        if ($criteria === null) {
136
            $criteria = new Comparator();
137
        }
138
139
        uasort($this->elements, function ($a, $b) use ($criteria) {
140
            return $criteria->compare($a, $b);
141
        });
142
    }
143
144
    /**
145
     * {@inheritdoc}
146
     */
147
    public function sorted(ComparatorInterface $criteria)
148
    {
149
        return new DataSourceList(new ArrayDataSource($this->elements, null, $criteria));
150
    }
151
152
    /**
153
     * {@inheritdoc}
154
     */
155
    public function find(SpecificationInterface $criteria)
156
    {
157
        return new DataSourceList(new ArrayDataSource($this->elements, $criteria));
158
    }
159
160
    /**
161
     * {@inheritdoc}
162
     */
163
    public function findOne(SpecificationInterface $criteria)
164
    {
165
        return (new ArrayDataSource($this->elements, $criteria))->findOne();
166
    }
167
168
    /**
169
     * {@inheritdoc}
170
     */
171
    protected function validateKey($key)
172
    {
173
        if (!is_int($key)) {
174
            throw InvalidKeyException::forKey($key, 'Expected a key of type integer. Got: %s');
175
        }
176
177
        return true;
178
    }
179
180
    /**
181
     * {@inheritdoc}
182
     */
183
    public function offsetUnset($offset)
184
    {
185
        return $this->removeAt($offset);
186
    }
187
188
    /**
189
     * {@inheritdoc}
190
     */
191
    public function offsetSet($offset, $value)
192
    {
193
        $this->add($value);
194
    }
195
}
196