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 |
||
25 | class ArrayHashMap extends ArrayCollection implements ArrayHashMapInterface |
||
26 | { |
||
27 | /** |
||
28 | * ArrayHashMap constructor. |
||
29 | * |
||
30 | * @param array $elements |
||
31 | */ |
||
32 | public function __construct(array $elements = array()) |
||
38 | |||
39 | /** |
||
40 | * {@inheritdoc} |
||
41 | */ |
||
42 | public function get($key) |
||
50 | |||
51 | /** |
||
52 | * {@inheritdoc} |
||
53 | */ |
||
54 | public function set($key, $value) |
||
60 | |||
61 | /** |
||
62 | * {@inheritdoc} |
||
63 | */ |
||
64 | public function containsKey($key) |
||
68 | |||
69 | /** |
||
70 | * {@inheritdoc} |
||
71 | */ |
||
72 | public function containsValue($value) |
||
76 | |||
77 | /** |
||
78 | * {@inheritdoc} |
||
79 | */ |
||
80 | public function removeAt($key) |
||
91 | |||
92 | /** |
||
93 | * {@inheritdoc} |
||
94 | */ |
||
95 | public function find(SpecificationInterface $criteria) |
||
99 | |||
100 | /** |
||
101 | * {@inheritdoc} |
||
102 | */ |
||
103 | public function keys() |
||
107 | |||
108 | /** |
||
109 | * {@inheritdoc} |
||
110 | */ |
||
111 | public function values() |
||
115 | |||
116 | /** |
||
117 | * {@inheritdoc} |
||
118 | */ |
||
119 | View Code Duplication | public function sort(ComparatorInterface $criteria = null) |
|
129 | |||
130 | /** |
||
131 | * @param ComparatorInterface $criteria |
||
132 | * |
||
133 | * @return CollectionInterface |
||
134 | */ |
||
135 | public function sorted(ComparatorInterface $criteria) |
||
139 | |||
140 | /** |
||
141 | * {@inheritdoc} |
||
142 | */ |
||
143 | public function offsetUnset($offset) |
||
147 | } |
||
148 |
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.