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:
Complex classes like Dictionary often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Dictionary, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class Dictionary implements DictionaryContract { |
||
16 | |||
17 | /** |
||
18 | * Dictionary storage array. |
||
19 | * |
||
20 | * @var array |
||
21 | */ |
||
22 | protected $storage = array(); |
||
23 | |||
24 | /** |
||
25 | * Key Type service. |
||
26 | * |
||
27 | * @var Type |
||
28 | */ |
||
29 | protected $key_type; |
||
30 | |||
31 | /** |
||
32 | * Value Type service. |
||
33 | * |
||
34 | * @var Type |
||
35 | */ |
||
36 | protected $val_type; |
||
37 | |||
38 | /** |
||
39 | * Where Dictionary is in loop. |
||
40 | * |
||
41 | * @var int |
||
42 | */ |
||
43 | protected $position = 0; |
||
44 | |||
45 | /** |
||
46 | * Dictionary keys. |
||
47 | * |
||
48 | * @var array |
||
49 | */ |
||
50 | protected $keys = array(); |
||
51 | |||
52 | /** |
||
53 | * Dictionary constructor. |
||
54 | * |
||
55 | * @param string $key_type |
||
56 | * @param string $val_type |
||
57 | * @param array $storage |
||
58 | * |
||
59 | * @throws InvalidArgumentException |
||
60 | */ |
||
61 | 105 | public function __construct( $key_type, $val_type, array $storage = array() ) { |
|
72 | |||
73 | /** |
||
74 | * {@inheritdoc} |
||
75 | * |
||
76 | * @return string |
||
77 | */ |
||
78 | 45 | public function get_key_type() { |
|
81 | |||
82 | /** |
||
83 | * {@inheritdoc} |
||
84 | * |
||
85 | * @return string |
||
86 | */ |
||
87 | 45 | public function get_value_type() { |
|
90 | |||
91 | /** |
||
92 | * {@inheritdoc} |
||
93 | * |
||
94 | * @param mixed $key Key to check. |
||
95 | * |
||
96 | * @return bool |
||
97 | */ |
||
98 | 36 | public function exists( $key ) { |
|
101 | |||
102 | /** |
||
103 | * {@inheritdoc} |
||
104 | * |
||
105 | * @param mixed $key Key to get. |
||
106 | * |
||
107 | * @return mixed|null |
||
108 | */ |
||
109 | 27 | public function get( $key ) { |
|
112 | |||
113 | /** |
||
114 | * {@inheritdoc} |
||
115 | * |
||
116 | * @param mixed $key Key to remove. |
||
117 | * |
||
118 | * @return DictionaryContract |
||
119 | */ |
||
120 | 3 | public function delete( $key ) { |
|
128 | |||
129 | /** |
||
130 | * {@inheritdoc} |
||
131 | * |
||
132 | * @param mixed $value Value to validate. |
||
133 | * |
||
134 | * @return bool |
||
135 | */ |
||
136 | 9 | public function value_exists( $value ) { |
|
139 | |||
140 | /** |
||
141 | * {@inheritdoc} |
||
142 | */ |
||
143 | 24 | public function count() { |
|
146 | |||
147 | /** |
||
148 | * {@inheritdoc} |
||
149 | */ |
||
150 | 3 | public function clear() { |
|
153 | |||
154 | /** |
||
155 | * {@inheritdoc} |
||
156 | * |
||
157 | * @return array |
||
158 | */ |
||
159 | 9 | public function to_array() { |
|
162 | |||
163 | /** |
||
164 | * {@inheritdoc} |
||
165 | * |
||
166 | * @param callable $condition Conditional callback. |
||
167 | * |
||
168 | * @return DictionaryContract |
||
169 | */ |
||
170 | 6 | public function filter( $condition ) { |
|
181 | |||
182 | /** |
||
183 | * {@inheritdoc} |
||
184 | * |
||
185 | * @param callable $condition Callback condition. |
||
186 | * |
||
187 | * @return DictionaryContract |
||
188 | */ |
||
189 | 2 | public function reject( $condition ) { |
|
194 | |||
195 | /** |
||
196 | * {@inheritdoc} |
||
197 | * |
||
198 | * @param mixed $key Key to add. |
||
199 | * @param mixed $value Value to add. |
||
200 | * |
||
201 | * @return DictionaryContract |
||
202 | */ |
||
203 | 21 | public function add( $key, $value ) { |
|
209 | |||
210 | /** |
||
211 | * {@inheritdoc} |
||
212 | * |
||
213 | * @param callable $callable Function to call. |
||
214 | */ |
||
215 | 3 | public function each( $callable ) { |
|
220 | |||
221 | /** |
||
222 | * {@inheritdoc} |
||
223 | * |
||
224 | * @param mixed $key Key to fetch. |
||
225 | * @param mixed $default Default to return if key is missing. |
||
226 | * |
||
227 | * @return mixed |
||
228 | */ |
||
229 | 3 | public function get_or_else( $key, $default ) { |
|
232 | |||
233 | /** |
||
234 | * {@inheritdoc} |
||
235 | */ |
||
236 | 3 | public function keys() { |
|
239 | |||
240 | /** |
||
241 | * {@inheritdoc} |
||
242 | */ |
||
243 | 3 | public function values() { |
|
246 | |||
247 | /** |
||
248 | * {@inheritdoc} |
||
249 | * |
||
250 | * @param callable $callable Function to call. |
||
251 | * |
||
252 | * @return DictionaryContract |
||
253 | */ |
||
254 | 6 | public function map( $callable ) { |
|
270 | |||
271 | /** |
||
272 | * {@inheritdoc} |
||
273 | * |
||
274 | * @param array|DictionaryContract $source Source to merge. |
||
275 | * |
||
276 | * @return DictionaryContract |
||
277 | * |
||
278 | * @throws InvalidArgumentException |
||
279 | */ |
||
280 | 12 | View Code Duplication | public function merge( $source ) { |
291 | |||
292 | |||
293 | /** |
||
294 | * {@inheritdoc} |
||
295 | * |
||
296 | * @param callable $callable |
||
297 | * |
||
298 | * @return bool |
||
299 | */ |
||
300 | 3 | public function contains( $callable ) { |
|
309 | |||
310 | /** |
||
311 | * {@inheritdoc} |
||
312 | * |
||
313 | * @param callable $callable |
||
314 | * @param mixed $initial |
||
315 | * |
||
316 | * @return mixed |
||
317 | */ |
||
318 | 3 | public function reduce( $callable, $initial ) { |
|
327 | |||
328 | /** |
||
329 | * Return the current element. |
||
330 | * |
||
331 | * @return mixed |
||
332 | */ |
||
333 | 3 | public function current() { |
|
337 | |||
338 | /** |
||
339 | * Move forward to next element. |
||
340 | */ |
||
341 | 3 | public function next() { |
|
344 | |||
345 | /** |
||
346 | * Return the key of the current element. |
||
347 | * |
||
348 | * @return mixed |
||
349 | */ |
||
350 | 3 | public function key() { |
|
353 | |||
354 | /** |
||
355 | * Checks if current position is valid. |
||
356 | * |
||
357 | * @return bool |
||
358 | */ |
||
359 | 3 | public function valid() { |
|
362 | |||
363 | /** |
||
364 | * Rewind the Iterator to the first element. |
||
365 | */ |
||
366 | 3 | public function rewind() { |
|
370 | |||
371 | /** |
||
372 | * {@inheritDoc} |
||
373 | * |
||
374 | * @return array |
||
375 | */ |
||
376 | public function serialize() { |
||
385 | } |
||
386 |
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.