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 |
||
15 | abstract class SelectAbstract extends StatementAbstract implements SelectInterface{ |
||
16 | |||
17 | protected $limit; |
||
18 | protected $offset; |
||
19 | protected $distinct = false; |
||
20 | |||
21 | protected $cols = []; |
||
22 | protected $from = []; |
||
23 | protected $where = []; |
||
24 | protected $orderby = []; |
||
25 | protected $groupby = []; |
||
26 | |||
27 | /** |
||
28 | * @return string |
||
29 | */ |
||
30 | public function sql():string{ |
||
47 | |||
48 | /** |
||
49 | * @return \chillerlan\Database\Query\SelectInterface |
||
50 | */ |
||
51 | public function distinct():SelectInterface{ |
||
56 | |||
57 | /** |
||
58 | * @param $expr1 |
||
59 | * @param null $expr2 |
||
60 | * @param null $func |
||
61 | * |
||
62 | * @return void |
||
63 | */ |
||
64 | protected function addColumn($expr1, $expr2 = null, $func = null){ |
||
80 | |||
81 | /** |
||
82 | * @param array $expressions |
||
83 | * |
||
84 | * @return \chillerlan\Database\Query\SelectInterface |
||
85 | */ |
||
86 | public function cols(array $expressions):SelectInterface{ |
||
105 | |||
106 | /** |
||
107 | * @param string $table |
||
108 | * @param string|null $ref |
||
109 | */ |
||
110 | protected function addFrom(string $table, string $ref = null){ |
||
119 | |||
120 | /** |
||
121 | * @param array $expressions |
||
122 | * |
||
123 | * @return \chillerlan\Database\Query\SelectInterface |
||
124 | */ |
||
125 | public function from(array $expressions):SelectInterface{ |
||
147 | |||
148 | protected function addWhere($val1, $val2, $operator = '=', $bind = false, $and_or = 'AND'){ |
||
151 | |||
152 | public function where():SelectInterface{ |
||
155 | |||
156 | public function join():SelectInterface{ |
||
159 | |||
160 | public function having():SelectInterface{ |
||
163 | |||
164 | /** |
||
165 | * @param int $limit |
||
166 | * |
||
167 | * @return \chillerlan\Database\Query\SelectInterface |
||
168 | */ |
||
169 | public function limit(int $limit):SelectInterface{ |
||
174 | |||
175 | /** |
||
176 | * @param int $offset |
||
177 | * |
||
178 | * @return \chillerlan\Database\Query\SelectInterface |
||
179 | */ |
||
180 | public function offset(int $offset):SelectInterface{ |
||
185 | |||
186 | /** |
||
187 | * @param array $expressions |
||
188 | * |
||
189 | * @return \chillerlan\Database\Query\SelectInterface |
||
190 | */ |
||
191 | public function orderby(array $expressions):SelectInterface{ |
||
213 | |||
214 | public function groupBy(array $expressions):SelectInterface{ |
||
222 | |||
223 | public function union():SelectInterface{ |
||
226 | |||
227 | } |
||
228 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.