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 |
||
15 | class ImmutableEquatableMap implements EquatableMap |
||
16 | { |
||
17 | /** |
||
18 | * @var Equatable[] |
||
19 | */ |
||
20 | private $items = []; |
||
21 | |||
22 | /** |
||
23 | * @param Equatable[] $items |
||
24 | */ |
||
25 | 126 | View Code Duplication | public function __construct(array $items = []) |
|
|||
26 | { |
||
27 | 126 | foreach ($items as $key => $value) { |
|
28 | 90 | if (!$value instanceof Equatable) { |
|
29 | 3 | throw InvalidArgumentException::invalidTypeInArray('items', Equatable::class, $value); |
|
30 | } |
||
31 | |||
32 | 87 | $this->items[$key] = $value; |
|
33 | 82 | } |
|
34 | 123 | } |
|
35 | |||
36 | 3 | public function __clone() |
|
37 | { |
||
38 | 3 | $items = []; |
|
39 | |||
40 | 3 | foreach ($this->items as $key => $item) { |
|
41 | 3 | $items[$key] = clone $item; |
|
42 | 2 | } |
|
43 | |||
44 | 3 | $this->items = $items; |
|
45 | 3 | } |
|
46 | |||
47 | /** |
||
48 | * @inheritdoc |
||
49 | */ |
||
50 | 15 | public function add($key, Equatable $value) |
|
51 | { |
||
52 | 15 | if ($this->containsKey($key)) { |
|
53 | 3 | throw InRangeException::keyInRange($key); |
|
54 | } |
||
55 | |||
56 | 9 | $items = $this->items; |
|
57 | |||
58 | 9 | $items[$key] = $value; |
|
59 | |||
60 | 9 | return new static($items); |
|
61 | } |
||
62 | |||
63 | /** |
||
64 | * @inheritdoc |
||
65 | */ |
||
66 | 9 | View Code Duplication | public function remove(Equatable $value) |
75 | |||
76 | /** |
||
77 | * @inheritdoc |
||
78 | */ |
||
79 | 12 | public function replace($key, Equatable $value) |
|
80 | { |
||
81 | 12 | if (!$this->containsKey($key)) { |
|
82 | 3 | throw OutOfRangeException::keyOutOfRange($key); |
|
83 | } |
||
84 | |||
85 | 6 | $items = $this->items; |
|
86 | |||
87 | 6 | $items[$key] = $value; |
|
88 | |||
89 | 6 | return new static($items); |
|
90 | } |
||
91 | |||
92 | /** |
||
93 | * @inheritdoc |
||
94 | */ |
||
95 | 36 | public function get($key) |
|
103 | |||
104 | /** |
||
105 | * @inheritdoc |
||
106 | */ |
||
107 | 21 | View Code Duplication | public function search(Equatable $value) |
117 | |||
118 | /** |
||
119 | * @inheritdoc |
||
120 | */ |
||
121 | 9 | View Code Duplication | public function searchAll(Equatable $value) |
137 | |||
138 | /** |
||
139 | * @inheritdoc |
||
140 | */ |
||
141 | 3 | public function contains(Equatable $value) |
|
150 | |||
151 | /** |
||
152 | * @inheritdoc |
||
153 | */ |
||
154 | 57 | public function containsKey($key) |
|
162 | |||
163 | /** |
||
164 | * @inheritdoc |
||
165 | */ |
||
166 | 18 | View Code Duplication | public function equals($other) |
184 | |||
185 | /** |
||
186 | * @inheritdoc |
||
187 | */ |
||
188 | 12 | public function getIterator() |
|
192 | |||
193 | /** |
||
194 | * @inheritdoc |
||
195 | */ |
||
196 | 42 | public function count() |
|
200 | |||
201 | /** |
||
202 | * @inheritdoc |
||
203 | */ |
||
204 | 12 | View Code Duplication | public function countItem(Equatable $value) |
216 | } |
||
217 |
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.