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 |
||
| 19 | class Query |
||
| 20 | { |
||
| 21 | const DEFAULT_LIMIT = 100; |
||
| 22 | const MAX_LIMIT = 1000; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var Model|string |
||
| 26 | */ |
||
| 27 | private $model; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var array |
||
| 31 | */ |
||
| 32 | private $joins; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var array |
||
| 36 | */ |
||
| 37 | private $eagerLoaded; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var array |
||
| 41 | */ |
||
| 42 | private $where; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var int |
||
| 46 | */ |
||
| 47 | private $limit; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var int |
||
| 51 | */ |
||
| 52 | private $start; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var array |
||
| 56 | */ |
||
| 57 | private $sort; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @param Model|string $model |
||
| 61 | */ |
||
| 62 | public function __construct($model = '') |
||
| 63 | { |
||
| 64 | $this->model = $model; |
||
| 65 | $this->joins = []; |
||
| 66 | $this->eagerLoaded = []; |
||
| 67 | $this->where = []; |
||
| 68 | $this->start = 0; |
||
| 69 | $this->limit = self::DEFAULT_LIMIT; |
||
| 70 | $this->sort = []; |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Gets the model class associated with this query. |
||
| 75 | * |
||
| 76 | * @return Model|string |
||
| 77 | */ |
||
| 78 | public function getModel() |
||
| 79 | { |
||
| 80 | return $this->model; |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Sets the limit for this query. |
||
| 85 | * |
||
| 86 | * @return $this |
||
| 87 | */ |
||
| 88 | public function limit(int $limit) |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Gets the limit for this query. |
||
| 97 | */ |
||
| 98 | public function getLimit(): int |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Sets the start offset. |
||
| 105 | * |
||
| 106 | * @return $this |
||
| 107 | */ |
||
| 108 | public function start(int $start) |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Gets the start offset. |
||
| 117 | */ |
||
| 118 | public function getStart(): int |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Sets the sort pattern for the query. |
||
| 125 | * |
||
| 126 | * @param array|string $sort |
||
| 127 | * |
||
| 128 | * @return $this |
||
| 129 | */ |
||
| 130 | public function sort($sort) |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Gets the sort parameters. |
||
| 158 | */ |
||
| 159 | public function getSort(): array |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Sets the where parameters. |
||
| 166 | * Accepts the following forms: |
||
| 167 | * i) where(['name' => 'Bob']) |
||
| 168 | * ii) where('name', 'Bob') |
||
| 169 | * iii) where('balance', 100, '>') |
||
| 170 | * iv) where('balance > 100'). |
||
| 171 | * |
||
| 172 | * @param array|string $where |
||
| 173 | * @param mixed $value optional value |
||
| 174 | * @param string|null $condition optional condition |
||
| 175 | * |
||
| 176 | * @return $this |
||
| 177 | */ |
||
| 178 | public function where($where, $value = null, $condition = null) |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Gets the where parameters. |
||
| 202 | */ |
||
| 203 | public function getWhere(): array |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Adds a join to the query. Matches a property on this model |
||
| 210 | * to the ID of the model we are joining. |
||
| 211 | * |
||
| 212 | * @param string $model model being joined |
||
| 213 | * @param string $column name of local property |
||
| 214 | * |
||
| 215 | * @return $this |
||
| 216 | */ |
||
| 217 | public function join($model, string $column, string $foreignKey) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Gets the joins. |
||
| 226 | */ |
||
| 227 | public function getJoins(): array |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Marks a relationship property on the model that should be eager loaded. |
||
| 234 | * |
||
| 235 | * @param string $k local property containing the relationship |
||
| 236 | * |
||
| 237 | * @return $this |
||
| 238 | */ |
||
| 239 | public function with(string $k) |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Gets the relationship properties that are going to be eager-loaded. |
||
| 250 | */ |
||
| 251 | public function getWith(): array |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Executes the query against the model's driver. |
||
| 258 | * |
||
| 259 | * @return array results |
||
| 260 | */ |
||
| 261 | public function execute(): array |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Creates an iterator for a search. |
||
| 366 | * |
||
| 367 | * @return Iterator |
||
| 368 | */ |
||
| 369 | public function all() |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Executes the query against the model's driver and returns the first result. |
||
| 376 | * |
||
| 377 | * @return array|Model|null when $limit = 1, returns a single model or null, otherwise returns an array |
||
| 378 | */ |
||
| 379 | public function first(int $limit = 1) |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Gets the number of models matching the query. |
||
| 392 | */ |
||
| 393 | public function count(): int |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Gets the sum of a property matching the query. |
||
| 403 | * |
||
| 404 | * @return number |
||
| 405 | */ |
||
| 406 | public function sum(string $property) |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Gets the average of a property matching the query. |
||
| 416 | * |
||
| 417 | * @return number |
||
| 418 | */ |
||
| 419 | public function average(string $property) |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Gets the max of a property matching the query. |
||
| 429 | * |
||
| 430 | * @return number |
||
| 431 | */ |
||
| 432 | public function max(string $property) |
||
| 439 | |||
| 440 | /** |
||
| 441 | * Gets the min of a property matching the query. |
||
| 442 | * |
||
| 443 | * @return number |
||
| 444 | */ |
||
| 445 | public function min(string $property) |
||
| 452 | |||
| 453 | /** |
||
| 454 | * Updates all of the models matched by this query. |
||
| 455 | * |
||
| 456 | * @todo should be optimized to be done in a single call to the data layer |
||
| 457 | * |
||
| 458 | * @param array $params key-value update parameters |
||
| 459 | * |
||
| 460 | * @return int # of models updated |
||
| 461 | */ |
||
| 462 | public function set(array $params): int |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Deletes all of the models matched by this query. |
||
| 475 | * |
||
| 476 | * @todo should be optimized to be done in a single call to the data layer |
||
| 477 | * |
||
| 478 | * @return int # of models deleted |
||
| 479 | */ |
||
| 480 | public function delete(): int |
||
| 490 | |||
| 491 | /** |
||
| 492 | * Hydrates the eager-loaded relationships for a given set of IDs. |
||
| 493 | * |
||
| 494 | * @param string $modelClass |
||
| 495 | * @param bool $multiple when true will condense |
||
| 496 | * |
||
| 497 | * @return Model[] |
||
| 498 | */ |
||
| 499 | private function fetchRelationships($modelClass, array $ids, string $foreignKey, bool $multiple): array |
||
| 524 | } |
||
| 525 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.