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 |
||
28 | class ArrayStore implements SortableStore, CountableStore |
||
29 | { |
||
30 | /** |
||
31 | * Flag: Enable serialization. |
||
32 | */ |
||
33 | const SERIALIZE = 1; |
||
34 | |||
35 | /** |
||
36 | * @var array |
||
37 | */ |
||
38 | private $array = array(); |
||
39 | |||
40 | /** |
||
41 | * @var callable |
||
42 | */ |
||
43 | private $serialize; |
||
44 | |||
45 | /** |
||
46 | * @var callable |
||
47 | */ |
||
48 | private $unserialize; |
||
49 | |||
50 | /** |
||
51 | * Creates a new store. |
||
52 | * |
||
53 | * @param array $array The values to set initially in the store. |
||
54 | */ |
||
55 | 192 | public function __construct(array $array = array(), $flags = 0) |
|
79 | |||
80 | /** |
||
81 | * {@inheritdoc} |
||
82 | */ |
||
83 | 132 | public function set($key, $value) |
|
89 | |||
90 | /** |
||
91 | * {@inheritdoc} |
||
92 | */ |
||
93 | 12 | View Code Duplication | public function get($key, $default = null) |
103 | |||
104 | /** |
||
105 | * {@inheritdoc} |
||
106 | */ |
||
107 | 60 | View Code Duplication | public function getOrFail($key) |
117 | |||
118 | /** |
||
119 | * {@inheritdoc} |
||
120 | */ |
||
121 | 80 | public function getMultiple(array $keys, $default = null) |
|
135 | |||
136 | /** |
||
137 | * {@inheritdoc} |
||
138 | */ |
||
139 | 60 | public function getMultipleOrFail(array $keys) |
|
151 | |||
152 | /** |
||
153 | * {@inheritdoc} |
||
154 | */ |
||
155 | 20 | public function remove($key) |
|
165 | |||
166 | /** |
||
167 | * {@inheritdoc} |
||
168 | */ |
||
169 | 60 | public function exists($key) |
|
175 | |||
176 | /** |
||
177 | * {@inheritdoc} |
||
178 | */ |
||
179 | 186 | public function clear() |
|
183 | |||
184 | /** |
||
185 | * {@inheritdoc} |
||
186 | */ |
||
187 | 24 | public function keys() |
|
191 | |||
192 | /** |
||
193 | * Returns the contents of the store as array. |
||
194 | * |
||
195 | * @return array The keys and values in the store. |
||
196 | */ |
||
197 | public function toArray() |
||
201 | |||
202 | /** |
||
203 | * {@inheritdoc} |
||
204 | */ |
||
205 | 12 | public function sort($flags = SORT_REGULAR) |
|
209 | |||
210 | /** |
||
211 | * {@inheritdoc} |
||
212 | */ |
||
213 | 2 | public function count() |
|
217 | } |
||
218 |
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.