Completed
Push — master ( d6f0bd...41a21b )
by Ivannis Suárez
05:43
created

ArrayList   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 169
Duplicated Lines 28.4 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 26
c 1
b 0
f 0
lcom 1
cbo 8
dl 48
loc 169
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 4 1
A addAll() 0 8 2
A remove() 14 14 3
A removeAll() 13 13 3
A removeAt() 0 12 2
A contains() 11 11 3
A indexOf() 0 11 3
A sort() 10 10 2
A sorted() 0 4 1
A find() 0 4 1
A findOne() 0 4 1
A validateKey() 0 8 2
A offsetUnset() 0 4 1
A offsetSet() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
namespace Cubiche\Core\Collections\ArrayCollection;
11
12
use Cubiche\Core\Collections\DataSource\ArrayDataSource;
13
use Cubiche\Core\Collections\DataSourceList;
14
use Cubiche\Core\Collections\Exception\InvalidKeyException;
15
use Cubiche\Core\Comparable\Comparator;
16
use Cubiche\Core\Comparable\ComparatorInterface;
17
use Cubiche\Core\Specification\Criteria;
18
use Cubiche\Core\Specification\SpecificationInterface;
19
20
/**
21
 * ArrayList Class.
22
 *
23
 * @author Karel Osorio Ramírez <[email protected]>
24
 * @author Ivannis Suárez Jerez <[email protected]>
25
 */
26
class ArrayList extends ArrayCollection implements ArrayListInterface
27
{
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function add($element)
32
    {
33
        $this->elements[] = $element;
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function addAll($elements)
40
    {
41
        $this->validateTraversable($elements);
42
43
        foreach ($elements as $element) {
44
            $this->add($element);
45
        }
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 View Code Duplication
    public function remove($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...
52
    {
53
        $criteria = Criteria::same($element);
54
        foreach ($this->elements as $key => $value) {
55
            if ($criteria->evaluate($value)) {
56
                unset($this->elements[$key]);
57
                $this->elements = array_values($this->elements);
58
59
                return true;
60
            }
61
        }
62
63
        return false;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69 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...
70
    {
71
        $this->validateTraversable($elements);
72
73
        $changed = false;
74
        foreach ($elements as $element) {
75
            if ($this->remove($element)) {
76
                $changed = true;
77
            }
78
        }
79
80
        return $changed;
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function removeAt($key)
87
    {
88
        if ($this->hasKey($key)) {
89
            $removed = $this->elements[$key];
90
            unset($this->elements[$key]);
91
            $this->elements = array_values($this->elements);
92
93
            return $removed;
94
        }
95
96
        return;
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102 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...
103
    {
104
        $criteria = Criteria::same($element);
105
        foreach ($this->elements as $key => $value) {
106
            if ($criteria->evaluate($value)) {
107
                return true;
108
            }
109
        }
110
111
        return false;
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117
    public function indexOf($element)
118
    {
119
        $criteria = Criteria::same($element);
120
        foreach ($this->elements as $key => $value) {
121
            if ($criteria->evaluate($value)) {
122
                return $key;
123
            }
124
        }
125
126
        return -1;
127
    }
128
129
    /**
130
     * {@inheritdoc}
131
     */
132 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...
133
    {
134
        if ($criteria === null) {
135
            $criteria = new Comparator();
136
        }
137
138
        uasort($this->elements, function ($a, $b) use ($criteria) {
139
            return $criteria->compare($a, $b);
140
        });
141
    }
142
143
    /**
144
     * {@inheritdoc}
145
     */
146
    public function sorted(ComparatorInterface $criteria)
147
    {
148
        return new DataSourceList(new ArrayDataSource($this->elements, null, $criteria));
149
    }
150
151
    /**
152
     * {@inheritdoc}
153
     */
154
    public function find(SpecificationInterface $criteria)
155
    {
156
        return new DataSourceList(new ArrayDataSource($this->elements, $criteria));
157
    }
158
159
    /**
160
     * {@inheritdoc}
161
     */
162
    public function findOne(SpecificationInterface $criteria)
163
    {
164
        return (new ArrayDataSource($this->elements, $criteria))->findOne();
165
    }
166
167
    /**
168
     * {@inheritdoc}
169
     */
170
    protected function validateKey($key)
171
    {
172
        if (!is_int($key)) {
173
            throw InvalidKeyException::forKey($key, 'Expected a key of type integer. Got: %s');
174
        }
175
176
        return true;
177
    }
178
179
    /**
180
     * {@inheritdoc}
181
     */
182
    public function offsetUnset($offset)
183
    {
184
        return $this->removeAt($offset);
185
    }
186
187
    /**
188
     * {@inheritdoc}
189
     */
190
    public function offsetSet($offset, $value)
191
    {
192
        $this->add($value);
193
    }
194
}
195