Complex classes like SqlSelect 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 SqlSelect, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class SqlSelect extends SqlActions |
||
16 | { |
||
17 | /** |
||
18 | * @var string $returnType PHP Type used for return result |
||
19 | */ |
||
20 | protected $returnType = ''; |
||
21 | |||
22 | /** |
||
23 | * @var object $mainTable Informations about main table. Used for FROM part |
||
24 | */ |
||
25 | protected $mainTable; |
||
26 | |||
27 | /** |
||
28 | * @var array $subQueries All sub-queries |
||
29 | */ |
||
30 | protected $subQueries = []; |
||
31 | |||
32 | /** |
||
33 | * @var array $join List of all INNER JOIN |
||
34 | */ |
||
35 | protected $join = []; |
||
36 | |||
37 | /** |
||
38 | * @var array $joinLeft List of all LEFT JOIN |
||
39 | */ |
||
40 | protected $joinLeft = []; |
||
41 | |||
42 | /** |
||
43 | * @var array $joinRight List of all RIGHT JOIN |
||
44 | */ |
||
45 | protected $joinRight = []; |
||
46 | |||
47 | /** |
||
48 | * @var string[] $order All columns used for ORDER BY part |
||
49 | */ |
||
50 | protected $order = []; |
||
51 | |||
52 | /** |
||
53 | * @var string $limit The LIMIT part |
||
54 | */ |
||
55 | protected $limit = ''; |
||
56 | |||
57 | /** |
||
58 | * @var string[] $group The GROUP BY part |
||
59 | */ |
||
60 | protected $group = []; |
||
61 | |||
62 | /** |
||
63 | * Constructor |
||
64 | * |
||
65 | * @param \BfwSql\SqlConnect $sqlConnect Instance of SGBD connexion |
||
66 | * @param string $returnType PHP type used for return result |
||
67 | */ |
||
68 | public function __construct(SqlConnect $sqlConnect, $returnType) |
||
73 | |||
74 | /** |
||
75 | * Define object used to save informations about a table |
||
76 | * This object will be used to write query |
||
77 | * |
||
78 | * @param string|array $table Tables informations |
||
79 | * |
||
80 | * @return \stdClass |
||
81 | */ |
||
82 | protected function obtainTableInfos($table) |
||
105 | |||
106 | /** |
||
107 | * Add columns for select |
||
108 | * |
||
109 | * @param array|string $columns Columns to add |
||
110 | * @param string $tableName Table name for will be columns added |
||
111 | * |
||
112 | * @return void |
||
113 | */ |
||
114 | protected function addColumnsForSelect($columns, $tableName) |
||
148 | |||
149 | /** |
||
150 | * Declare information for FROM part and column will be get for main table |
||
151 | * |
||
152 | * @param string|array $table Table name. |
||
153 | * It can be an array if a table shortcut is declared. |
||
154 | * In array mode, the format is ['asValue' => 'tableName'] |
||
155 | * @param string|array $columns (default: "*") Columns will be get for |
||
156 | * the table declared in first argument |
||
157 | * |
||
158 | * @return \BfwSql\SqlSelect |
||
159 | */ |
||
160 | public function from($table, $columns = '*') |
||
173 | |||
174 | /** |
||
175 | * Add a sub-query in the SELECT part on the request |
||
176 | * |
||
177 | * @param \BfwSql\SqlActions|string $subRequest The sub-request |
||
178 | * @param string $shortcut The shortcut to use for |
||
179 | * this query in SELECT part |
||
180 | * |
||
181 | * @return \BfwSql\SqlSelect |
||
182 | */ |
||
183 | public function subQuery($subRequest, $shortcut) |
||
206 | |||
207 | /** |
||
208 | * Add a (inner|left|right) join to the request |
||
209 | * |
||
210 | * @param string $joinPropertyName The name of the property in this |
||
211 | * class where the join is add |
||
212 | * @param string|array $table Name of the table concerned by |
||
213 | * the join. Or an array with the table shortcut in key. |
||
214 | * @param string $joinOn SQL part "ON" for this join |
||
215 | * @param string|array $joinColumns Columns from the table joined to |
||
216 | * add in the SELECT part of the request |
||
217 | * |
||
218 | * @return \BfwSql\SqlSelect |
||
219 | */ |
||
220 | protected function createJoin( |
||
239 | |||
240 | /** |
||
241 | * Add a INNER JOIN to the request |
||
242 | * |
||
243 | * @param string|array $table Name of the table concerned by the |
||
244 | * join. Or an array with the table shortcut in key. |
||
245 | * @param string $joinOn SQL part "ON" for this join |
||
246 | * @param string|array $joinColumns Columns from the table joined to add |
||
247 | * in the SELECT part of the request |
||
248 | * |
||
249 | * @return \BfwSql\SqlSelect |
||
250 | */ |
||
251 | public function join($table, $joinOn, $joinColumns = '*') |
||
255 | |||
256 | /** |
||
257 | * Add a LEFT JOIN to the request |
||
258 | * |
||
259 | * @param string|array $table Name of the table concerned by the |
||
260 | * join. Or an array with the table shortcut in key. |
||
261 | * @param string $joinOn SQL part "ON" for this join |
||
262 | * @param string|array $joinColumns Columns from the table joined to add |
||
263 | * in the SELECT part of the request |
||
264 | * |
||
265 | * @return \BfwSql\SqlSelect |
||
266 | */ |
||
267 | public function joinLeft($table, $joinOn, $joinColumns = '*') |
||
271 | |||
272 | /** |
||
273 | * Add a RIGHT JOIN to the request |
||
274 | * |
||
275 | * @param string|array $table Name of the table concerned by the |
||
276 | * join. Or an array with the table shortcut in key. |
||
277 | * @param string $joinOn SQL part "ON" for this join |
||
278 | * @param string|array $joinColumns Columns from the table joined to add |
||
279 | * in the SELECT part of the request |
||
280 | * |
||
281 | * @return \BfwSql\SqlSelect |
||
282 | */ |
||
283 | public function joinRight($table, $joinOn, $joinColumns = '*') |
||
287 | |||
288 | /** |
||
289 | * Add a order condition to the request for the ORDER BY part |
||
290 | * |
||
291 | * @param string $condition The new condition |
||
292 | * |
||
293 | * @return \BfwSql\SqlSelect |
||
294 | */ |
||
295 | public function order($condition) |
||
300 | |||
301 | /** |
||
302 | * Add information about the LIMIT part in request |
||
303 | * |
||
304 | * @param array|integer $limit If it's a integer, the number of row to |
||
305 | * return. If an array, the format is [numberToStart, numberOfRowToReturn] |
||
306 | * |
||
307 | * @return \BfwSql\SqlSelect |
||
308 | */ |
||
309 | public function limit($limit) |
||
323 | |||
324 | /** |
||
325 | * Add a GROUP BY part to the request |
||
326 | * |
||
327 | * @param string $condition The condition to use in GROUP BY |
||
328 | * |
||
329 | * @return \BfwSql\SqlSelect |
||
330 | */ |
||
331 | public function group($condition) |
||
336 | |||
337 | /** |
||
338 | * Return the PDO constant for the returnType declared |
||
339 | * |
||
340 | * @return integer |
||
341 | */ |
||
342 | protected function obtainPdoFetchType() |
||
350 | |||
351 | /** |
||
352 | * Fetch one row of the result |
||
353 | * |
||
354 | * @return mixed |
||
355 | */ |
||
356 | public function fetchRow() |
||
361 | |||
362 | /** |
||
363 | * Fetch all rows returned by the request |
||
364 | * |
||
365 | * @return generator |
||
366 | */ |
||
367 | public function fetchAll() |
||
375 | |||
376 | /** |
||
377 | * {@inheritdoc} |
||
378 | */ |
||
379 | protected function assembleRequest() |
||
393 | |||
394 | /** |
||
395 | * Write the SELECT part of the request |
||
396 | * |
||
397 | * @return string |
||
398 | */ |
||
399 | protected function generateSelect() |
||
424 | |||
425 | /** |
||
426 | * Write the FROM part of the request |
||
427 | * |
||
428 | * @return string |
||
429 | */ |
||
430 | protected function generateFrom() |
||
440 | |||
441 | /** |
||
442 | * Write a (inner|left|right) join in the request |
||
443 | * |
||
444 | * @param string $joinProperty The join property name |
||
445 | * |
||
446 | * @return string |
||
447 | */ |
||
448 | protected function generateJoin($joinProperty) |
||
474 | |||
475 | /** |
||
476 | * Write the ORDER BY part for the request |
||
477 | * |
||
478 | * @return string |
||
479 | */ |
||
480 | protected function generateOrderBy() |
||
497 | |||
498 | /** |
||
499 | * Write the GRUOP BY part for the request |
||
500 | * |
||
501 | * @return string |
||
502 | */ |
||
503 | protected function generateGroupBy() |
||
520 | |||
521 | /** |
||
522 | * Write the LIMIT part for the request |
||
523 | * |
||
524 | * @return string |
||
525 | */ |
||
526 | protected function generateLimit() |
||
535 | } |
||
536 |