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 namespace Arcanedev\LaravelNestedSet\Eloquent; |
||
18 | class QueryBuilder extends Builder |
||
19 | { |
||
20 | /* ------------------------------------------------------------------------------------------------ |
||
21 | | Properties |
||
22 | | ------------------------------------------------------------------------------------------------ |
||
23 | */ |
||
24 | /** |
||
25 | * @var \Arcanedev\LaravelNestedSet\Traits\NodeTrait |
||
26 | */ |
||
27 | protected $model; |
||
28 | |||
29 | /* ------------------------------------------------------------------------------------------------ |
||
30 | | Main Functions |
||
31 | | ------------------------------------------------------------------------------------------------ |
||
32 | */ |
||
33 | /** |
||
34 | * Get node's `lft` and `rgt` values. |
||
35 | * |
||
36 | * @param mixed $id |
||
37 | * @param bool $required |
||
38 | * |
||
39 | * @return array |
||
40 | */ |
||
41 | 76 | public function getNodeData($id, $required = false) |
|
58 | |||
59 | /** |
||
60 | * Get plain node data. |
||
61 | * |
||
62 | * @param mixed $id |
||
63 | * @param bool $required |
||
64 | * |
||
65 | * @return array |
||
66 | */ |
||
67 | 48 | public function getPlainNodeData($id, $required = false) |
|
71 | |||
72 | /** |
||
73 | * Scope limits query to select just root node. |
||
74 | * |
||
75 | * @return self |
||
76 | */ |
||
77 | 20 | public function whereIsRoot() |
|
83 | |||
84 | /** |
||
85 | * Limit results to ancestors of specified node. |
||
86 | * |
||
87 | * @param mixed $id |
||
88 | * |
||
89 | * @return self |
||
90 | */ |
||
91 | 20 | public function whereAncestorOf($id) |
|
124 | |||
125 | /** |
||
126 | * Get ancestors of specified node. |
||
127 | * |
||
128 | * @param mixed $id |
||
129 | * @param array $columns |
||
130 | * |
||
131 | * @return self |
||
132 | */ |
||
133 | 12 | public function ancestorsOf($id, array $columns = ['*']) |
|
137 | |||
138 | /** |
||
139 | * Add node selection statement between specified range. |
||
140 | * |
||
141 | * @param array $values |
||
142 | * @param string $boolean |
||
143 | * @param bool $not |
||
144 | * |
||
145 | * @return self |
||
146 | */ |
||
147 | 44 | public function whereNodeBetween($values, $boolean = 'and', $not = false) |
|
153 | |||
154 | /** |
||
155 | * Add node selection statement between specified range joined with `or` operator. |
||
156 | * |
||
157 | * @param array $values |
||
158 | * |
||
159 | * @return self |
||
160 | */ |
||
161 | public function orWhereNodeBetween($values) |
||
165 | |||
166 | /** |
||
167 | * Add constraint statement to descendants of specified node. |
||
168 | * |
||
169 | * @param mixed $id |
||
170 | * @param string $boolean |
||
171 | * @param bool $not |
||
172 | * |
||
173 | * @return self |
||
174 | */ |
||
175 | 48 | public function whereDescendantOf($id, $boolean = 'and', $not = false) |
|
186 | |||
187 | /** |
||
188 | * @param mixed $id |
||
189 | * |
||
190 | * @return self |
||
191 | */ |
||
192 | public function whereNotDescendantOf($id) |
||
196 | |||
197 | /** |
||
198 | * @param mixed $id |
||
199 | * |
||
200 | * @return self |
||
201 | */ |
||
202 | 4 | public function orWhereDescendantOf($id) |
|
206 | |||
207 | /** |
||
208 | * @param mixed $id |
||
209 | * |
||
210 | * @return self |
||
211 | */ |
||
212 | public function orWhereNotDescendantOf($id) |
||
216 | |||
217 | /** |
||
218 | * Get descendants of specified node. |
||
219 | * |
||
220 | * @param mixed $id |
||
221 | * @param array $columns |
||
222 | * |
||
223 | * @return \Arcanedev\LaravelNestedSet\Eloquent\Collection |
||
224 | */ |
||
225 | public function descendantsOf($id, array $columns = ['*']) |
||
234 | |||
235 | /** |
||
236 | * @param mixed $id |
||
237 | * @param string $operator |
||
238 | * @param string $boolean |
||
239 | * |
||
240 | * @return self |
||
241 | */ |
||
242 | protected function whereIsBeforeOrAfter($id, $operator, $boolean) |
||
243 | { |
||
244 | if (NestedSet::isNode($id)) { |
||
245 | $value = '?'; |
||
246 | |||
247 | $this->query->addBinding($id->getLft()); |
||
248 | } else { |
||
249 | $valueQuery = $this->model |
||
250 | ->newQuery() |
||
251 | ->toBase() |
||
252 | ->select('_n.'.$this->model->getLftName()) |
||
253 | ->from($this->model->getTable().' as _n') |
||
254 | ->where('_n.'.$this->model->getKeyName(), '=', $id); |
||
255 | |||
256 | $this->query->mergeBindings($valueQuery); |
||
257 | |||
258 | $value = '('.$valueQuery->toSql().')'; |
||
259 | } |
||
260 | |||
261 | list($lft,) = $this->wrappedColumns(); |
||
262 | |||
263 | $this->query->whereRaw("{$lft} {$operator} {$value}", [ ], $boolean); |
||
264 | |||
265 | return $this; |
||
266 | } |
||
267 | |||
268 | /** |
||
269 | * Constraint nodes to those that are after specified node. |
||
270 | * |
||
271 | * @param mixed $id |
||
272 | * @param string $boolean |
||
273 | * |
||
274 | * @return self |
||
275 | */ |
||
276 | public function whereIsAfter($id, $boolean = 'and') |
||
277 | { |
||
278 | return $this->whereIsBeforeOrAfter($id, '>', $boolean); |
||
279 | } |
||
280 | |||
281 | /** |
||
282 | * Constraint nodes to those that are before specified node. |
||
283 | * |
||
284 | * @param mixed $id |
||
285 | * @param string $boolean |
||
286 | * |
||
287 | * @return self |
||
288 | */ |
||
289 | public function whereIsBefore($id, $boolean = 'and') |
||
290 | { |
||
291 | return $this->whereIsBeforeOrAfter($id, '<', $boolean); |
||
292 | } |
||
293 | |||
294 | /** |
||
295 | * Include depth level into the result. |
||
296 | * |
||
297 | * @param string $as |
||
298 | * |
||
299 | * @return self |
||
300 | */ |
||
301 | 16 | public function withDepth($as = 'depth') |
|
322 | |||
323 | /** |
||
324 | * Get wrapped `lft` and `rgt` column names. |
||
325 | * |
||
326 | * @return array |
||
327 | */ |
||
328 | 48 | protected function wrappedColumns() |
|
337 | |||
338 | /** |
||
339 | * Get a wrapped table name. |
||
340 | * |
||
341 | * @return string |
||
342 | */ |
||
343 | 28 | protected function wrappedTable() |
|
347 | |||
348 | /** |
||
349 | * Wrap model's key name. |
||
350 | * |
||
351 | * @return string |
||
352 | */ |
||
353 | 12 | protected function wrappedKey() |
|
357 | |||
358 | /** |
||
359 | * Exclude root node from the result. |
||
360 | * |
||
361 | * @return self |
||
362 | */ |
||
363 | 8 | public function withoutRoot() |
|
369 | |||
370 | /** |
||
371 | * Order by node position. |
||
372 | * |
||
373 | * @param string $dir |
||
374 | * |
||
375 | * @return self |
||
376 | */ |
||
377 | 52 | public function defaultOrder($dir = 'asc') |
|
385 | |||
386 | /** |
||
387 | * Order by reversed node position. |
||
388 | * |
||
389 | * @return $this |
||
390 | */ |
||
391 | 4 | public function reversed() |
|
395 | |||
396 | /** |
||
397 | * Move a node to the new position. |
||
398 | * |
||
399 | * @param mixed $key |
||
400 | * @param int $position |
||
401 | * |
||
402 | * @return int |
||
403 | */ |
||
404 | 40 | public function moveNode($key, $position) |
|
444 | |||
445 | /** |
||
446 | * Make or remove gap in the tree. Negative height will remove gap. |
||
447 | * |
||
448 | * @param int $cut |
||
449 | * @param int $height |
||
450 | * |
||
451 | * @return int |
||
452 | */ |
||
453 | 48 | public function makeGap($cut, $height) |
|
464 | |||
465 | /** |
||
466 | * Get patch for columns. |
||
467 | * |
||
468 | * @param array $params |
||
469 | * |
||
470 | * @return array |
||
471 | */ |
||
472 | 84 | protected function patch(array $params) |
|
483 | |||
484 | /** |
||
485 | * Get patch for single column. |
||
486 | * |
||
487 | * @param string $col |
||
488 | * @param array $params |
||
489 | * |
||
490 | * @return string |
||
491 | */ |
||
492 | 84 | protected function columnPatch($col, array $params) |
|
521 | |||
522 | /** |
||
523 | * Get statistics of errors of the tree. |
||
524 | * |
||
525 | * @return array |
||
526 | */ |
||
527 | 12 | public function countErrors() |
|
547 | |||
548 | /** |
||
549 | * @return \Illuminate\Database\Query\Builder |
||
550 | */ |
||
551 | 12 | protected function getOddnessQuery() |
|
563 | |||
564 | /** |
||
565 | * @return \Illuminate\Database\Query\Builder |
||
566 | */ |
||
567 | 12 | protected function getDuplicatesQuery() |
|
587 | |||
588 | /** |
||
589 | * @return \Illuminate\Database\Query\Builder |
||
590 | */ |
||
591 | 12 | protected function getWrongParentQuery() |
|
616 | |||
617 | /** |
||
618 | * @return \Illuminate\Database\Query\Builder |
||
619 | */ |
||
620 | 12 | protected function getMissingParentQuery() |
|
644 | |||
645 | /** |
||
646 | * Get the number of total errors of the tree. |
||
647 | * |
||
648 | * @return int |
||
649 | */ |
||
650 | 8 | public function getTotalErrors() |
|
654 | |||
655 | /** |
||
656 | * Get whether the tree is broken. |
||
657 | * |
||
658 | * @return bool |
||
659 | */ |
||
660 | 8 | public function isBroken() |
|
664 | |||
665 | /** |
||
666 | * Fixes the tree based on parentage info. |
||
667 | * Nodes with invalid parent are saved as roots. |
||
668 | * |
||
669 | * @return int The number of fixed nodes |
||
670 | */ |
||
671 | 4 | public function fixTree() |
|
687 | |||
688 | /** |
||
689 | * Rebuild the tree based on raw data. |
||
690 | * If item data does not contain primary key, new node will be created. |
||
691 | * |
||
692 | * @param array $data |
||
693 | * @param bool $delete Whether to delete nodes that exists but not in the data array |
||
694 | * |
||
695 | * @return int |
||
696 | */ |
||
697 | 8 | public function rebuildTree(array $data, $delete = false) |
|
719 | |||
720 | /** |
||
721 | * @param array $dictionary |
||
722 | * @param array $data |
||
723 | * @param array $existing |
||
724 | * @param mixed $parentId |
||
725 | */ |
||
726 | 8 | protected function buildRebuildDictionary( |
|
762 | |||
763 | /** |
||
764 | * @param string|null $table |
||
765 | * |
||
766 | * @return self |
||
767 | */ |
||
768 | public function applyNestedSetScope($table = null) |
||
772 | |||
773 | /** |
||
774 | * Get the root node. |
||
775 | * |
||
776 | * @param array $columns |
||
777 | * |
||
778 | * @return self |
||
779 | */ |
||
780 | 16 | public function root(array $columns = ['*']) |
|
784 | } |
||
785 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..