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; |
||
| 19 | class QueryBuilder extends Builder |
||
| 20 | { |
||
| 21 | /* ------------------------------------------------------------------------------------------------ |
||
| 22 | | Properties |
||
| 23 | | ------------------------------------------------------------------------------------------------ |
||
| 24 | */ |
||
| 25 | /** |
||
| 26 | * The model being queried. |
||
| 27 | * |
||
| 28 | * @var \Arcanedev\LaravelNestedSet\Contracts\Nodeable |
||
| 29 | */ |
||
| 30 | protected $model; |
||
| 31 | |||
| 32 | /* ------------------------------------------------------------------------------------------------ |
||
| 33 | | Main Functions |
||
| 34 | | ------------------------------------------------------------------------------------------------ |
||
| 35 | */ |
||
| 36 | /** |
||
| 37 | * Get node's `lft` and `rgt` values. |
||
| 38 | * |
||
| 39 | * @param mixed $id |
||
| 40 | * @param bool $required |
||
| 41 | * |
||
| 42 | * @return array |
||
| 43 | */ |
||
| 44 | 76 | public function getNodeData($id, $required = false) |
|
| 45 | { |
||
| 46 | 76 | $query = $this->toBase(); |
|
| 47 | |||
| 48 | 76 | $query->where($this->model->getKeyName(), '=', $id); |
|
| 49 | |||
| 50 | 76 | $data = $query->first([ |
|
| 51 | 76 | $this->model->getLftName(), |
|
| 52 | 76 | $this->model->getRgtName(), |
|
| 53 | 57 | ]); |
|
| 54 | |||
| 55 | 76 | if ( ! $data && $required) { |
|
| 56 | 4 | throw new ModelNotFoundException; |
|
| 57 | } |
||
| 58 | |||
| 59 | 72 | return (array) $data; |
|
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Get plain node data. |
||
| 64 | * |
||
| 65 | * @param mixed $id |
||
| 66 | * @param bool $required |
||
| 67 | * |
||
| 68 | * @return array |
||
| 69 | */ |
||
| 70 | 48 | public function getPlainNodeData($id, $required = false) |
|
| 71 | { |
||
| 72 | 48 | return array_values($this->getNodeData($id, $required)); |
|
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Scope limits query to select just root node. |
||
| 77 | * |
||
| 78 | * @return self |
||
| 79 | */ |
||
| 80 | 20 | public function whereIsRoot() |
|
| 81 | { |
||
| 82 | 20 | $this->query->whereNull($this->model->getParentIdName()); |
|
| 83 | |||
| 84 | 20 | return $this; |
|
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Limit results to ancestors of specified node. |
||
| 89 | * |
||
| 90 | * @param mixed $id |
||
| 91 | * |
||
| 92 | * @return self |
||
| 93 | */ |
||
| 94 | 20 | public function whereAncestorOf($id) |
|
| 95 | { |
||
| 96 | 20 | $keyName = $this->model->getKeyName(); |
|
| 97 | |||
| 98 | 20 | if (NestedSet::isNode($id)) { |
|
| 99 | 16 | $value = '?'; |
|
| 100 | |||
| 101 | 16 | $this->query->addBinding($id->getLft()); |
|
| 102 | |||
| 103 | 16 | $id = $id->getKey(); |
|
| 104 | 12 | } else { |
|
| 105 | 4 | $valueQuery = $this->model |
|
| 106 | 4 | ->newQuery() |
|
| 107 | 4 | ->toBase() |
|
| 108 | 4 | ->select("_.".$this->model->getLftName()) |
|
| 109 | 4 | ->from($this->model->getTable().' as _') |
|
| 110 | 4 | ->where($keyName, '=', $id) |
|
| 111 | 4 | ->limit(1); |
|
| 112 | |||
| 113 | 4 | $this->query->mergeBindings($valueQuery); |
|
| 114 | |||
| 115 | 4 | $value = '(' . $valueQuery->toSql() . ')'; |
|
| 116 | } |
||
| 117 | |||
| 118 | 20 | list($lft, $rgt) = $this->wrappedColumns(); |
|
| 119 | |||
| 120 | 20 | $this->query->whereRaw("{$value} between {$lft} and {$rgt}"); |
|
| 121 | |||
| 122 | // Exclude the node |
||
| 123 | 20 | $this->where($keyName, '<>', $id); |
|
| 124 | |||
| 125 | 20 | return $this; |
|
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Get ancestors of specified node. |
||
| 130 | * |
||
| 131 | * @param mixed $id |
||
| 132 | * @param array $columns |
||
| 133 | * |
||
| 134 | * @return self |
||
| 135 | */ |
||
| 136 | 12 | public function ancestorsOf($id, array $columns = ['*']) |
|
| 137 | { |
||
| 138 | 12 | return $this->whereAncestorOf($id)->get($columns); |
|
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Add node selection statement between specified range. |
||
| 143 | * |
||
| 144 | * @param array $values |
||
| 145 | * @param string $boolean |
||
| 146 | * @param bool $not |
||
| 147 | * |
||
| 148 | * @return self |
||
| 149 | */ |
||
| 150 | 44 | public function whereNodeBetween($values, $boolean = 'and', $not = false) |
|
| 151 | { |
||
| 152 | 44 | $this->query->whereBetween($this->model->getLftName(), $values, $boolean, $not); |
|
| 153 | |||
| 154 | 44 | return $this; |
|
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Add node selection statement between specified range joined with `or` operator. |
||
| 159 | * |
||
| 160 | * @param array $values |
||
| 161 | * |
||
| 162 | * @return self |
||
| 163 | */ |
||
| 164 | public function orWhereNodeBetween($values) |
||
| 165 | { |
||
| 166 | return $this->whereNodeBetween($values, 'or'); |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Add constraint statement to descendants of specified node. |
||
| 171 | * |
||
| 172 | * @param mixed $id |
||
| 173 | * @param string $boolean |
||
| 174 | * @param bool $not |
||
| 175 | * |
||
| 176 | * @return self |
||
| 177 | */ |
||
| 178 | 48 | public function whereDescendantOf($id, $boolean = 'and', $not = false) |
|
| 179 | { |
||
| 180 | 48 | $data = NestedSet::isNode($id) |
|
| 181 | 47 | ? $id->getBounds() |
|
| 182 | 48 | : $this->model->newNestedSetQuery()->getPlainNodeData($id, true); |
|
| 183 | |||
| 184 | // Don't include the node |
||
| 185 | 44 | ++$data[0]; |
|
| 186 | |||
| 187 | 44 | return $this->whereNodeBetween($data, $boolean, $not); |
|
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @param mixed $id |
||
| 192 | * |
||
| 193 | * @return self |
||
| 194 | */ |
||
| 195 | public function whereNotDescendantOf($id) |
||
| 196 | { |
||
| 197 | return $this->whereDescendantOf($id, 'and', true); |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * @param mixed $id |
||
| 202 | * |
||
| 203 | * @return self |
||
| 204 | */ |
||
| 205 | 4 | public function orWhereDescendantOf($id) |
|
| 206 | { |
||
| 207 | 4 | return $this->whereDescendantOf($id, 'or'); |
|
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @param mixed $id |
||
| 212 | * |
||
| 213 | * @return self |
||
| 214 | */ |
||
| 215 | public function orWhereNotDescendantOf($id) |
||
| 216 | { |
||
| 217 | return $this->whereDescendantOf($id, 'or', true); |
||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Get descendants of specified node. |
||
| 222 | * |
||
| 223 | * @param mixed $id |
||
| 224 | * @param array $columns |
||
| 225 | * |
||
| 226 | * @return \Arcanedev\LaravelNestedSet\Eloquent\Collection |
||
| 227 | */ |
||
| 228 | public function descendantsOf($id, array $columns = ['*']) |
||
| 229 | { |
||
| 230 | try { |
||
| 231 | return $this->whereDescendantOf($id)->get($columns); |
||
| 232 | } |
||
| 233 | catch (ModelNotFoundException $e) { |
||
| 234 | return $this->model->newCollection(); |
||
| 235 | } |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @param mixed $id |
||
| 240 | * @param string $operator |
||
| 241 | * @param string $boolean |
||
| 242 | * |
||
| 243 | * @return self |
||
| 244 | */ |
||
| 245 | protected function whereIsBeforeOrAfter($id, $operator, $boolean) |
||
| 246 | { |
||
| 247 | if (NestedSet::isNode($id)) { |
||
| 248 | $value = '?'; |
||
| 249 | |||
| 250 | $this->query->addBinding($id->getLft()); |
||
| 251 | } else { |
||
| 252 | $valueQuery = $this->model |
||
| 253 | ->newQuery() |
||
| 254 | ->toBase() |
||
| 255 | ->select('_n.'.$this->model->getLftName()) |
||
| 256 | ->from($this->model->getTable().' as _n') |
||
| 257 | ->where('_n.'.$this->model->getKeyName(), '=', $id); |
||
| 258 | |||
| 259 | $this->query->mergeBindings($valueQuery); |
||
| 260 | |||
| 261 | $value = '('.$valueQuery->toSql().')'; |
||
| 262 | } |
||
| 263 | |||
| 264 | list($lft,) = $this->wrappedColumns(); |
||
| 265 | |||
| 266 | $this->query->whereRaw("{$lft} {$operator} {$value}", [ ], $boolean); |
||
| 267 | |||
| 268 | return $this; |
||
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Constraint nodes to those that are after specified node. |
||
| 273 | * |
||
| 274 | * @param mixed $id |
||
| 275 | * @param string $boolean |
||
| 276 | * |
||
| 277 | * @return self |
||
| 278 | */ |
||
| 279 | public function whereIsAfter($id, $boolean = 'and') |
||
| 280 | { |
||
| 281 | return $this->whereIsBeforeOrAfter($id, '>', $boolean); |
||
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Constraint nodes to those that are before specified node. |
||
| 286 | * |
||
| 287 | * @param mixed $id |
||
| 288 | * @param string $boolean |
||
| 289 | * |
||
| 290 | * @return self |
||
| 291 | */ |
||
| 292 | public function whereIsBefore($id, $boolean = 'and') |
||
| 293 | { |
||
| 294 | return $this->whereIsBeforeOrAfter($id, '<', $boolean); |
||
| 295 | } |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Include depth level into the result. |
||
| 299 | * |
||
| 300 | * @param string $as |
||
| 301 | * |
||
| 302 | * @return self |
||
| 303 | */ |
||
| 304 | 16 | public function withDepth($as = 'depth') |
|
| 305 | { |
||
| 306 | 16 | if ($this->query->columns === null) { |
|
| 307 | 16 | $this->query->columns = ['*']; |
|
| 308 | 12 | } |
|
| 309 | |||
| 310 | 16 | $table = $this->wrappedTable(); |
|
| 311 | |||
| 312 | 16 | list($lft, $rgt) = $this->wrappedColumns(); |
|
| 313 | |||
| 314 | 16 | $query = $this->model |
|
| 315 | 16 | ->newScopedQuery('_d') |
|
| 316 | 16 | ->toBase() |
|
| 317 | 16 | ->selectRaw('count(1) - 1') |
|
| 318 | 16 | ->from($this->model->getTable().' as _d') |
|
| 319 | 16 | ->whereRaw("{$table}.{$lft} between _d.{$lft} and _d.{$rgt}"); |
|
| 320 | |||
| 321 | 16 | $this->query->selectSub($query, $as); |
|
| 322 | |||
| 323 | 16 | return $this; |
|
| 324 | } |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Get wrapped `lft` and `rgt` column names. |
||
| 328 | * |
||
| 329 | * @return array |
||
| 330 | */ |
||
| 331 | 48 | protected function wrappedColumns() |
|
| 332 | { |
||
| 333 | 48 | $grammar = $this->query->getGrammar(); |
|
| 334 | |||
| 335 | return [ |
||
| 336 | 48 | $grammar->wrap($this->model->getLftName()), |
|
| 337 | 48 | $grammar->wrap($this->model->getRgtName()), |
|
| 338 | 36 | ]; |
|
| 339 | } |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Get a wrapped table name. |
||
| 343 | * |
||
| 344 | * @return string |
||
| 345 | */ |
||
| 346 | 28 | protected function wrappedTable() |
|
| 347 | { |
||
| 348 | 28 | return $this->query->getGrammar()->wrapTable($this->getQuery()->from); |
|
| 349 | } |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Wrap model's key name. |
||
| 353 | * |
||
| 354 | * @return string |
||
| 355 | */ |
||
| 356 | 12 | protected function wrappedKey() |
|
| 357 | { |
||
| 358 | 12 | return $this->query->getGrammar()->wrap($this->model->getKeyName()); |
|
| 359 | } |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Exclude root node from the result. |
||
| 363 | * |
||
| 364 | * @return self |
||
| 365 | */ |
||
| 366 | 8 | public function withoutRoot() |
|
| 367 | { |
||
| 368 | 8 | $this->query->whereNotNull($this->model->getParentIdName()); |
|
| 369 | |||
| 370 | 8 | return $this; |
|
| 371 | } |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Order by node position. |
||
| 375 | * |
||
| 376 | * @param string $dir |
||
| 377 | * |
||
| 378 | * @return self |
||
| 379 | */ |
||
| 380 | 52 | public function defaultOrder($dir = 'asc') |
|
| 381 | { |
||
| 382 | 52 | $this->query->orders = []; |
|
| 383 | 52 | $this->query->orderBy($this->model->getLftName(), $dir); |
|
| 384 | |||
| 385 | 52 | return $this; |
|
| 386 | } |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Order by reversed node position. |
||
| 390 | * |
||
| 391 | * @return self |
||
| 392 | */ |
||
| 393 | 4 | public function reversed() |
|
| 397 | |||
| 398 | /** |
||
| 399 | * Move a node to the new position. |
||
| 400 | * |
||
| 401 | * @param mixed $key |
||
| 402 | * @param int $position |
||
| 403 | * |
||
| 404 | * @return int |
||
| 405 | */ |
||
| 406 | 40 | public function moveNode($key, $position) |
|
| 446 | |||
| 447 | /** |
||
| 448 | * Make or remove gap in the tree. Negative height will remove gap. |
||
| 449 | * |
||
| 450 | * @param int $cut |
||
| 451 | * @param int $height |
||
| 452 | * |
||
| 453 | * @return int |
||
| 454 | */ |
||
| 455 | 48 | public function makeGap($cut, $height) |
|
| 466 | |||
| 467 | /** |
||
| 468 | * Get patch for columns. |
||
| 469 | * |
||
| 470 | * @param array $params |
||
| 471 | * |
||
| 472 | * @return array |
||
| 473 | */ |
||
| 474 | 84 | protected function patch(array $params) |
|
| 485 | |||
| 486 | /** |
||
| 487 | * Get patch for single column. |
||
| 488 | * |
||
| 489 | * @param string $col |
||
| 490 | * @param array $params |
||
| 491 | * |
||
| 492 | * @return string |
||
| 493 | */ |
||
| 494 | 84 | protected function columnPatch($col, array $params) |
|
| 523 | |||
| 524 | /** |
||
| 525 | * Get statistics of errors of the tree. |
||
| 526 | * |
||
| 527 | * @return array |
||
| 528 | */ |
||
| 529 | 12 | public function countErrors() |
|
| 549 | |||
| 550 | /** |
||
| 551 | * Get the oddness errors query. |
||
| 552 | * |
||
| 553 | * @return \Illuminate\Database\Query\Builder |
||
| 554 | */ |
||
| 555 | 12 | protected function getOddnessQuery() |
|
| 567 | |||
| 568 | /** |
||
| 569 | * Get the duplicates errors query. |
||
| 570 | * |
||
| 571 | * @return \Arcanedev\LaravelNestedSet\Eloquent\QueryBuilder|\Illuminate\Database\Query\Builder |
||
| 572 | */ |
||
| 573 | 12 | protected function getDuplicatesQuery() |
|
| 593 | |||
| 594 | /** |
||
| 595 | * Get the wrong parent query. |
||
| 596 | * |
||
| 597 | * @return \Illuminate\Database\Query\Builder |
||
| 598 | */ |
||
| 599 | 12 | protected function getWrongParentQuery() |
|
| 600 | { |
||
| 601 | 12 | $table = $this->wrappedTable(); |
|
| 602 | 12 | $keyName = $this->wrappedKey(); |
|
| 603 | 12 | $parentIdName = $this->query->raw($this->model->getParentIdName()); |
|
| 604 | 12 | $query = $this->model->newNestedSetQuery('c') |
|
| 605 | 12 | ->toBase() |
|
| 606 | 12 | ->from($this->query->raw("{$table} c, {$table} p, $table m")) |
|
| 607 | 12 | ->whereRaw("c.{$parentIdName}=p.{$keyName}") |
|
| 608 | 12 | ->whereRaw("m.{$keyName} <> p.{$keyName}") |
|
| 609 | 12 | ->whereRaw("m.{$keyName} <> c.{$keyName}") |
|
| 610 | ->whereNested(function (Query $inner) { |
||
| 611 | 12 | list($lft, $rgt) = $this->wrappedColumns(); |
|
| 612 | |||
| 613 | 12 | $inner->whereRaw("c.{$lft} not between p.{$lft} and p.{$rgt}") |
|
| 614 | 12 | ->orWhereRaw("c.{$lft} between m.{$lft} and m.{$rgt}") |
|
| 623 | |||
| 624 | /** |
||
| 625 | * Get the missing parent query. |
||
| 626 | * |
||
| 627 | * @return \Illuminate\Database\Query\Builder |
||
| 628 | */ |
||
| 629 | 12 | protected function getMissingParentQuery() |
|
| 653 | |||
| 654 | /** |
||
| 655 | * Get the number of total errors of the tree. |
||
| 656 | * |
||
| 657 | * @return int |
||
| 658 | */ |
||
| 659 | 8 | public function getTotalErrors() |
|
| 663 | |||
| 664 | /** |
||
| 665 | * Get whether the tree is broken. |
||
| 666 | * |
||
| 667 | * @return bool |
||
| 668 | */ |
||
| 669 | 8 | public function isBroken() |
|
| 673 | |||
| 674 | /** |
||
| 675 | * Fixes the tree based on parentage info. |
||
| 676 | * Nodes with invalid parent are saved as roots. |
||
| 677 | * |
||
| 678 | * @return int The number of fixed nodes |
||
| 679 | */ |
||
| 680 | 4 | public function fixTree() |
|
| 694 | |||
| 695 | /** |
||
| 696 | * Rebuild the tree based on raw data. |
||
| 697 | * If item data does not contain primary key, new node will be created. |
||
| 698 | * |
||
| 699 | * @param array $data |
||
| 700 | * @param bool $delete Whether to delete nodes that exists but not in the data array |
||
| 701 | * |
||
| 702 | * @return int |
||
| 703 | */ |
||
| 704 | 12 | public function rebuildTree(array $data, $delete = false) |
|
| 710 | |||
| 711 | /** |
||
| 712 | * Get the root node. |
||
| 713 | * |
||
| 714 | * @param array $columns |
||
| 715 | * |
||
| 716 | * @return \Illuminate\Database\Eloquent\Model|\Arcanedev\LaravelNestedSet\Contracts\Nodeable|null |
||
| 717 | */ |
||
| 718 | 16 | public function root(array $columns = ['*']) |
|
| 722 | } |
||
| 723 |