Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
| 20 | class Query |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * Mapper Instance |
||
| 24 | * |
||
| 25 | * @var \Analogue\ORM\System\Mapper |
||
| 26 | */ |
||
| 27 | protected $mapper; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * DB Adatper |
||
| 31 | * |
||
| 32 | * @var \Analogue\ORM\Drivers\DBAdapter |
||
| 33 | */ |
||
| 34 | protected $adapter; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Query Builder Instance |
||
| 38 | * |
||
| 39 | * @var \Analogue\ORM\Drivers\QueryAdapter|\Analogue\ORM\Drivers\IlluminateQueryAdapter |
||
| 40 | */ |
||
| 41 | protected $query; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Entity Map Instance |
||
| 45 | * |
||
| 46 | * @var \Analogue\ORM\EntityMap |
||
| 47 | */ |
||
| 48 | protected $entityMap; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * The relationships that should be eager loaded. |
||
| 52 | * |
||
| 53 | * @var array |
||
| 54 | */ |
||
| 55 | protected $eagerLoad = []; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * All of the registered builder macros. |
||
| 59 | * |
||
| 60 | * @var array |
||
| 61 | */ |
||
| 62 | protected $macros = []; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * The methods that should be returned from query builder. |
||
| 66 | * |
||
| 67 | * @var array |
||
| 68 | */ |
||
| 69 | protected $passthru = [ |
||
| 70 | 'toSql', |
||
| 71 | 'lists', |
||
| 72 | 'pluck', |
||
| 73 | 'count', |
||
| 74 | 'min', |
||
| 75 | 'max', |
||
| 76 | 'avg', |
||
| 77 | 'sum', |
||
| 78 | 'exists', |
||
| 79 | 'getBindings', |
||
| 80 | ]; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Query Builder Blacklist |
||
| 84 | */ |
||
| 85 | protected $blacklist = [ |
||
| 86 | 'insert', |
||
| 87 | 'insertGetId', |
||
| 88 | 'lock', |
||
| 89 | 'lockForUpdate', |
||
| 90 | 'sharedLock', |
||
| 91 | 'update', |
||
| 92 | 'increment', |
||
| 93 | 'decrement', |
||
| 94 | 'delete', |
||
| 95 | 'truncate', |
||
| 96 | 'raw', |
||
| 97 | ]; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Create a new Analogue Query Builder instance. |
||
| 101 | * |
||
| 102 | * @param Mapper $mapper |
||
| 103 | * @param DBAdapter $adapter |
||
| 104 | */ |
||
| 105 | public function __construct(Mapper $mapper, DBAdapter $adapter) |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Run the query and return the result |
||
| 121 | * |
||
| 122 | * @param array $columns |
||
| 123 | * @return \Analogue\ORM\EntityCollection |
||
| 124 | */ |
||
| 125 | public function get($columns = ['*']) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Find an entity by its primary key |
||
| 135 | * |
||
| 136 | * @param string|integer $id |
||
| 137 | * @param array $columns |
||
| 138 | * @return \Analogue\ORM\Mappable |
||
| 139 | */ |
||
| 140 | public function find($id, $columns = ['*']) |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Find many entities by their primary keys. |
||
| 153 | * |
||
| 154 | * @param array $id |
||
| 155 | * @param array $columns |
||
| 156 | * @return EntityCollection |
||
| 157 | */ |
||
| 158 | public function findMany($id, $columns = ['*']) |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Find a model by its primary key or throw an exception. |
||
| 171 | * |
||
| 172 | * @param mixed $id |
||
| 173 | * @param array $columns |
||
| 174 | * @throws \Analogue\ORM\Exceptions\EntityNotFoundException |
||
| 175 | * @return mixed|self |
||
| 176 | */ |
||
| 177 | View Code Duplication | public function findOrFail($id, $columns = ['*']) |
|
| 185 | |||
| 186 | |||
| 187 | /** |
||
| 188 | * Execute the query and get the first result. |
||
| 189 | * |
||
| 190 | * @param array $columns |
||
| 191 | * @return \Analogue\ORM\Entity |
||
| 192 | */ |
||
| 193 | public function first($columns = ['*']) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Execute the query and get the first result or throw an exception. |
||
| 200 | * |
||
| 201 | * @param array $columns |
||
| 202 | * @throws EntityNotFoundException |
||
| 203 | * @return \Analogue\ORM\Entity |
||
| 204 | */ |
||
| 205 | View Code Duplication | public function firstOrFail($columns = ['*']) |
|
| 213 | |||
| 214 | /** |
||
| 215 | * Pluck a single column from the database. |
||
| 216 | * |
||
| 217 | * @param string $column |
||
| 218 | * @return mixed |
||
| 219 | */ |
||
| 220 | public function pluck($column) |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Chunk the results of the query. |
||
| 231 | * |
||
| 232 | * @param int $count |
||
| 233 | * @param callable $callback |
||
| 234 | * @return void |
||
| 235 | */ |
||
| 236 | public function chunk($count, callable $callback) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Get an array with the values of a given column. |
||
| 254 | * |
||
| 255 | * @param string $column |
||
| 256 | * @param string $key |
||
| 257 | * @return array |
||
| 258 | */ |
||
| 259 | public function lists($column, $key = null) |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Get a paginator for the "select" statement. |
||
| 266 | * |
||
| 267 | * @param int $perPage |
||
| 268 | * @param array $columns |
||
| 269 | * @return LengthAwarePaginator |
||
| 270 | */ |
||
| 271 | public function paginate($perPage = null, $columns = ['*']) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Get a paginator for a grouped statement. |
||
| 287 | * |
||
| 288 | * @param \Illuminate\Pagination\Factory $paginator |
||
| 289 | * @param int $perPage |
||
| 290 | * @param array $columns |
||
| 291 | * @return \Illuminate\Pagination\Paginator |
||
| 292 | */ |
||
| 293 | protected function groupedPaginate($paginator, $perPage, $columns) |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Get a paginator for an ungrouped statement. |
||
| 302 | * |
||
| 303 | * @param \Illuminate\Pagination\Factory $paginator |
||
| 304 | * @param int $perPage |
||
| 305 | * @param array $columns |
||
| 306 | * @return \Illuminate\Pagination\Paginator |
||
| 307 | */ |
||
| 308 | protected function ungroupedPaginate($paginator, $perPage, $columns) |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Paginate the given query into a simple paginator. |
||
| 324 | * |
||
| 325 | * @param int $perPage |
||
| 326 | * @param array $columns |
||
| 327 | * @return \Illuminate\Contracts\Pagination\Paginator |
||
| 328 | */ |
||
| 329 | public function simplePaginate($perPage = null, $columns = ['*']) |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Add a basic where clause to the query. |
||
| 342 | * |
||
| 343 | * @param string $column |
||
| 344 | * @param string $operator |
||
| 345 | * @param mixed $value |
||
| 346 | * @param string $boolean |
||
| 347 | * @return $this |
||
| 348 | */ |
||
| 349 | public function where($column, $operator = null, $value = null, $boolean = 'and') |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Add an "or where" clause to the query. |
||
| 366 | * |
||
| 367 | * @param string $column |
||
| 368 | * @param string $operator |
||
| 369 | * @param mixed $value |
||
| 370 | * @return \Analogue\ORM\System\Query |
||
| 371 | */ |
||
| 372 | public function orWhere($column, $operator = null, $value = null) |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Add a relationship count condition to the query. |
||
| 379 | * |
||
| 380 | * @param string $relation |
||
| 381 | * @param string $operator |
||
| 382 | * @param int $count |
||
| 383 | * @param string $boolean |
||
| 384 | * @param \Closure $callback |
||
| 385 | * @return \Analogue\ORM\System\Query |
||
| 386 | */ |
||
| 387 | public function has($relation, $operator = '>=', $count = 1, $boolean = 'and', $callback = null) |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Add a relationship count condition to the query with where clauses. |
||
| 404 | * |
||
| 405 | * @param string $relation |
||
| 406 | * @param \Closure $callback |
||
| 407 | * @param string $operator |
||
| 408 | * @param int $count |
||
| 409 | * @return \Analogue\ORM\System\Query |
||
| 410 | */ |
||
| 411 | public function whereHas($relation, Closure $callback, $operator = '>=', $count = 1) |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Add a relationship count condition to the query with an "or". |
||
| 418 | * |
||
| 419 | * @param string $relation |
||
| 420 | * @param string $operator |
||
| 421 | * @param int $count |
||
| 422 | * @return \Analogue\ORM\System\Query |
||
| 423 | */ |
||
| 424 | public function orHas($relation, $operator = '>=', $count = 1) |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Add a relationship count condition to the query with where clauses and an "or". |
||
| 431 | * |
||
| 432 | * @param string $relation |
||
| 433 | * @param \Closure $callback |
||
| 434 | * @param string $operator |
||
| 435 | * @param int $count |
||
| 436 | * @return \Analogue\ORM\System\Query |
||
| 437 | */ |
||
| 438 | public function orWhereHas($relation, Closure $callback, $operator = '>=', $count = 1) |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Add the "has" condition where clause to the query. |
||
| 445 | * |
||
| 446 | * @param \Analogue\ORM\System\Query $hasQuery |
||
| 447 | * @param \Analogue\ORM\Relationships\Relationship $relation |
||
| 448 | * @param string $operator |
||
| 449 | * @param int $count |
||
| 450 | * @param string $boolean |
||
| 451 | * @return \Analogue\ORM\System\Query |
||
| 452 | */ |
||
| 453 | protected function addHasWhere(Query $hasQuery, Relationship $relation, $operator, $count, $boolean) |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Merge the "wheres" from a relation query to a has query. |
||
| 466 | * |
||
| 467 | * @param \Analogue\ORM\System\Query $hasQuery |
||
| 468 | * @param \Analogue\ORM\Relationships\Relationship $relation |
||
| 469 | * @return void |
||
| 470 | */ |
||
| 471 | protected function mergeWheresToHas(Query $hasQuery, Relationship $relation) |
||
| 484 | |||
| 485 | /** |
||
| 486 | * Get the "has relation" base query instance. |
||
| 487 | * |
||
| 488 | * @param string $relation |
||
| 489 | * @param $entity |
||
| 490 | * @return \Analogue\ORM\System\Query |
||
| 491 | */ |
||
| 492 | protected function getHasRelationQuery($relation, $entity) |
||
| 498 | |||
| 499 | /** |
||
| 500 | * Get the table for the current query object |
||
| 501 | * |
||
| 502 | * @return string |
||
| 503 | */ |
||
| 504 | public function getTable() |
||
| 508 | |||
| 509 | /** |
||
| 510 | * Set the relationships that should be eager loaded. |
||
| 511 | * |
||
| 512 | * @param mixed $relations |
||
| 513 | * @return $this |
||
| 514 | */ |
||
| 515 | public function with($relations) |
||
| 525 | |||
| 526 | /** |
||
| 527 | * Get the relationships being eagerly loaded. |
||
| 528 | * |
||
| 529 | * @return array |
||
| 530 | */ |
||
| 531 | public function getEagerLoads() |
||
| 535 | |||
| 536 | /** |
||
| 537 | * Add the Entity primary key if not in requested columns |
||
| 538 | * |
||
| 539 | * @param array $columns |
||
| 540 | * @return array |
||
| 541 | */ |
||
| 542 | protected function enforceIdColumn($columns) |
||
| 549 | |||
| 550 | /** |
||
| 551 | * Get the hydrated models without eager loading. |
||
| 552 | * |
||
| 553 | * @param array $columns |
||
| 554 | * @return \Analogue\ORM\EntityCollection |
||
| 555 | */ |
||
| 556 | public function getEntities($columns = ['*']) |
||
| 571 | |||
| 572 | /** |
||
| 573 | * Extend the builder with a given callback. |
||
| 574 | * |
||
| 575 | * @param string $name |
||
| 576 | * @param \Closure $callback |
||
| 577 | * @return void |
||
| 578 | */ |
||
| 579 | public function macro($name, Closure $callback) |
||
| 583 | |||
| 584 | /** |
||
| 585 | * Get the given macro by name. |
||
| 586 | * |
||
| 587 | * @param string $name |
||
| 588 | * @return \Closure |
||
| 589 | */ |
||
| 590 | public function getMacro($name) |
||
| 594 | |||
| 595 | /** |
||
| 596 | * Get a new query builder for the model's table. |
||
| 597 | * |
||
| 598 | * @return \Analogue\ORM\System\Query |
||
| 599 | */ |
||
| 600 | public function newQuery() |
||
| 606 | |||
| 607 | /** |
||
| 608 | * Get a new query builder without any scope applied. |
||
| 609 | * |
||
| 610 | * @return \Analogue\ORM\System\Query |
||
| 611 | */ |
||
| 612 | public function newQueryWithoutScopes() |
||
| 616 | |||
| 617 | /** |
||
| 618 | * Get the Mapper instance for this Query Builder |
||
| 619 | * |
||
| 620 | * @return \Analogue\ORM\System\Mapper |
||
| 621 | */ |
||
| 622 | public function getMapper() |
||
| 626 | |||
| 627 | /** |
||
| 628 | * Get the underlying query adapter |
||
| 629 | * |
||
| 630 | * (REFACTOR: this method should move out, we need to provide the client classes |
||
| 631 | * with the adapter instead.) |
||
| 632 | * |
||
| 633 | * @return \Analogue\ORM\Drivers\QueryAdapter|\Analogue\ORM\Drivers\IlluminateQueryAdapter |
||
| 634 | */ |
||
| 635 | public function getQuery() |
||
| 639 | |||
| 640 | /** |
||
| 641 | * Dynamically handle calls into the query instance. |
||
| 642 | * |
||
| 643 | * @param string $method |
||
| 644 | * @param array $parameters |
||
| 645 | * @throws Exception |
||
| 646 | * @return mixed |
||
| 647 | */ |
||
| 648 | public function __call($method, $parameters) |
||
| 664 | } |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.