Complex classes like QueryBuilder 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 QueryBuilder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class QueryBuilder |
||
11 | { |
||
12 | /** |
||
13 | * @var TableFactory $tableFactory |
||
14 | */ |
||
15 | private $tableFactory; |
||
16 | |||
17 | /** |
||
18 | * @var CompilerManager $compilerManager |
||
19 | */ |
||
20 | private $compilerManager; |
||
21 | |||
22 | /** |
||
23 | * @var array $selectClauses |
||
24 | */ |
||
25 | private $selectClauses; |
||
26 | |||
27 | /** |
||
28 | * @var array $whereClauses |
||
29 | */ |
||
30 | private $whereClauses; |
||
31 | |||
32 | /** |
||
33 | * @var array $orderByClauses |
||
34 | */ |
||
35 | private $orderByClauses; |
||
36 | |||
37 | /** |
||
38 | * @var array $setClauses |
||
39 | */ |
||
40 | private $setClauses; |
||
41 | |||
42 | /** |
||
43 | * @var array $groupByClauses |
||
44 | */ |
||
45 | private $groupByClauses; |
||
46 | |||
47 | /** |
||
48 | * @var array $havingClauses |
||
49 | */ |
||
50 | private $havingClauses; |
||
51 | |||
52 | /** |
||
53 | * @var AbstractTable[] $tables |
||
54 | */ |
||
55 | private $tables = []; |
||
56 | |||
57 | /** |
||
58 | * @var bool $highPriority |
||
59 | */ |
||
60 | private $highPriority; |
||
61 | |||
62 | /** |
||
63 | * @var int $maxStatementTime |
||
64 | */ |
||
65 | private $maxStatementTime; |
||
66 | |||
67 | /** |
||
68 | * @var bool $straightJoin |
||
69 | */ |
||
70 | private $straightJoin; |
||
71 | |||
72 | /** |
||
73 | * @var bool $sqlSmallResult |
||
74 | */ |
||
75 | private $sqlSmallResult; |
||
76 | |||
77 | /** |
||
78 | * @var bool $sqlBigResult |
||
79 | */ |
||
80 | private $sqlBigResult; |
||
81 | |||
82 | /** |
||
83 | * @var bool $sqlBufferResult |
||
84 | */ |
||
85 | private $sqlBufferResult; |
||
86 | |||
87 | /** |
||
88 | * @var bool $sqlNoCache |
||
89 | */ |
||
90 | private $sqlNoCache; |
||
91 | |||
92 | /** |
||
93 | * @var bool $sqlCalcFoundRows |
||
94 | */ |
||
95 | private $sqlCalcFoundRows; |
||
96 | |||
97 | /** |
||
98 | * @param TableFactory $tableFactory |
||
99 | * @param CompilerManager $compilerManager |
||
100 | */ |
||
101 | public function __construct(TableFactory $tableFactory = null, CompilerManager $compilerManager = null) |
||
112 | |||
113 | /** |
||
114 | * @return ExprBuilder |
||
115 | */ |
||
116 | public function expr() |
||
120 | |||
121 | /** |
||
122 | * @return $this |
||
123 | */ |
||
124 | public function addSelect() |
||
130 | |||
131 | /** |
||
132 | * @return $this |
||
133 | */ |
||
134 | public function andWhere() |
||
140 | |||
141 | /** |
||
142 | * @return $this |
||
143 | */ |
||
144 | public function andHaving() |
||
150 | |||
151 | /** |
||
152 | * @param $table |
||
153 | * |
||
154 | * @return AbstractTable |
||
155 | */ |
||
156 | private function addRootTable($table) |
||
165 | |||
166 | /** |
||
167 | * @param mixed $table |
||
168 | * |
||
169 | * @return AbstractTable |
||
170 | */ |
||
171 | public function from($table) |
||
175 | |||
176 | /** |
||
177 | * @param mixed $table |
||
178 | * |
||
179 | * @return AbstractTable |
||
180 | */ |
||
181 | public function addFrom($table) |
||
185 | |||
186 | /** |
||
187 | * @param mixed $table |
||
188 | * |
||
189 | * @return AbstractTable |
||
190 | */ |
||
191 | public function update($table) |
||
195 | |||
196 | /** |
||
197 | * @param string $joinType |
||
198 | * @param mixed $table |
||
199 | * |
||
200 | * @return AbstractTable |
||
201 | */ |
||
202 | public function join($joinType, $table) |
||
211 | |||
212 | /** |
||
213 | * @param mixed $table |
||
214 | * |
||
215 | * @return AbstractTable |
||
216 | */ |
||
217 | public function crossJoin($table) |
||
221 | |||
222 | /** |
||
223 | * @param mixed $table |
||
224 | * |
||
225 | * @return AbstractTable |
||
226 | */ |
||
227 | public function leftJoin($table) |
||
231 | |||
232 | /** |
||
233 | * @param string $table |
||
234 | * |
||
235 | * @return AbstractTable |
||
236 | */ |
||
237 | public function innerJoin($table) |
||
241 | |||
242 | /** |
||
243 | * @return $this |
||
244 | */ |
||
245 | public function addOrderBy() |
||
251 | |||
252 | /** |
||
253 | * @return $this |
||
254 | */ |
||
255 | public function addSet() |
||
261 | |||
262 | /** |
||
263 | * @return $this |
||
264 | */ |
||
265 | public function addGroupBy() |
||
271 | |||
272 | /** |
||
273 | * @return string |
||
274 | */ |
||
275 | public function buildSQL() |
||
279 | |||
280 | /** |
||
281 | * @return Query |
||
282 | */ |
||
283 | public function buildQuery() |
||
287 | |||
288 | /** |
||
289 | * @return array |
||
290 | */ |
||
291 | public function getSelectClauses() |
||
295 | |||
296 | /** |
||
297 | * @return array |
||
298 | */ |
||
299 | public function getWhereClauses() |
||
303 | |||
304 | /** |
||
305 | * @return array |
||
306 | */ |
||
307 | public function getOrderByClauses() |
||
311 | |||
312 | /** |
||
313 | * @return array |
||
314 | */ |
||
315 | public function getSetClauses() |
||
319 | |||
320 | /** |
||
321 | * @return array |
||
322 | */ |
||
323 | public function getGroupByClauses() |
||
327 | |||
328 | /** |
||
329 | * @return array |
||
330 | */ |
||
331 | public function getHavingClauses() |
||
335 | |||
336 | /** |
||
337 | * @return AbstractTable[] |
||
338 | */ |
||
339 | public function getTables() |
||
343 | |||
344 | /** |
||
345 | * @return AbstractTable[] |
||
346 | */ |
||
347 | public function getRootTables() |
||
353 | |||
354 | /** |
||
355 | * @return AbstractTable[] |
||
356 | */ |
||
357 | public function getJoinTables() |
||
363 | |||
364 | /** |
||
365 | * @return boolean |
||
366 | */ |
||
367 | public function isHighPriority() |
||
371 | |||
372 | /** |
||
373 | * @param boolean $highPriority |
||
374 | * |
||
375 | * @return $this |
||
376 | */ |
||
377 | public function setHighPriority($highPriority) |
||
383 | |||
384 | /** |
||
385 | * @return int |
||
386 | */ |
||
387 | public function getMaxStatementTime() |
||
391 | |||
392 | /** |
||
393 | * @param integer $maxStatementTime |
||
394 | * |
||
395 | * @return $this |
||
396 | */ |
||
397 | public function setMaxStatementTime($maxStatementTime) |
||
403 | |||
404 | /** |
||
405 | * @return boolean |
||
406 | */ |
||
407 | public function isStraightJoin() |
||
411 | |||
412 | /** |
||
413 | * @param boolean $straightJoin |
||
414 | * |
||
415 | * @return $this |
||
416 | */ |
||
417 | public function setStraightJoin($straightJoin) |
||
423 | |||
424 | /** |
||
425 | * @return boolean |
||
426 | */ |
||
427 | public function isSqlSmallResult() |
||
431 | |||
432 | /** |
||
433 | * @param boolean $sqlSmallResult |
||
434 | * |
||
435 | * @return $this |
||
436 | */ |
||
437 | public function setSqlSmallResult($sqlSmallResult) |
||
443 | |||
444 | /** |
||
445 | * @return boolean |
||
446 | */ |
||
447 | public function isSqlBigResult() |
||
451 | |||
452 | /** |
||
453 | * @param boolean $sqlBigResult |
||
454 | * |
||
455 | * @return $this |
||
456 | */ |
||
457 | public function setSqlBigResult($sqlBigResult) |
||
463 | |||
464 | /** |
||
465 | * @return boolean |
||
466 | */ |
||
467 | public function isSqlBufferResult() |
||
471 | |||
472 | /** |
||
473 | * @param boolean $sqlBufferResult |
||
474 | * |
||
475 | * @return $this |
||
476 | */ |
||
477 | public function setSqlBufferResult($sqlBufferResult) |
||
483 | |||
484 | /** |
||
485 | * @return boolean |
||
486 | */ |
||
487 | public function isSqlNoCache() |
||
491 | |||
492 | /** |
||
493 | * @param boolean $sqlNoCache |
||
494 | * |
||
495 | * @return $this |
||
496 | */ |
||
497 | public function setSqlNoCache($sqlNoCache) |
||
503 | |||
504 | /** |
||
505 | * @return boolean |
||
506 | */ |
||
507 | public function isSqlCalcFoundRows() |
||
511 | |||
512 | /** |
||
513 | * @param boolean $sqlCalcFoundRows |
||
514 | * |
||
515 | * @return $this |
||
516 | */ |
||
517 | public function setSqlCalcFoundRows($sqlCalcFoundRows) |
||
523 | } |