Complex classes like DbSqlStatement 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 DbSqlStatement, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class DbSqlStatement extends DbSqlAware { |
||
19 | |||
20 | const SELECT = 'SELECT'; |
||
21 | |||
22 | protected static $allowedOperations = array( |
||
23 | self::SELECT, |
||
24 | ); |
||
25 | |||
26 | public $operation = ''; |
||
27 | |||
28 | public $table = ''; |
||
29 | public $alias = ''; |
||
30 | |||
31 | public $idField = ''; |
||
32 | |||
33 | /** |
||
34 | * @var array |
||
35 | */ |
||
36 | public $fields = array(); |
||
37 | |||
38 | public $where = array(); |
||
39 | public $groupBy = array(); |
||
40 | public $orderBy = array(); |
||
41 | public $having = array(); |
||
42 | |||
43 | public $limit = 0; |
||
44 | public $offset = 0; |
||
45 | |||
46 | public $fetchOne = false; |
||
47 | public $forUpdate = false; |
||
48 | public $skipLock = false; |
||
49 | |||
50 | protected $_compiledQuery = array(); |
||
51 | |||
52 | /** |
||
53 | * @param string $fieldName |
||
54 | * |
||
55 | * @return $this |
||
56 | */ |
||
57 | public function setIdField($fieldName) { |
||
62 | |||
63 | /** |
||
64 | * @return self |
||
65 | */ |
||
66 | public function select() { |
||
74 | 1 | ||
75 | /** |
||
76 | 1 | * @param string $alias |
|
77 | * |
||
78 | * @return $this |
||
79 | */ |
||
80 | public function fromAlias($alias) { |
||
85 | 3 | ||
86 | 3 | /** |
|
87 | * @param string $tableName |
||
88 | * @param string $alias |
||
89 | * |
||
90 | * @return $this |
||
91 | */ |
||
92 | public function from($tableName, $alias = '') { |
||
98 | 1 | ||
99 | 1 | public function __call($name, $arguments) { |
|
114 | 1 | ||
115 | 1 | /** |
|
116 | * @param int $limit |
||
117 | 1 | * |
|
118 | * @return $this |
||
119 | */ |
||
120 | public function limit($limit) { |
||
125 | 2 | ||
126 | 2 | /** |
|
127 | * @param int $offset |
||
128 | 2 | * |
|
129 | * @return $this |
||
130 | */ |
||
131 | public function offset($offset) { |
||
136 | 1 | ||
137 | 1 | ||
138 | /** |
||
139 | 1 | * Make statement fetch only one record |
|
140 | * |
||
141 | * @return $this |
||
142 | */ |
||
143 | public function fetchOne($fetchOne = true) { |
||
148 | 2 | ||
149 | /** |
||
150 | 2 | * @return $this |
|
151 | */ |
||
152 | public function forUpdate($forUpdate = true) { |
||
157 | |||
158 | 2 | /** |
|
159 | 2 | * @return $this |
|
160 | */ |
||
161 | 2 | public function skipLock($skipLock = true) { |
|
166 | |||
167 | /** |
||
168 | * @param string $className |
||
169 | * |
||
170 | * @return $this |
||
171 | 2 | */ |
|
172 | 2 | public function getParamsFromStaticClass($className) { |
|
180 | |||
181 | /** |
||
182 | * @param db_mysql|null $db |
||
183 | 2 | * @param string $className |
|
184 | 2 | * |
|
185 | 2 | * @return static |
|
186 | 2 | */ |
|
187 | 2 | public static function build($db = null, $className = '') { |
|
198 | 3 | ||
199 | 3 | /** |
|
200 | 3 | * Resets statement |
|
201 | * |
||
202 | 3 | * @param bool $full |
|
203 | * |
||
204 | * @return static |
||
205 | 3 | */ |
|
206 | protected function _reset($full = true) { |
||
229 | |||
230 | 1 | ||
231 | 1 | /** |
|
232 | * @param array $array |
||
233 | 1 | * |
|
234 | * @return array |
||
235 | */ |
||
236 | protected function arrayEscape(&$array) { |
||
244 | |||
245 | protected function compileFrom() { |
||
251 | 2 | ||
252 | 2 | protected function compileJoin() { |
|
254 | |||
255 | protected function compileWhere() { |
||
259 | |||
260 | protected function compileGroupBy() { |
||
264 | |||
265 | protected function compileOrderBy() { |
||
269 | |||
270 | protected function compileHaving() { |
||
274 | |||
275 | protected function compileLimit() { |
||
281 | |||
282 | protected function compileForUpdate() { |
||
290 | |||
291 | /** |
||
292 | * @return string |
||
293 | * @throws ExceptionDbOperationEmpty |
||
294 | * @throws ExceptionDbOperationRestricted |
||
295 | */ |
||
296 | public function __toString() { |
||
321 | |||
322 | /** |
||
323 | * @param array|mixed $fields |
||
324 | * |
||
325 | * @return string |
||
326 | * @throws ExceptionDBFieldEmpty |
||
327 | */ |
||
328 | 13 | protected function selectFieldsToString($fields) { |
|
345 | |||
346 | /** |
||
347 | 9 | * @param mixed $fieldName |
|
348 | 9 | * |
|
349 | * @return string |
||
350 | */ |
||
351 | 9 | protected function processFieldString($fieldName) { |
|
365 | |||
366 | /** |
||
367 | * @param mixed $fieldName |
||
368 | * |
||
369 | * @return string |
||
370 | */ |
||
371 | protected function processField($fieldName) { |
||
387 | |||
388 | } |
||
389 |
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.