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 |
||
30 | trait ColTrait |
||
31 | { |
||
32 | /** |
||
33 | * @var bool |
||
34 | * @access protected |
||
35 | */ |
||
36 | protected $is_distinct = false; |
||
37 | |||
38 | /** |
||
39 | * {@inheritDoc} |
||
40 | */ |
||
41 | public function col($col, /*# string */ $alias = '') |
||
45 | |||
46 | /** |
||
47 | * {@inheritDoc} |
||
48 | */ |
||
49 | public function distinct($col = '', /*# string */ $alias = '') |
||
54 | |||
55 | /** |
||
56 | * {@inheritDoc} |
||
57 | */ |
||
58 | public function count(/*# string */ $col, /*# string */ $alias = '') |
||
62 | |||
63 | /** |
||
64 | * {@inheritDoc} |
||
65 | */ |
||
66 | public function min(/*# string */ $col, /*# string */ $alias = '') |
||
70 | |||
71 | /** |
||
72 | * {@inheritDoc} |
||
73 | */ |
||
74 | public function max(/*# string */ $col, /*# string */ $alias = '') |
||
78 | |||
79 | /** |
||
80 | * {@inheritDoc} |
||
81 | */ |
||
82 | public function avg(/*# string */ $col, /*# string */ $alias = '') |
||
86 | |||
87 | /** |
||
88 | * {@inheritDoc} |
||
89 | */ |
||
90 | public function sum(/*# string */ $col, /*# string */ $alias = '') |
||
94 | |||
95 | /** |
||
96 | * {@inheritDoc} |
||
97 | */ |
||
98 | public function colTpl( |
||
105 | |||
106 | /** |
||
107 | * {@inheritDoc} |
||
108 | */ |
||
109 | public function colRaw($rawString, /*# string */ $alias = '') |
||
113 | |||
114 | /** |
||
115 | * @param mixed $col column/field specification[s] |
||
116 | * @param string $alias column alias name |
||
117 | * @param bool $rawMode raw mode |
||
118 | */ |
||
119 | protected function realCol( |
||
139 | |||
140 | /** |
||
141 | * from multiple tables |
||
142 | * |
||
143 | * @param array $cols |
||
144 | * @param bool $rawMode |
||
145 | * @access protected |
||
146 | */ |
||
147 | protected function multipleCol(array $cols, /*# bool */ $rawMode) |
||
157 | |||
158 | /** |
||
159 | * Build fields |
||
160 | * |
||
161 | * @param array $settings |
||
162 | * @return string |
||
163 | * @access protected |
||
164 | */ |
||
165 | protected function buildCol(array $settings)/*# : string */ |
||
182 | |||
183 | /** |
||
184 | * Build DISTINCT |
||
185 | * |
||
186 | * @param array $settings |
||
187 | * @return string |
||
188 | * @access protected |
||
189 | */ |
||
190 | protected function buildDistinct(array $settings)/*# : string */ |
||
194 | |||
195 | abstract protected function isRaw($str, /*# bool */ $rawMode)/*# : bool */; |
||
206 | } |
||
207 |
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.