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

ArraySet   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 158
Duplicated Lines 30.38 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 25
c 1
b 0
f 0
lcom 1
cbo 9
dl 48
loc 158
rs 10

13 Methods

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

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