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 |
||
29 | trait GroupByTrait |
||
30 | { |
||
31 | /** |
||
32 | * {@inheritDoc} |
||
33 | */ |
||
34 | public function groupBy($col) |
||
42 | |||
43 | /** |
||
44 | * {@inheritDoc} |
||
45 | */ |
||
46 | public function groupByDesc($col) |
||
50 | |||
51 | /** |
||
52 | * {@inheritDoc} |
||
53 | */ |
||
54 | public function groupByTpl(/*# string */ $template, $col) |
||
58 | |||
59 | /** |
||
60 | * {@inheritDoc} |
||
61 | */ |
||
62 | public function groupByRaw(/*# string */ $groupby) |
||
66 | |||
67 | /** |
||
68 | * real group by |
||
69 | * @param string|string[]|Template $col column[s] |
||
70 | * @param string $suffix ''|ASC|DESC |
||
71 | * @param bool $rawMode |
||
72 | * @return $this |
||
73 | * @access protected |
||
74 | */ |
||
75 | View Code Duplication | protected function realGroupBy( |
|
92 | |||
93 | /** |
||
94 | * Multitple groupbys |
||
95 | * |
||
96 | * @param array $cols |
||
97 | * @param string $suffix |
||
98 | * @access protected |
||
99 | */ |
||
100 | protected function multipleGroupBy(array $cols, /*# string */ $suffix) |
||
106 | |||
107 | /** |
||
108 | * Build GROUP BY |
||
109 | * |
||
110 | * @param string $prefix |
||
111 | * @param array $settings |
||
112 | * @return string |
||
113 | * @access protected |
||
114 | */ |
||
115 | protected function buildGroupby( |
||
121 | |||
122 | abstract protected function isRaw($str, /*# bool */ $rawMode)/*# : bool */; |
||
130 | } |
||
131 |
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.