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 SelectAbstract 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 SelectAbstract, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | abstract class SelectAbstract extends StatementAbstract implements SelectInterface{ |
||
19 | use WhereTrait; |
||
20 | |||
21 | /** |
||
22 | * @var bool |
||
23 | */ |
||
24 | protected $distinct = false; |
||
25 | |||
26 | /** |
||
27 | * @var array |
||
28 | */ |
||
29 | protected $cols = []; |
||
30 | |||
31 | /** |
||
32 | * @var array |
||
33 | */ |
||
34 | protected $from = []; |
||
35 | |||
36 | /** |
||
37 | * @var array |
||
38 | */ |
||
39 | protected $orderby = []; |
||
40 | |||
41 | /** |
||
42 | * @var array |
||
43 | */ |
||
44 | protected $groupby = []; |
||
45 | |||
46 | |||
47 | /** |
||
48 | * @param $val1 |
||
49 | * @param $val2 |
||
50 | * @param string $operator |
||
51 | * @param bool $bind |
||
52 | * @param string $join |
||
53 | * |
||
54 | * @return \chillerlan\Database\Query\SelectInterface |
||
55 | */ |
||
56 | public function where($val1, $val2, $operator = '=', $bind = true, $join = 'AND'):SelectInterface{ |
||
59 | |||
60 | /** |
||
61 | * @param null $join |
||
62 | * |
||
63 | * @return \chillerlan\Database\Query\SelectInterface |
||
64 | */ |
||
65 | public function openBracket($join = null):SelectInterface{ |
||
68 | |||
69 | /** |
||
70 | * @return \chillerlan\Database\Query\SelectInterface |
||
71 | */ |
||
72 | public function closeBracket():SelectInterface{ |
||
75 | |||
76 | /** |
||
77 | * @return \chillerlan\Database\Query\SelectInterface |
||
78 | */ |
||
79 | public function distinct():SelectInterface{ |
||
84 | |||
85 | /** |
||
86 | * @param $expr1 |
||
87 | * @param null $expr2 |
||
88 | * @param null $func |
||
89 | * |
||
90 | * @return void |
||
91 | */ |
||
92 | protected function addColumn($expr1, $expr2 = null, $func = null){ |
||
108 | |||
109 | /** |
||
110 | * @param array $expressions |
||
111 | * |
||
112 | * @return \chillerlan\Database\Query\SelectInterface |
||
113 | */ |
||
114 | public function cols(array $expressions):SelectInterface{ |
||
133 | |||
134 | /** |
||
135 | * @param string $table |
||
136 | * @param string|null $ref |
||
137 | */ |
||
138 | protected function _addFrom(string $table, string $ref = null){ |
||
148 | |||
149 | /** |
||
150 | * @param array $expressions |
||
151 | * |
||
152 | * @return \chillerlan\Database\Query\SelectInterface |
||
153 | */ |
||
154 | public function from(array $expressions):SelectInterface{ |
||
176 | |||
177 | /** |
||
178 | * @param int $limit |
||
179 | * |
||
180 | * @return \chillerlan\Database\Query\SelectInterface |
||
181 | */ |
||
182 | public function limit(int $limit):SelectInterface{ |
||
187 | |||
188 | /** |
||
189 | * @param int $offset |
||
190 | * |
||
191 | * @return \chillerlan\Database\Query\SelectInterface |
||
192 | */ |
||
193 | public function offset(int $offset):SelectInterface{ |
||
198 | |||
199 | /** |
||
200 | * @param array $expressions |
||
201 | * |
||
202 | * @return \chillerlan\Database\Query\SelectInterface |
||
203 | */ |
||
204 | public function orderBy(array $expressions):SelectInterface{ |
||
236 | |||
237 | /** |
||
238 | * @param array $expressions |
||
239 | * |
||
240 | * @return \chillerlan\Database\Query\SelectInterface |
||
241 | */ |
||
242 | public function groupBy(array $expressions):SelectInterface{ |
||
250 | |||
251 | /** |
||
252 | * @todo |
||
253 | * |
||
254 | * @return int |
||
255 | * @throws \chillerlan\Database\Query\QueryException |
||
256 | */ |
||
257 | public function count():int{ |
||
280 | |||
281 | } |
||
282 |
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.