Complex classes like ModelQueryBuilder 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 ModelQueryBuilder, and based on these observations, apply Extract Interface, too.
| 1 | <?php namespace Limoncello\Flute\Adapters; |
||
| 41 | class ModelQueryBuilder extends QueryBuilder |
||
| 42 | { |
||
| 43 | /** |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | private $modelClass; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var string |
||
| 50 | */ |
||
| 51 | private $mainTableName; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | private $mainAlias; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var Closure |
||
| 60 | */ |
||
| 61 | private $columnMapper; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var ModelSchemaInfoInterface |
||
| 65 | */ |
||
| 66 | private $modelSchemas; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var int |
||
| 70 | */ |
||
| 71 | private $aliasIdCounter = 0; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var array |
||
| 75 | */ |
||
| 76 | private $knownAliases = []; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var Type|null |
||
| 80 | */ |
||
| 81 | private $dateTimeType; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @param Connection $connection |
||
| 85 | * @param string $modelClass |
||
| 86 | * @param ModelSchemaInfoInterface $modelSchemas |
||
| 87 | * |
||
| 88 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 89 | */ |
||
| 90 | 63 | public function __construct(Connection $connection, string $modelClass, ModelSchemaInfoInterface $modelSchemas) |
|
| 104 | |||
| 105 | /** |
||
| 106 | * @return string |
||
| 107 | */ |
||
| 108 | 63 | public function getModelClass(): string |
|
| 112 | |||
| 113 | /** |
||
| 114 | * Select all fields associated with model. |
||
| 115 | * |
||
| 116 | * @param iterable|null $columns |
||
| 117 | * |
||
| 118 | * @return self |
||
| 119 | */ |
||
| 120 | 56 | public function selectModelColumns(iterable $columns = null): self |
|
| 135 | |||
| 136 | /** |
||
| 137 | * @return self |
||
| 138 | */ |
||
| 139 | 14 | public function distinct(): self |
|
| 147 | |||
| 148 | /** |
||
| 149 | * @param Closure $columnMapper |
||
| 150 | * |
||
| 151 | * @return self |
||
| 152 | */ |
||
| 153 | 63 | public function setColumnToDatabaseMapper(Closure $columnMapper): self |
|
| 159 | |||
| 160 | /** |
||
| 161 | * @return self |
||
| 162 | */ |
||
| 163 | 56 | public function fromModelTable(): self |
|
| 172 | |||
| 173 | /** |
||
| 174 | * @param iterable $attributes |
||
| 175 | * |
||
| 176 | * @return self |
||
| 177 | * |
||
| 178 | * @throws DBALException |
||
| 179 | * |
||
| 180 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 181 | */ |
||
| 182 | 4 | public function createModel(iterable $attributes): self |
|
| 194 | |||
| 195 | /** |
||
| 196 | * @param iterable $attributes |
||
| 197 | * |
||
| 198 | * @return self |
||
| 199 | * |
||
| 200 | * @throws DBALException |
||
| 201 | * |
||
| 202 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 203 | */ |
||
| 204 | 5 | public function updateModels(iterable $attributes): self |
|
| 214 | |||
| 215 | /** |
||
| 216 | * @param string $modelClass |
||
| 217 | * @param iterable $attributes |
||
| 218 | * |
||
| 219 | * @return iterable |
||
|
|
|||
| 220 | * |
||
| 221 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 222 | * |
||
| 223 | * @throws DBALException |
||
| 224 | */ |
||
| 225 | 9 | public function bindAttributes(string $modelClass, iterable $attributes): iterable |
|
| 241 | |||
| 242 | /** |
||
| 243 | * @return self |
||
| 244 | */ |
||
| 245 | 4 | public function deleteModels(): self |
|
| 251 | |||
| 252 | /** |
||
| 253 | * @param string $relationshipName |
||
| 254 | * @param string $identity |
||
| 255 | * @param string $secondaryIdBindName |
||
| 256 | * |
||
| 257 | * @return self |
||
| 258 | */ |
||
| 259 | 5 | public function prepareCreateInToManyRelationship( |
|
| 276 | |||
| 277 | /** |
||
| 278 | * @param string $relationshipName |
||
| 279 | * @param string $identity |
||
| 280 | * @param iterable $secondaryIds |
||
| 281 | * |
||
| 282 | * @return ModelQueryBuilder |
||
| 283 | * |
||
| 284 | * @throws DBALException |
||
| 285 | */ |
||
| 286 | 1 | public function prepareDeleteInToManyRelationship( |
|
| 308 | |||
| 309 | /** |
||
| 310 | * @param string $relationshipName |
||
| 311 | * @param string $identity |
||
| 312 | * |
||
| 313 | * @return self |
||
| 314 | * |
||
| 315 | * @throws DBALException |
||
| 316 | */ |
||
| 317 | 2 | public function clearToManyRelationship(string $relationshipName, string $identity): self |
|
| 332 | |||
| 333 | /** |
||
| 334 | * @param iterable $filters |
||
| 335 | * |
||
| 336 | * @return self |
||
| 337 | * |
||
| 338 | * @throws DBALException |
||
| 339 | */ |
||
| 340 | 8 | public function addFiltersWithAndToTable(iterable $filters): self |
|
| 348 | |||
| 349 | /** |
||
| 350 | * @param iterable $filters |
||
| 351 | * |
||
| 352 | * @return self |
||
| 353 | * |
||
| 354 | * @throws DBALException |
||
| 355 | */ |
||
| 356 | 1 | public function addFiltersWithOrToTable(iterable $filters): self |
|
| 364 | |||
| 365 | /** |
||
| 366 | * @param iterable $filters |
||
| 367 | * |
||
| 368 | * @return self |
||
| 369 | * |
||
| 370 | * @throws DBALException |
||
| 371 | */ |
||
| 372 | 39 | public function addFiltersWithAndToAlias(iterable $filters): self |
|
| 380 | |||
| 381 | /** |
||
| 382 | * @param iterable $filters |
||
| 383 | * |
||
| 384 | * @return self |
||
| 385 | * |
||
| 386 | * @throws DBALException |
||
| 387 | */ |
||
| 388 | 2 | public function addFiltersWithOrToAlias(iterable $filters): self |
|
| 396 | |||
| 397 | /** |
||
| 398 | * @param string $relationshipName |
||
| 399 | * @param iterable|null $relationshipFilters |
||
| 400 | * @param iterable|null $relationshipSorts |
||
| 401 | * |
||
| 402 | * @return self |
||
| 403 | * |
||
| 404 | * @throws DBALException |
||
| 405 | */ |
||
| 406 | 27 | public function addRelationshipFiltersAndSortsWithAnd( |
|
| 423 | |||
| 424 | /** |
||
| 425 | * @param string $relationshipName |
||
| 426 | * @param iterable $relationshipFilters |
||
| 427 | * @param iterable|null $relationshipSorts |
||
| 428 | * |
||
| 429 | * @return self |
||
| 430 | * |
||
| 431 | * @throws DBALException |
||
| 432 | */ |
||
| 433 | 2 | public function addRelationshipFiltersAndSortsWithOr( |
|
| 450 | |||
| 451 | /** |
||
| 452 | * @param iterable $sortParameters |
||
| 453 | * |
||
| 454 | * @return self |
||
| 455 | */ |
||
| 456 | 8 | public function addSorts(iterable $sortParameters): self |
|
| 460 | |||
| 461 | /** |
||
| 462 | * @param string $column |
||
| 463 | * |
||
| 464 | * @return string |
||
| 465 | */ |
||
| 466 | 1 | public function getQuotedMainTableColumn(string $column): string |
|
| 470 | |||
| 471 | /** |
||
| 472 | * @param string $column |
||
| 473 | * |
||
| 474 | * @return string |
||
| 475 | */ |
||
| 476 | 57 | public function getQuotedMainAliasColumn(string $column): string |
|
| 480 | |||
| 481 | /** |
||
| 482 | * @param string $name |
||
| 483 | * |
||
| 484 | * @return string Table alias. |
||
| 485 | */ |
||
| 486 | 29 | public function createRelationshipAlias(string $name): string |
|
| 535 | |||
| 536 | /** |
||
| 537 | * @return string |
||
| 538 | */ |
||
| 539 | 57 | public function getAlias(): string |
|
| 543 | |||
| 544 | /** |
||
| 545 | * @param CompositeExpression $expression |
||
| 546 | * @param string $tableOrAlias |
||
| 547 | * @param iterable $filters |
||
| 548 | * |
||
| 549 | * @return self |
||
| 550 | * |
||
| 551 | * @throws DBALException |
||
| 552 | * @throws InvalidArgumentException |
||
| 553 | */ |
||
| 554 | 59 | public function applyFilters(CompositeExpression $expression, string $tableOrAlias, iterable $filters): self |
|
| 574 | |||
| 575 | /** |
||
| 576 | * @param string $tableOrAlias |
||
| 577 | * @param iterable $sorts |
||
| 578 | * |
||
| 579 | * @return self |
||
| 580 | */ |
||
| 581 | 30 | public function applySorts(string $tableOrAlias, iterable $sorts): self |
|
| 591 | |||
| 592 | /** |
||
| 593 | * @param string $table |
||
| 594 | * @param string $column |
||
| 595 | * |
||
| 596 | * @return string |
||
| 597 | */ |
||
| 598 | 62 | public function buildColumnName(string $table, string $column): string |
|
| 602 | |||
| 603 | /** |
||
| 604 | * @param $value |
||
| 605 | * |
||
| 606 | * @return string |
||
| 607 | * |
||
| 608 | * @throws DBALException |
||
| 609 | */ |
||
| 610 | 57 | public function createSingleValueNamedParameter($value): string |
|
| 616 | |||
| 617 | /** |
||
| 618 | * @param iterable $values |
||
| 619 | * |
||
| 620 | * @return array |
||
| 621 | * |
||
| 622 | * @throws DBALException |
||
| 623 | */ |
||
| 624 | 18 | public function createArrayValuesNamedParameter(iterable $values): array |
|
| 634 | |||
| 635 | /** |
||
| 636 | * @param string $tableName |
||
| 637 | * |
||
| 638 | * @return string |
||
| 639 | */ |
||
| 640 | 63 | public function createAlias(string $tableName): string |
|
| 647 | |||
| 648 | /** |
||
| 649 | * @param string $name |
||
| 650 | * |
||
| 651 | * @return Type |
||
| 652 | * |
||
| 653 | * @throws DBALException |
||
| 654 | * |
||
| 655 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 656 | */ |
||
| 657 | 9 | protected function getDbalType(string $name): Type |
|
| 664 | |||
| 665 | /** |
||
| 666 | * @return string |
||
| 667 | */ |
||
| 668 | 63 | private function getTableName(): string |
|
| 672 | |||
| 673 | /** |
||
| 674 | * @return ModelSchemaInfoInterface |
||
| 675 | */ |
||
| 676 | 63 | private function getModelSchemas(): ModelSchemaInfoInterface |
|
| 680 | |||
| 681 | /** |
||
| 682 | * @param string $fromAlias |
||
| 683 | * @param string $fromColumn |
||
| 684 | * @param string $targetTable |
||
| 685 | * @param string $targetColumn |
||
| 686 | * |
||
| 687 | * @return string |
||
| 688 | */ |
||
| 689 | 29 | private function innerJoinOneTable( |
|
| 708 | |||
| 709 | /** |
||
| 710 | * @param string $fromAlias |
||
| 711 | * @param string $fromColumn |
||
| 712 | * @param string $intTable |
||
| 713 | * @param string $intToFromColumn |
||
| 714 | * @param string $intToTargetColumn |
||
| 715 | * @param string $targetTable |
||
| 716 | * @param string $targetColumn |
||
| 717 | * |
||
| 718 | * @return string |
||
| 719 | */ |
||
| 720 | 10 | private function innerJoinTwoSequentialTables( |
|
| 734 | |||
| 735 | /** |
||
| 736 | * @param string $tableName |
||
| 737 | * |
||
| 738 | * @return string |
||
| 739 | */ |
||
| 740 | 63 | private function quoteTableName(string $tableName): string |
|
| 744 | |||
| 745 | /** |
||
| 746 | * @param string $columnName |
||
| 747 | * |
||
| 748 | * @return string |
||
| 749 | */ |
||
| 750 | 63 | private function quoteColumnName(string $columnName): string |
|
| 754 | |||
| 755 | /** |
||
| 756 | * @param string $fullColumnName |
||
| 757 | * @param int $operation |
||
| 758 | * @param iterable $arguments |
||
| 759 | * |
||
| 760 | * @return string |
||
| 761 | * |
||
| 762 | * @throws DBALException |
||
| 763 | * @throws InvalidArgumentException |
||
| 764 | * |
||
| 765 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 766 | * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
||
| 767 | */ |
||
| 768 | 58 | private function createFilterExpression(string $fullColumnName, int $operation, iterable $arguments): string |
|
| 823 | |||
| 824 | /** |
||
| 825 | * @param iterable $arguments |
||
| 826 | * |
||
| 827 | * @return mixed |
||
| 828 | * |
||
| 829 | * @throws InvalidArgumentException |
||
| 830 | */ |
||
| 831 | 54 | private function firstValue(iterable $arguments) |
|
| 840 | |||
| 841 | /** |
||
| 842 | * @return Closure |
||
| 843 | */ |
||
| 844 | 56 | private function getColumnToDatabaseMapper(): Closure |
|
| 848 | |||
| 849 | /** |
||
| 850 | * @param mixed $value |
||
| 851 | * |
||
| 852 | * @return mixed |
||
| 853 | * |
||
| 854 | * @throws DBALException |
||
| 855 | */ |
||
| 856 | 57 | private function getPdoValue($value) |
|
| 860 | |||
| 861 | /** |
||
| 862 | * @param DateTimeInterface $dateTime |
||
| 863 | * |
||
| 864 | * @return string |
||
| 865 | * |
||
| 866 | * @throws DBALException |
||
| 867 | */ |
||
| 868 | 1 | private function convertDataTimeToDatabaseFormat(DateTimeInterface $dateTime): string |
|
| 875 | |||
| 876 | /** |
||
| 877 | * @param mixed $value |
||
| 878 | * |
||
| 879 | * @return int |
||
| 880 | * |
||
| 881 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
| 882 | */ |
||
| 883 | 57 | private function getPdoType($value): int |
|
| 903 | |||
| 904 | /** |
||
| 905 | * @return Type |
||
| 906 | * |
||
| 907 | * @throws DBALException |
||
| 908 | * |
||
| 909 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 910 | */ |
||
| 911 | 1 | private function getDateTimeType(): Type |
|
| 919 | } |
||
| 920 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.