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 | trait GroupByTrait |
||
29 | { |
||
30 | /** |
||
31 | * {@inheritDoc} |
||
32 | */ |
||
33 | public function groupBy($col) |
||
37 | |||
38 | /** |
||
39 | * {@inheritDoc} |
||
40 | */ |
||
41 | public function groupByTpl(/*# string */ $template, $col) |
||
45 | |||
46 | /** |
||
47 | * {@inheritDoc} |
||
48 | */ |
||
49 | public function groupByRaw(/*# string */ $groupby) |
||
53 | |||
54 | /** |
||
55 | * @param string|string[] $col column[s] |
||
56 | * @param bool $rawMode |
||
57 | * @return $this |
||
58 | * @access protected |
||
59 | */ |
||
60 | View Code Duplication | protected function realGroupBy($col, /*# bool */ $rawMode = false) |
|
70 | |||
71 | /** |
||
72 | * Multitple groupbys |
||
73 | * |
||
74 | * @param array $cols |
||
75 | * @access protected |
||
76 | */ |
||
77 | protected function multipleGroupBy(array $cols) |
||
83 | |||
84 | /** |
||
85 | * Build GROUP BY |
||
86 | * |
||
87 | * @param array $settings |
||
88 | * @return string |
||
89 | * @access protected |
||
90 | */ |
||
91 | protected function buildGroupby(array $settings)/*# : string */ |
||
100 | |||
101 | abstract protected function isRaw($str, /*# bool */ $rawMode)/*# : bool */; |
||
111 | } |
||
112 |
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.