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 { |
||
19 | |||
20 | const SELECT = 'SELECT'; |
||
21 | |||
22 | protected static $allowedOperations = array( |
||
23 | self::SELECT, |
||
24 | ); |
||
25 | |||
26 | /** |
||
27 | * @var db_mysql $db |
||
28 | */ |
||
29 | protected $db; |
||
30 | |||
31 | public $operation = ''; |
||
32 | |||
33 | public $table = ''; |
||
34 | public $alias = ''; |
||
35 | |||
36 | public $idField = ''; |
||
37 | |||
38 | /** |
||
39 | * @var array |
||
40 | */ |
||
41 | public $fields = array(); |
||
42 | |||
43 | public $where = array(); |
||
44 | public $group = array(); |
||
45 | public $order = array(); |
||
46 | |||
47 | public $having = array(); |
||
48 | |||
49 | |||
50 | /** |
||
51 | * @var array |
||
52 | * [0] - row_count |
||
53 | * [1] - offset |
||
54 | 1 | * Used {LIMIT row_count [OFFSET offset]} syntax |
|
55 | 1 | */ |
|
56 | 1 | // TODO - separate offset and row_count |
|
57 | 1 | public $limit = 0; |
|
58 | 1 | public $offset = 0; |
|
59 | |||
60 | 1 | public $fetchOne = false; |
|
61 | public $forUpdate = false; |
||
62 | public $skipLock = false; |
||
63 | |||
64 | /** |
||
65 | * @param db_mysql|null $db |
||
66 | * @param string $className |
||
67 | * |
||
68 | 3 | * @return DbSqlStatement |
|
69 | 3 | */ |
|
70 | 3 | public static function build($db = null, $className = '') { |
|
78 | |||
79 | /** |
||
80 | 1 | * DbSqlStatement constructor. |
|
81 | 1 | * |
|
82 | 1 | * @param db_mysql|null $db |
|
83 | 1 | */ |
|
84 | 1 | public function __construct($db = null) { |
|
87 | |||
88 | 1 | /** |
|
89 | 1 | * Resets statement |
|
90 | 1 | * |
|
91 | 1 | * @param bool $full |
|
92 | 1 | * |
|
93 | 1 | * @return $this |
|
94 | */ |
||
95 | 1 | // TODO - UNITTEST |
|
96 | protected function _reset($full = true) { |
||
119 | |||
120 | /** |
||
121 | * @param string $fieldName |
||
122 | * |
||
123 | * @return $this |
||
124 | */ |
||
125 | public function setIdField($fieldName) { |
||
130 | 1 | ||
131 | /** |
||
132 | * @param string $alias |
||
133 | * |
||
134 | * @return $this |
||
135 | */ |
||
136 | public function fromAlias($alias) { |
||
141 | 2 | ||
142 | 2 | /** |
|
143 | * @param int $limit |
||
144 | 2 | * |
|
145 | * @return $this |
||
146 | */ |
||
147 | public function limit($limit) { |
||
152 | 2 | ||
153 | 2 | /** |
|
154 | 2 | * @param int $offset |
|
155 | * |
||
156 | 2 | * @return $this |
|
157 | */ |
||
158 | public function offset($offset) { |
||
163 | |||
164 | 1 | ||
165 | 1 | /** |
|
166 | * @param string $tableName |
||
167 | 1 | * @param string $alias |
|
168 | * |
||
169 | * @return $this |
||
170 | */ |
||
171 | public function from($tableName, $alias = '') { |
||
177 | |||
178 | /** |
||
179 | 1 | * @param string $params |
|
180 | 1 | * |
|
181 | * @return $this |
||
182 | */ |
||
183 | public function getParamsFromStaticClass($params) { |
||
191 | |||
192 | |||
193 | /** |
||
194 | * @return self |
||
195 | */ |
||
196 | public function select() { |
||
204 | |||
205 | public function __call($name, $arguments) { |
||
215 | |||
216 | /** |
||
217 | * Make statement fetch only one record |
||
218 | * |
||
219 | * @return $this |
||
220 | */ |
||
221 | public function fetchOne($fetchOne = true) { |
||
226 | |||
227 | /** |
||
228 | * @return $this |
||
229 | */ |
||
230 | public function forUpdate($forUpdate = true) { |
||
235 | 2 | ||
236 | 2 | /** |
|
237 | 1 | * @return $this |
|
238 | */ |
||
239 | public function skipLock($skipLock = true) { |
||
244 | |||
245 | |||
246 | /** |
||
247 | * @return string |
||
248 | * @throws ExceptionDbOperationEmpty |
||
249 | * @throws ExceptionDbOperationRestricted |
||
250 | */ |
||
251 | public function __toString() { |
||
298 | |||
299 | /** |
||
300 | * @param array|mixed $fields |
||
301 | * |
||
302 | 13 | * @return string |
|
303 | 13 | * @throws ExceptionDBFieldEmpty |
|
304 | 4 | */ |
|
305 | 13 | protected function selectFieldsToString($fields) { |
|
322 | 9 | ||
323 | /** |
||
324 | * @param mixed $fieldName |
||
325 | 9 | * |
|
326 | * @return string |
||
327 | 8 | */ |
|
328 | 9 | protected function processField($fieldName) { |
|
341 | |||
342 | /** |
||
343 | * @param mixed $fieldName |
||
344 | * |
||
345 | * @return string |
||
346 | */ |
||
347 | protected function processFieldDefault($fieldName) { |
||
364 | |||
365 | /** |
||
366 | * @param $string |
||
367 | * |
||
368 | * @return mixed|string |
||
369 | */ |
||
370 | protected function stringEscape($string) { |
||
376 | |||
377 | } |
||
378 |
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.