Complex classes like TSQLQueryBuilderBasic 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 TSQLQueryBuilderBasic, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | trait TSQLQueryBuilderBasic |
||
10 | { |
||
11 | /** |
||
12 | * Properties |
||
13 | */ |
||
14 | private $dialect; // SQL dialect for a certain database |
||
15 | private $sql; // The query built |
||
16 | private $prefix; // Prefix to attach to all table names |
||
17 | |||
18 | private $columns; // Columns to select |
||
19 | private $from; // From part |
||
20 | private $join; // Join part |
||
21 | private $where; // Where part |
||
22 | private $groupby; // Group by part |
||
23 | private $orderby; // Order by part |
||
24 | private $limit; // Limit by part |
||
25 | private $offset; // Offset by part |
||
26 | |||
27 | |||
28 | |||
29 | |||
30 | /** |
||
31 | * Get SQL. |
||
32 | * |
||
33 | * @return string as sql-query |
||
34 | */ |
||
35 | 25 | public function getSQL() |
|
42 | |||
43 | |||
44 | |||
45 | /** |
||
46 | * Build SQL. |
||
47 | * |
||
48 | * @return string as SQL query |
||
49 | */ |
||
50 | 16 | protected function build() |
|
65 | |||
66 | |||
67 | |||
68 | /** |
||
69 | * Create a inner or outer join. |
||
70 | * |
||
71 | * @param string $table name of table. |
||
72 | * @param string $condition to join. |
||
73 | * @param string $type what type of join to create. |
||
74 | * |
||
75 | * @return void |
||
76 | */ |
||
77 | 3 | private function createJoin($table, $condition, $type) |
|
84 | |||
85 | |||
86 | |||
87 | /** |
||
88 | * Set database type to consider when generating SQL. |
||
89 | * |
||
90 | * @param string $dialect representing database type. |
||
91 | * |
||
92 | * @return void |
||
93 | */ |
||
94 | 9 | public function setSQLDialect($dialect) |
|
98 | |||
99 | |||
100 | |||
101 | /** |
||
102 | * Set a table prefix. |
||
103 | * |
||
104 | * @param string $prefix to use in front of all tables. |
||
105 | * |
||
106 | * @return void |
||
107 | */ |
||
108 | 18 | public function setTablePrefix($prefix) |
|
112 | |||
113 | |||
114 | |||
115 | /** |
||
116 | * Utility to check if array is associative array. |
||
117 | * |
||
118 | * http://stackoverflow.com/questions/173400/php-arrays-a-good-way-to-check-if-an-array-is-associative-or-sequential/4254008#4254008 |
||
119 | * |
||
120 | * @param array $array input array to check. |
||
121 | * |
||
122 | * @return boolean true if array is associative array with at least one key, else false. |
||
123 | * |
||
124 | */ |
||
125 | 4 | private function isAssoc($array) |
|
129 | |||
130 | |||
131 | |||
132 | /** |
||
133 | * Create a table. |
||
134 | * |
||
135 | * @param string $name the table name. |
||
136 | * @param array $columns the columns in the table. |
||
137 | * |
||
138 | * @return $this |
||
139 | */ |
||
140 | 2 | public function createTable($name, $columns) |
|
162 | |||
163 | |||
164 | |||
165 | /** |
||
166 | * Drop a table. |
||
167 | * |
||
168 | * @param string $name the table name. |
||
169 | * |
||
170 | * @return $this |
||
171 | */ |
||
172 | 2 | public function dropTable($name) |
|
181 | |||
182 | |||
183 | |||
184 | /** |
||
185 | * Drop a table if it exists. |
||
186 | * |
||
187 | * @param string $name the table name. |
||
188 | * |
||
189 | * @return $this |
||
190 | */ |
||
191 | 1 | public function dropTableIfExists($name) |
|
200 | |||
201 | |||
202 | |||
203 | /** |
||
204 | * Create a proper column value arrays from incoming $columns and $values. |
||
205 | * |
||
206 | * @param array $columns |
||
207 | * @param array|null $values |
||
208 | * |
||
209 | * @return array that can be parsed with list($columns, $values) |
||
210 | */ |
||
211 | 9 | public function mapColumnsWithValues($columns, $values) |
|
234 | |||
235 | |||
236 | |||
237 | /** |
||
238 | * Build a insert-query. |
||
239 | * |
||
240 | * @param string $table the table name. |
||
241 | * @param array $columns to insert och key=>value with columns and values. |
||
242 | * @param array $values to insert or empty if $columns has both columns and values. |
||
243 | * |
||
244 | * @return void |
||
245 | */ |
||
246 | 6 | public function insert($table, $columns, $values = null) |
|
286 | |||
287 | |||
288 | |||
289 | /** |
||
290 | * Build an update-query. |
||
291 | * |
||
292 | * @param string $table the table name. |
||
293 | * @param array $columns to update or key=>value with columns and values. |
||
294 | * @param array $values to update or empty if $columns has bot columns and values. |
||
295 | * @param array $where limit which rows are updated. |
||
296 | * |
||
297 | * @return void |
||
298 | */ |
||
299 | 3 | public function update($table, $columns, $values = null, $where = null) |
|
341 | |||
342 | |||
343 | |||
344 | /** |
||
345 | * Build a delete-query. |
||
346 | * |
||
347 | * @param string $table the table name. |
||
348 | * @param array $where limit which rows are updated. |
||
349 | * |
||
350 | * @return void |
||
351 | */ |
||
352 | 2 | public function delete($table, $where = null) |
|
364 | |||
365 | |||
366 | |||
367 | /** |
||
368 | * Clear all previous sql-code. |
||
369 | * |
||
370 | * @return void |
||
371 | */ |
||
372 | 16 | protected function clear() |
|
384 | |||
385 | |||
386 | |||
387 | /** |
||
388 | * Build a select-query. |
||
389 | * |
||
390 | * @param string $columns which columns to select. |
||
391 | * |
||
392 | * @return $this |
||
393 | */ |
||
394 | 16 | public function select($columns = '*') |
|
401 | |||
402 | |||
403 | |||
404 | /** |
||
405 | * Build the from part. |
||
406 | * |
||
407 | * @param string $table name of table. |
||
408 | * |
||
409 | * @return $this |
||
410 | */ |
||
411 | 16 | public function from($table) |
|
417 | |||
418 | |||
419 | |||
420 | /** |
||
421 | * Build the inner join part. |
||
422 | * |
||
423 | * @param string $table name of table. |
||
424 | * @param string $condition to join. |
||
425 | * |
||
426 | * @return $this |
||
427 | */ |
||
428 | 1 | public function join($table, $condition) |
|
433 | |||
434 | |||
435 | |||
436 | /** |
||
437 | * Build the right join part. |
||
438 | * |
||
439 | * @param string $table name of table. |
||
440 | * @param string $condition to join. |
||
441 | * |
||
442 | * @return $this |
||
443 | */ |
||
444 | 1 | public function rightJoin($table, $condition) |
|
448 | |||
449 | |||
450 | |||
451 | /** |
||
452 | * Build the left join part. |
||
453 | * |
||
454 | * @param string $table name of table. |
||
455 | * @param string $condition to join. |
||
456 | * |
||
457 | * @return $this |
||
458 | */ |
||
459 | 1 | public function leftJoin($table, $condition) |
|
463 | |||
464 | |||
465 | |||
466 | /** |
||
467 | * Build the where part. |
||
468 | * |
||
469 | * @param string $condition for building the where part of the query. |
||
470 | * |
||
471 | * @return $this |
||
472 | */ |
||
473 | 1 | public function where($condition) |
|
479 | |||
480 | |||
481 | |||
482 | /** |
||
483 | * Build the where part with conditions. |
||
484 | * |
||
485 | * @param string $condition for building the where part of the query. |
||
486 | * |
||
487 | * @return $this |
||
488 | */ |
||
489 | 1 | public function andWhere($condition) |
|
495 | |||
496 | |||
497 | |||
498 | /** |
||
499 | * Build the group by part. |
||
500 | * |
||
501 | * @param string $condition for building the group by part of the query. |
||
502 | * |
||
503 | * @return $this |
||
504 | */ |
||
505 | 1 | public function groupBy($condition) |
|
511 | |||
512 | |||
513 | |||
514 | /** |
||
515 | * Build the order by part. |
||
516 | * |
||
517 | * @param string $condition for building the where part of the query. |
||
518 | * |
||
519 | * @return $this |
||
520 | */ |
||
521 | 1 | public function orderBy($condition) |
|
527 | |||
528 | |||
529 | |||
530 | /** |
||
531 | * Build the LIMIT by part. |
||
532 | * |
||
533 | * @param string $condition for building the LIMIT part of the query. |
||
534 | * |
||
535 | * @return $this |
||
536 | */ |
||
537 | 1 | public function limit($condition) |
|
543 | |||
544 | |||
545 | |||
546 | /** |
||
547 | * Build the OFFSET by part. |
||
548 | * |
||
549 | * @param string $condition for building the OFFSET part of the query. |
||
550 | * |
||
551 | * @return $this |
||
552 | */ |
||
553 | 1 | public function offset($condition) |
|
559 | } |
||
560 |