Complex classes like Query 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 Query, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | abstract class Query implements QueryInterface |
||
27 | { |
||
28 | const JOIN_TYPE_INNER = 'INNER'; |
||
29 | const JOIN_TYPE_LEFT = 'LEFT'; |
||
30 | |||
31 | /** |
||
32 | * @var string |
||
33 | */ |
||
34 | protected $alias; |
||
35 | |||
36 | /** |
||
37 | * @var string |
||
38 | */ |
||
39 | protected $modelNamespace; |
||
40 | |||
41 | /** |
||
42 | * @var TableMapInterface |
||
43 | */ |
||
44 | protected $tableMap; |
||
45 | |||
46 | /** |
||
47 | * @var array |
||
48 | */ |
||
49 | protected $clauses = []; |
||
50 | |||
51 | /** |
||
52 | * @var int |
||
53 | */ |
||
54 | protected $limit; |
||
55 | |||
56 | /** |
||
57 | * @var int |
||
58 | */ |
||
59 | protected $offset; |
||
60 | |||
61 | /** |
||
62 | * @var array |
||
63 | */ |
||
64 | protected $joins = []; |
||
65 | |||
66 | /** |
||
67 | * @var array All related tables that will inserted in the SELECT statement |
||
68 | */ |
||
69 | protected $with = []; |
||
70 | |||
71 | |||
72 | /** |
||
73 | * @param string $alias |
||
74 | * @param string $modelNamespace |
||
75 | */ |
||
76 | 15 | public function __construct($alias, $modelNamespace) |
|
85 | |||
86 | /** |
||
87 | * @param string $clause |
||
88 | * @param null|mixed $value Can't be an array |
||
89 | * |
||
90 | * @return $this |
||
91 | */ |
||
92 | 6 | public function where($clause, $value = null) |
|
98 | |||
99 | /** |
||
100 | * @param string $clause |
||
101 | * @param null|mixed $value |
||
102 | * |
||
103 | * @return $this|Query |
||
104 | * |
||
105 | * @throws \Exception |
||
106 | */ |
||
107 | 1 | public function orWhere($clause, $value = null) |
|
113 | |||
114 | /** |
||
115 | * @param string $clause |
||
116 | * @param mixed $value |
||
117 | * @param string $operator |
||
118 | */ |
||
119 | 6 | protected function doWhere($clause, $value, $operator) |
|
127 | |||
128 | /** |
||
129 | * @param int $limit |
||
130 | * @param null|int $offset |
||
131 | * |
||
132 | * @return $this|Query |
||
133 | */ |
||
134 | 4 | public function limit($limit, $offset = null) |
|
143 | |||
144 | /** |
||
145 | * @param \PDO $con |
||
146 | * |
||
147 | * @return RocketObject|null |
||
148 | */ |
||
149 | 1 | public function findOne(\PDO $con = null) |
|
161 | |||
162 | /** |
||
163 | * @param string $relation |
||
164 | * @param null|string $alias |
||
165 | * |
||
166 | * @return $this|Query |
||
167 | * |
||
168 | * @throws RelationNotFoundException |
||
169 | * @throws RelationAliasNotFoundException |
||
170 | */ |
||
171 | 3 | public function innerJoinWith($relation, $alias = null) |
|
175 | |||
176 | /** |
||
177 | * @param string $relation |
||
178 | * @param null|string $alias |
||
179 | * |
||
180 | * @return $this|Query |
||
181 | * |
||
182 | * @throws RelationNotFoundException |
||
183 | * @throws RelationAliasNotFoundException |
||
184 | */ |
||
185 | 1 | public function leftJoinWith($relation, $alias = null) |
|
189 | |||
190 | /** |
||
191 | * @param string $relation |
||
192 | * @param string $alias |
||
193 | * @param string $joinType |
||
194 | * @param bool $with |
||
195 | * |
||
196 | * @return $this|Query |
||
197 | * |
||
198 | * @throws RelationNotFoundException |
||
199 | */ |
||
200 | 4 | protected function join($relation, $alias, $joinType = self::JOIN_TYPE_INNER, $with = false) |
|
241 | |||
242 | /** |
||
243 | * @param string $relation |
||
244 | * @param string $alias |
||
245 | * @param string $joinType |
||
246 | * @param string $with |
||
247 | * |
||
248 | * @return $this|Query |
||
249 | * |
||
250 | * @throws RelationAliasNotFoundException |
||
251 | */ |
||
252 | 3 | protected function joinDeep($relation, $alias, $joinType, $with) |
|
274 | |||
275 | /** |
||
276 | * @param string $alias |
||
277 | * @param null|string $table |
||
278 | */ |
||
279 | 3 | protected function with($alias, $table = null) |
|
286 | |||
287 | /** |
||
288 | * @return string |
||
289 | */ |
||
290 | 2 | protected function buildRelationWith() |
|
305 | |||
306 | /** |
||
307 | * @return string |
||
308 | */ |
||
309 | 2 | protected function buildRelationClauses() |
|
331 | |||
332 | /** |
||
333 | * @return string |
||
334 | */ |
||
335 | 4 | protected function buildClauses() |
|
360 | |||
361 | /** |
||
362 | * @return string |
||
363 | */ |
||
364 | 7 | protected function buildLimit() |
|
377 | |||
378 | /** |
||
379 | * @param \PDOStatement $stmt |
||
380 | * |
||
381 | * @return array|RocketObject[] |
||
382 | */ |
||
383 | 3 | protected function hydrate(\PDOStatement $stmt) |
|
398 | |||
399 | /** |
||
400 | * @return string |
||
401 | */ |
||
402 | 6 | public function getSqlQuery() |
|
417 | |||
418 | /** |
||
419 | * Clear all values |
||
420 | */ |
||
421 | 3 | protected function clear() |
|
431 | |||
432 | /** |
||
433 | * @return TableMapInterface |
||
434 | */ |
||
435 | 6 | protected function getTableMap() |
|
443 | |||
444 | /** |
||
445 | * @return QueryHydratorInterface |
||
446 | */ |
||
447 | 1 | protected function getSimpleQueryHydrator() |
|
451 | |||
452 | /** |
||
453 | * @return QueryHydratorInterface |
||
454 | */ |
||
455 | 1 | protected function getComplexQueryHydrator() |
|
459 | |||
460 | /** |
||
461 | * @param \PDO $con |
||
462 | * |
||
463 | * @return mixed |
||
464 | */ |
||
465 | public abstract function find(\PDO $con = null); |
||
466 | |||
467 | /** |
||
468 | * @return string |
||
469 | */ |
||
470 | protected abstract function buildQuery(); |
||
471 | } |
||
472 |