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 |
||
12 | trait DoubleEndedQueueTrait |
||
13 | { |
||
14 | /** |
||
15 | * Pop a specified value off the end of array. |
||
16 | * |
||
17 | * @return mixed The popped element |
||
18 | */ |
||
19 | 4 | public function pop() |
|
23 | |||
24 | /** |
||
25 | * Push one or more values onto the end of array at once. |
||
26 | * |
||
27 | * @return $this The current array with pushed elements |
||
28 | * to the end of array |
||
29 | */ |
||
30 | 4 | View Code Duplication | public function push(/* variadic arguments allowed */) |
39 | |||
40 | /** |
||
41 | * Shifts a specified value off the beginning of array. |
||
42 | * |
||
43 | * @return mixed A shifted element |
||
44 | */ |
||
45 | 4 | public function shift() |
|
49 | |||
50 | /** |
||
51 | * Prepends one or more values to the beginning of array at once. |
||
52 | * |
||
53 | * @return $this The current array with prepended elements to the beginning of array |
||
54 | */ |
||
55 | 4 | View Code Duplication | public function unshift(/* variadic arguments allowed */) |
64 | } |
||
65 |
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.