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 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 |
||
| 26 | class QueryBuilder implements Countable |
||
| 27 | { |
||
| 28 | /** |
||
| 29 | * The type of the model we're building a query for |
||
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | protected $type; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * The columns that the model provided us |
||
| 36 | * @var array |
||
| 37 | */ |
||
| 38 | protected $columns = array('id' => 'id'); |
||
| 39 | |||
| 40 | /** |
||
| 41 | * The conditions to include in WHERE |
||
| 42 | * @var string[] |
||
| 43 | */ |
||
| 44 | protected $conditions = array(); |
||
| 45 | |||
| 46 | /** |
||
| 47 | * The MySQL value parameters |
||
| 48 | * @var array |
||
| 49 | */ |
||
| 50 | protected $parameters = array(); |
||
| 51 | |||
| 52 | /** |
||
| 53 | * The MySQL parameter types |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | protected $types = ''; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * The MySQL value parameters for pagination |
||
| 60 | * @var array |
||
| 61 | */ |
||
| 62 | protected $paginationParameters = array(); |
||
| 63 | |||
| 64 | /** |
||
| 65 | * The MySQL parameter types for pagination |
||
| 66 | * @var string |
||
| 67 | */ |
||
| 68 | protected $paginationTypes = ''; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Extra MySQL query string to pass |
||
| 72 | * @var string |
||
| 73 | */ |
||
| 74 | protected $extras = ''; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * A column based on which we should sort the results |
||
| 78 | * @var string|null |
||
| 79 | */ |
||
| 80 | private $sortBy = null; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Whether to reverse the results |
||
| 84 | * @var bool |
||
| 85 | */ |
||
| 86 | private $reverseSort = false; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * The currently selected column |
||
| 90 | * @var string|null |
||
| 91 | */ |
||
| 92 | private $currentColumn = null; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * The currently selected column without the table name (unless it was |
||
| 96 | * explicitly provided) |
||
| 97 | * @var string|null |
||
| 98 | */ |
||
| 99 | protected $currentColumnRaw = null; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * A column to consider the name of the model |
||
| 103 | * @var string|null |
||
| 104 | */ |
||
| 105 | private $nameColumn = null; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Whether to return the results as arrays instead of models |
||
| 109 | * @var bool |
||
| 110 | */ |
||
| 111 | private $returnArray = false; |
||
|
|
|||
| 112 | |||
| 113 | /** |
||
| 114 | * The page to return |
||
| 115 | * @var int|null |
||
| 116 | */ |
||
| 117 | private $page = null; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Whether the ID of the first/last element has been provided |
||
| 121 | * @var bool |
||
| 122 | */ |
||
| 123 | private $limited = false; |
||
| 124 | |||
| 125 | /** |
||
| 126 | * The number of elements on every page |
||
| 127 | * @var int |
||
| 128 | */ |
||
| 129 | protected $resultsPerPage = 30; |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Create a new QueryBuilder |
||
| 133 | * |
||
| 134 | * A new query builder should be created on a static getQueryBuilder() |
||
| 135 | * method on each model. The options array can contain the following |
||
| 136 | * properties: |
||
| 137 | * |
||
| 138 | * - `columns`: An associative array - the key of each entry is the column |
||
| 139 | * name that will be used by other methods, while the value is |
||
| 140 | * is the column name that is used in the database structure |
||
| 141 | * |
||
| 142 | * - `activeStatuses`: If the model has a status column, this should be |
||
| 143 | * a list of values that make the entry be considered |
||
| 144 | * "active" |
||
| 145 | * |
||
| 146 | * - `name`: The name of the column which represents the name of the object |
||
| 147 | * |
||
| 148 | * @param string $type The type of the Model (e.g. "Player" or "Match") |
||
| 149 | * @param array $options The options to pass to the builder (see above) |
||
| 150 | */ |
||
| 151 | 4 | public function __construct($type, $options = array()) |
|
| 163 | |||
| 164 | /** |
||
| 165 | * Select a column |
||
| 166 | * |
||
| 167 | * `$queryBuilder->where('username')->equals('administrator');` |
||
| 168 | * |
||
| 169 | * @param string $column The column to select |
||
| 170 | * @return self |
||
| 171 | */ |
||
| 172 | 4 | public function where($column) |
|
| 182 | |||
| 183 | /** |
||
| 184 | * Request that a column equals a string (case-insensitive) |
||
| 185 | * |
||
| 186 | * @param string $string The string that the column's value should equal to |
||
| 187 | * @return self |
||
| 188 | */ |
||
| 189 | 1 | public function equals($string) |
|
| 195 | |||
| 196 | /** |
||
| 197 | * Request that a column doesNOT equals a string (case-insensitive) |
||
| 198 | * |
||
| 199 | * @param string $string The string that the column's value should equal to |
||
| 200 | * @return self |
||
| 201 | */ |
||
| 202 | 1 | public function notEquals($string) |
|
| 208 | |||
| 209 | /** |
||
| 210 | * Request that a column is greater than a quantity |
||
| 211 | * |
||
| 212 | * @param string $quantity The quantity to test against |
||
| 213 | * @return self |
||
| 214 | */ |
||
| 215 | public function greaterThan($quantity) |
||
| 216 | { |
||
| 217 | $this->addColumnCondition("> ?", $quantity, 's'); |
||
| 218 | |||
| 219 | return $this; |
||
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Request that a column is less than a quantity |
||
| 224 | * |
||
| 225 | * @param string $quantity The quantity to test against |
||
| 226 | * @return self |
||
| 227 | */ |
||
| 228 | public function lessThan($quantity) |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Request that a timestamp is before the specified time |
||
| 237 | * |
||
| 238 | * @param string|TimeDate $time The timestamp to compare to |
||
| 239 | * @param bool $inclusive Whether to include the given timestamp |
||
| 240 | * @param bool $reverse Whether to reverse the results |
||
| 241 | */ |
||
| 242 | public function isBefore($time, $inclusive = false, $reverse = false) |
||
| 243 | { |
||
| 244 | return $this->isAfter($time, $inclusive, !$reverse); |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Request that a timestamp is after the specified time |
||
| 249 | * |
||
| 250 | * @param string|TimeDate $time The timestamp to compare to |
||
| 251 | * @param bool $inclusive Whether to include the given timestamp |
||
| 252 | * @param bool $reverse Whether to reverse the results |
||
| 253 | */ |
||
| 254 | 1 | public function isAfter($time, $inclusive = false, $reverse = false) |
|
| 255 | { |
||
| 256 | 1 | if ($time instanceof TimeDate) { |
|
| 257 | 1 | $time = $time->toMysql(); |
|
| 258 | } |
||
| 259 | |||
| 260 | 1 | $comparison = ($reverse) ? '<' : '>'; |
|
| 261 | 1 | $comparison .= ($inclusive) ? '=' : ''; |
|
| 262 | |||
| 263 | 1 | $this->addColumnCondition("$comparison ?", $time, 's'); |
|
| 264 | |||
| 265 | 1 | return $this; |
|
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Request that a column equals a number |
||
| 270 | * |
||
| 271 | * @param int|Model|null $number The number that the column's value should |
||
| 272 | * equal to. If a Model is provided, use the |
||
| 273 | * model's ID, while null values are ignored. |
||
| 274 | * @return self |
||
| 275 | */ |
||
| 276 | 1 | View Code Duplication | public function is($number) |
| 277 | { |
||
| 278 | 1 | if ($number === null) { |
|
| 279 | return $this; |
||
| 280 | } |
||
| 281 | |||
| 282 | 1 | if ($number instanceof Model) { |
|
| 283 | 1 | $number = $number->getId(); |
|
| 284 | } |
||
| 285 | |||
| 286 | 1 | $this->addColumnCondition("= ?", $number, 'i'); |
|
| 287 | |||
| 288 | 1 | return $this; |
|
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Request that a column equals one of some strings |
||
| 293 | * |
||
| 294 | * @param string[] $strings The list of accepted values for the column |
||
| 295 | * @return self |
||
| 296 | */ |
||
| 297 | 3 | public function isOneOf($strings) |
|
| 298 | { |
||
| 299 | 3 | $count = count($strings); |
|
| 300 | 3 | $types = str_repeat('s', $count); |
|
| 301 | 3 | $questionMarks = str_repeat(',?', $count); |
|
| 302 | |||
| 303 | // Remove first comma from questionMarks so that MySQL can read our query |
||
| 304 | 3 | $questionMarks = ltrim($questionMarks, ','); |
|
| 305 | |||
| 306 | 3 | $this->addColumnCondition("IN ($questionMarks)", $strings, $types); |
|
| 307 | |||
| 308 | 3 | return $this; |
|
| 309 | } |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Request that a column value starts with a string (case-insensitive) |
||
| 313 | * |
||
| 314 | * @param string $string The substring that the column's value should start with |
||
| 315 | * @return self |
||
| 316 | */ |
||
| 317 | public function startsWith($string) |
||
| 318 | { |
||
| 319 | $this->addColumnCondition("LIKE CONCAT(?, '%')", $string, 's'); |
||
| 320 | |||
| 321 | return $this; |
||
| 322 | } |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Request that a specific model is not returned |
||
| 326 | * |
||
| 327 | * @param Model|int $model The ID or model you don't want to get |
||
| 328 | * @return self |
||
| 329 | */ |
||
| 330 | View Code Duplication | public function except($model) |
|
| 331 | { |
||
| 332 | if ($model instanceof Model) { |
||
| 333 | $model = $model->getId(); |
||
| 334 | } |
||
| 335 | |||
| 336 | $this->where('id'); |
||
| 337 | $this->addColumnCondition("!= ?", $model, 'i'); |
||
| 338 | |||
| 339 | return $this; |
||
| 340 | } |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Return the results sorted by the value of a column |
||
| 344 | * |
||
| 345 | * @param string $column The column based on which the results should be ordered |
||
| 346 | * @return self |
||
| 347 | */ |
||
| 348 | 2 | public function sortBy($column) |
|
| 349 | { |
||
| 350 | 2 | if (!isset($this->columns[$column])) { |
|
| 351 | throw new Exception("Unknown column"); |
||
| 352 | } |
||
| 353 | |||
| 354 | 2 | $this->sortBy = $this->columns[$column]; |
|
| 355 | |||
| 356 | 2 | return $this; |
|
| 357 | } |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Reverse the order |
||
| 361 | * |
||
| 362 | * Note: This only works if you have specified a column in the sortBy() method |
||
| 363 | * |
||
| 364 | * @return self |
||
| 365 | */ |
||
| 366 | 2 | public function reverse() |
|
| 372 | |||
| 373 | /** |
||
| 374 | * Specify the number of results per page |
||
| 375 | * |
||
| 376 | * @param int $count The number of results |
||
| 377 | * @return self |
||
| 378 | */ |
||
| 379 | 2 | public function limit($count) |
|
| 380 | { |
||
| 381 | 2 | $this->resultsPerPage = $count; |
|
| 382 | |||
| 383 | 2 | return $this; |
|
| 384 | } |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Only show results from a specific page |
||
| 388 | * |
||
| 389 | * @param int|null $page The page number (or null to show all pages - counting starts from 0) |
||
| 390 | * @return self |
||
| 391 | */ |
||
| 392 | 2 | public function fromPage($page) |
|
| 398 | |||
| 399 | /** |
||
| 400 | * End with a specific result |
||
| 401 | * |
||
| 402 | * @param int|Model $model The model (or database ID) after the first result |
||
| 403 | * @param bool $inclusive Whether to include the provided model |
||
| 404 | * @param bool $reverse Whether to reverse the results |
||
| 405 | * @return self |
||
| 406 | */ |
||
| 407 | 1 | public function endAt($model, $inclusive = false, $reverse = false) |
|
| 408 | { |
||
| 409 | 1 | return $this->startAt($model, $inclusive, !$reverse); |
|
| 410 | } |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Start with a specific result |
||
| 414 | * |
||
| 415 | * @param int|Model $model The model (or database ID) before the first result |
||
| 416 | * @param bool $inclusive Whether to include the provided model |
||
| 417 | * @param bool $reverse Whether to reverse the results |
||
| 418 | * @return self |
||
| 419 | */ |
||
| 420 | 1 | public function startAt($model, $inclusive = false, $reverse = false) |
|
| 421 | { |
||
| 422 | 1 | if (!$model) { |
|
| 423 | 1 | return $this; |
|
| 424 | } elseif ($model instanceof Model && !$model->isValid()) { |
||
| 425 | return $this; |
||
| 426 | } |
||
| 427 | |||
| 428 | $this->column($this->sortBy); |
||
| 429 | $this->limited = true; |
||
| 430 | $column = $this->currentColumn; |
||
| 431 | $table = $this->getTable(); |
||
| 432 | |||
| 433 | $comparison = $this->reverseSort ^ $reverse; |
||
| 434 | $comparison = ($comparison) ? '>' : '<'; |
||
| 435 | $comparison .= ($inclusive) ? '=' : ''; |
||
| 436 | $id = ($model instanceof Model) ? $model->getId() : $model; |
||
| 437 | |||
| 438 | // Compare an element's timestamp to the timestamp of $model; if it's the |
||
| 439 | // same, perform the comparison using IDs |
||
| 440 | $this->addColumnCondition( |
||
| 441 | "$comparison (SELECT $column FROM $table WHERE id = ?) OR ($column = (SELECT $column FROM $table WHERE id = ?) AND id $comparison ?)", |
||
| 442 | array($id, $id, $id), |
||
| 443 | 'iii' |
||
| 444 | ); |
||
| 445 | |||
| 446 | return $this; |
||
| 447 | } |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Request that only "active" Models should be returned |
||
| 451 | * |
||
| 452 | * @return self |
||
| 453 | */ |
||
| 454 | 3 | public function active() |
|
| 455 | { |
||
| 456 | 3 | if (!isset($this->columns['status'])) { |
|
| 457 | return $this; |
||
| 458 | } |
||
| 459 | |||
| 460 | 3 | $type = $this->type; |
|
| 461 | |||
| 462 | 3 | return $this->where('status')->isOneOf($type::getActiveStatuses()); |
|
| 463 | } |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Make sure that Models invisible to a player are not returned |
||
| 467 | * |
||
| 468 | * Note that this method does not take PermissionModel::canBeSeenBy() into |
||
| 469 | * consideration for performance purposes, so you will have to override this |
||
| 470 | * in your query builder if necessary. |
||
| 471 | * |
||
| 472 | * @param Player $player The player in question |
||
| 473 | * @param bool $showDeleted false to hide deleted models even from admins |
||
| 474 | * @return self |
||
| 475 | */ |
||
| 476 | 1 | public function visibleTo($player, $showDeleted = false) |
|
| 477 | { |
||
| 478 | 1 | $type = $this->type; |
|
| 479 | |||
| 480 | 1 | if (is_subclass_of($type, "PermissionModel") |
|
| 481 | 1 | && $player->hasPermission($type::EDIT_PERMISSION)) { |
|
| 482 | // The player is an admin who can see hidden models |
||
| 483 | 1 | if ($showDeleted) { |
|
| 484 | return $this; |
||
| 485 | } else { |
||
| 486 | 1 | return $this->where('status')->notEquals('deleted'); |
|
| 487 | } |
||
| 488 | } else { |
||
| 489 | 1 | return $this->active(); |
|
| 490 | } |
||
| 491 | } |
||
| 492 | |||
| 493 | /** |
||
| 494 | * Perform the query and get back the results in an array of names |
||
| 495 | * |
||
| 496 | * @return string[] An array of the type $id => $name |
||
| 497 | */ |
||
| 498 | 1 | public function getNames() |
|
| 499 | { |
||
| 500 | 1 | if (!$this->nameColumn) { |
|
| 501 | throw new Exception("You haven't specified a name column"); |
||
| 502 | } |
||
| 503 | |||
| 504 | 1 | $results = $this->getArray($this->nameColumn); |
|
| 505 | |||
| 506 | 1 | return array_column($results, $this->nameColumn, 'id'); |
|
| 507 | } |
||
| 508 | |||
| 509 | /** |
||
| 510 | * Perform the query and get back the results in a list of arrays |
||
| 511 | * |
||
| 512 | * @todo Play with partial models? |
||
| 513 | * @param string|string[] The column(s) that should be returned |
||
| 514 | * @param string $columns |
||
| 515 | * @return array[] |
||
| 516 | */ |
||
| 517 | 1 | public function getArray($columns) |
|
| 527 | |||
| 528 | /** |
||
| 529 | * An alias for QueryBuilder::getModels(), with fast fetching on by default |
||
| 530 | * and no return of results |
||
| 531 | * |
||
| 532 | * @param boolean $fastFetch Whether to perform one query to load all |
||
| 533 | * the model data instead of fetching them |
||
| 534 | * one by one |
||
| 535 | * @return void |
||
| 536 | */ |
||
| 537 | public function addToCache($fastFetch = true) |
||
| 538 | { |
||
| 539 | $this->getModels($fastFetch); |
||
| 540 | } |
||
| 541 | |||
| 542 | /** |
||
| 543 | * Perform the query and get the results as Models |
||
| 544 | * |
||
| 545 | * @todo Fix fast fetch for queries with multiple tables |
||
| 546 | * @param boolean $fastFetch Whether to perform one query to load all |
||
| 547 | * the model data instead of fetching them |
||
| 548 | * one by one (ignores cache) |
||
| 549 | * @return array |
||
| 550 | */ |
||
| 551 | 4 | public function getModels($fastFetch = false) |
|
| 566 | |||
| 567 | /** |
||
| 568 | * Count the results |
||
| 569 | * |
||
| 570 | * @return int |
||
| 571 | */ |
||
| 572 | 1 | public function count() |
|
| 585 | |||
| 586 | /** |
||
| 587 | * Count the number of pages that all the models could be separated into |
||
| 588 | */ |
||
| 589 | 1 | public function countPages() |
|
| 593 | |||
| 594 | /** |
||
| 595 | * Find if there is any result |
||
| 596 | * |
||
| 597 | * @return bool |
||
| 598 | */ |
||
| 599 | 1 | public function any() |
|
| 608 | |||
| 609 | /** |
||
| 610 | * Get the amount of results that are returned per page |
||
| 611 | * @return int |
||
| 612 | */ |
||
| 613 | 1 | public function getResultsPerPage() |
|
| 617 | |||
| 618 | /** |
||
| 619 | * Select a column to perform opeations on |
||
| 620 | * |
||
| 621 | * This is identical to the `where()` method, except that the column is |
||
| 622 | * specified as a MySQL column and not as a column name given by the model |
||
| 623 | * |
||
| 624 | * @param string $column The column to select |
||
| 625 | * @return self |
||
| 626 | */ |
||
| 627 | 4 | protected function column($column) |
|
| 642 | |||
| 643 | /** |
||
| 644 | * Add a condition for the column |
||
| 645 | * @param string $condition The MySQL condition |
||
| 646 | * @param mixed $value Value(s) to pass to MySQL |
||
| 647 | * @param string $type The type of the values |
||
| 648 | * @return void |
||
| 649 | */ |
||
| 650 | 4 | protected function addColumnCondition($condition, $value, $type) |
|
| 667 | |||
| 668 | /** |
||
| 669 | * Get the MySQL extra parameters |
||
| 670 | * |
||
| 671 | * @param bool $respectPagination Whether to respect pagination or not; useful for when pagination should be ignored such as count |
||
| 672 | * @return string |
||
| 673 | */ |
||
| 674 | 4 | protected function createQueryParams($respectPagination = true) |
|
| 687 | |||
| 688 | /** |
||
| 689 | * Get the query parameters |
||
| 690 | * |
||
| 691 | * @return array |
||
| 692 | */ |
||
| 693 | 4 | protected function getParameters() |
|
| 697 | |||
| 698 | /** |
||
| 699 | * Get the query types |
||
| 700 | * |
||
| 701 | * @return string |
||
| 702 | */ |
||
| 703 | 4 | protected function getTypes() |
|
| 707 | |||
| 708 | /** |
||
| 709 | * Get the table of the model |
||
| 710 | * |
||
| 711 | * @return string |
||
| 712 | */ |
||
| 713 | 4 | protected function getTable() |
|
| 719 | |||
| 720 | /** |
||
| 721 | * Get a MySQL query string in the requested format |
||
| 722 | * @param string|string[] $columns The columns that should be included |
||
| 723 | * (without the ID, if an array is provided) |
||
| 724 | * @return string The query |
||
| 725 | */ |
||
| 726 | 4 | protected function createQuery($columns = array()) |
|
| 740 | |||
| 741 | /** |
||
| 742 | * Generate the columns for the query |
||
| 743 | * @param string[] $columns The columns that should be included (without the ID) |
||
| 744 | * @return string |
||
| 745 | */ |
||
| 746 | 4 | private function createQueryColumns($columns = array()) |
|
| 764 | |||
| 765 | /** |
||
| 766 | * Generates all the WHERE conditions for the query |
||
| 767 | * @return string |
||
| 768 | */ |
||
| 769 | 4 | private function createQueryConditions() |
|
| 781 | |||
| 782 | /** |
||
| 783 | * Generates the sorting instructions for the query |
||
| 784 | * @return string |
||
| 785 | */ |
||
| 786 | 4 | private function createQueryOrder() |
|
| 804 | |||
| 805 | /** |
||
| 806 | * Generates the pagination instructions for the query |
||
| 807 | * @return string |
||
| 808 | */ |
||
| 809 | 4 | private function createQueryPagination() |
|
| 834 | } |
||
| 835 |
This check marks private properties in classes that are never used. Those properties can be removed.