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; |
||
| 42 | class ModelQueryBuilder extends QueryBuilder |
||
| 43 | { |
||
| 44 | /** |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | private $modelClass; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var string |
||
| 51 | */ |
||
| 52 | private $mainTableName; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var string |
||
| 56 | */ |
||
| 57 | private $mainAlias; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var Closure |
||
| 61 | */ |
||
| 62 | private $columnMapper; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var ModelSchemeInfoInterface |
||
| 66 | */ |
||
| 67 | private $modelSchemes; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var int |
||
| 71 | */ |
||
| 72 | private $aliasIdCounter = 0; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var array |
||
| 76 | */ |
||
| 77 | private $knownAliases = []; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var null|Closure |
||
| 81 | */ |
||
| 82 | private $dtToDbConverter = null; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @param Connection $connection |
||
| 86 | * @param string $modelClass |
||
| 87 | * @param ModelSchemeInfoInterface $modelSchemes |
||
| 88 | * |
||
| 89 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 90 | */ |
||
| 91 | 59 | public function __construct(Connection $connection, string $modelClass, ModelSchemeInfoInterface $modelSchemes) |
|
| 105 | |||
| 106 | /** |
||
| 107 | * @return string |
||
| 108 | */ |
||
| 109 | 59 | public function getModelClass(): string |
|
| 113 | |||
| 114 | /** |
||
| 115 | * Select all fields associated with model. |
||
| 116 | * |
||
| 117 | * @param iterable|null $columns |
||
| 118 | * |
||
| 119 | * @return self |
||
| 120 | */ |
||
| 121 | 53 | public function selectModelColumns(iterable $columns = null): self |
|
| 136 | |||
| 137 | /** |
||
| 138 | * @param Closure $columnMapper |
||
| 139 | * |
||
| 140 | * @return self |
||
| 141 | */ |
||
| 142 | 59 | public function setColumnToDatabaseMapper(Closure $columnMapper): self |
|
| 148 | |||
| 149 | /** |
||
| 150 | * @return self |
||
| 151 | */ |
||
| 152 | 54 | public function fromModelTable(): self |
|
| 161 | |||
| 162 | /** |
||
| 163 | * @param iterable $attributes |
||
| 164 | * |
||
| 165 | * @return self |
||
| 166 | * |
||
| 167 | * @throws DBALException |
||
| 168 | * |
||
| 169 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 170 | */ |
||
| 171 | 4 | public function createModel(iterable $attributes): self |
|
| 183 | |||
| 184 | /** |
||
| 185 | * @param iterable $attributes |
||
| 186 | * |
||
| 187 | * @return self |
||
| 188 | * |
||
| 189 | * @throws DBALException |
||
| 190 | * |
||
| 191 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 192 | */ |
||
| 193 | 5 | public function updateModels(iterable $attributes): self |
|
| 203 | |||
| 204 | /** |
||
| 205 | * @param string $modelClass |
||
| 206 | * @param iterable $attributes |
||
| 207 | * |
||
| 208 | * @return iterable |
||
|
|
|||
| 209 | * |
||
| 210 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 211 | * |
||
| 212 | * @throws DBALException |
||
| 213 | */ |
||
| 214 | 9 | public function bindAttributes(string $modelClass, iterable $attributes): iterable |
|
| 230 | |||
| 231 | /** |
||
| 232 | * @return self |
||
| 233 | */ |
||
| 234 | 5 | public function deleteModels(): self |
|
| 240 | |||
| 241 | /** |
||
| 242 | * @inheritdoc |
||
| 243 | */ |
||
| 244 | 4 | public function prepareCreateInToManyRelationship( |
|
| 261 | |||
| 262 | /** |
||
| 263 | * @inheritdoc |
||
| 264 | */ |
||
| 265 | 2 | public function clearToManyRelationship(string $relationshipName, string $identity): self |
|
| 277 | |||
| 278 | /** |
||
| 279 | * @param iterable $filters |
||
| 280 | * |
||
| 281 | * @return self |
||
| 282 | * |
||
| 283 | * @throws DBALException |
||
| 284 | */ |
||
| 285 | 9 | public function addFiltersWithAndToTable(iterable $filters): self |
|
| 289 | |||
| 290 | /** |
||
| 291 | * @param iterable $filters |
||
| 292 | * |
||
| 293 | * @return self |
||
| 294 | * |
||
| 295 | * @throws DBALException |
||
| 296 | */ |
||
| 297 | 1 | public function addFiltersWithOrToTable(iterable $filters): self |
|
| 301 | |||
| 302 | /** |
||
| 303 | * @param iterable $filters |
||
| 304 | * |
||
| 305 | * @return self |
||
| 306 | * |
||
| 307 | * @throws DBALException |
||
| 308 | */ |
||
| 309 | 38 | public function addFiltersWithAndToAlias(iterable $filters): self |
|
| 313 | |||
| 314 | /** |
||
| 315 | * @param iterable $filters |
||
| 316 | * |
||
| 317 | * @return self |
||
| 318 | * |
||
| 319 | * @throws DBALException |
||
| 320 | */ |
||
| 321 | 2 | public function addFiltersWithOrToAlias(iterable $filters): self |
|
| 325 | |||
| 326 | /** |
||
| 327 | * @param string $relationshipName |
||
| 328 | * @param iterable $relationshipFilters |
||
| 329 | * @param iterable|null $relationshipSorts |
||
| 330 | * |
||
| 331 | * @return self |
||
| 332 | * |
||
| 333 | * @throws DBALException |
||
| 334 | */ |
||
| 335 | 27 | public function addRelationshipFiltersAndSortsWithAnd( |
|
| 349 | |||
| 350 | /** |
||
| 351 | * @return self |
||
| 352 | */ |
||
| 353 | 15 | public function distinct(): self |
|
| 361 | |||
| 362 | /** |
||
| 363 | * @param string $relationshipName |
||
| 364 | * @param iterable $relationshipFilters |
||
| 365 | * @param iterable|null $relationshipSorts |
||
| 366 | * |
||
| 367 | * @return self |
||
| 368 | * |
||
| 369 | * @throws DBALException |
||
| 370 | */ |
||
| 371 | 2 | public function addRelationshipFiltersAndSortsWithOr( |
|
| 385 | |||
| 386 | /** |
||
| 387 | * @param iterable $sortParameters |
||
| 388 | * |
||
| 389 | * @return self |
||
| 390 | */ |
||
| 391 | 8 | public function addSorts(iterable $sortParameters): self |
|
| 402 | |||
| 403 | /** |
||
| 404 | * @param string $tableOrAlias |
||
| 405 | * @param CompositeExpression $filterLink |
||
| 406 | * @param iterable $filters |
||
| 407 | * |
||
| 408 | * @return self |
||
| 409 | * |
||
| 410 | * @throws DBALException |
||
| 411 | */ |
||
| 412 | 43 | private function addFilters(string $tableOrAlias, CompositeExpression $filterLink, iterable $filters): self |
|
| 424 | |||
| 425 | /** |
||
| 426 | * @param string $relationshipName |
||
| 427 | * @param CompositeExpression $filterLink |
||
| 428 | * @param iterable $relationshipFilters |
||
| 429 | * @param iterable|null $relationshipSorts |
||
| 430 | * |
||
| 431 | * @return self |
||
| 432 | * |
||
| 433 | * @throws DBALException |
||
| 434 | */ |
||
| 435 | 29 | private function addRelationshipFiltersAndSorts( |
|
| 475 | |||
| 476 | /** |
||
| 477 | * @return string |
||
| 478 | */ |
||
| 479 | 59 | private function getMainTableName(): string |
|
| 483 | |||
| 484 | /** |
||
| 485 | * @return ModelSchemeInfoInterface |
||
| 486 | */ |
||
| 487 | 59 | private function getModelSchemes(): ModelSchemeInfoInterface |
|
| 491 | |||
| 492 | /** |
||
| 493 | * @return string |
||
| 494 | */ |
||
| 495 | 55 | private function getMainAlias(): string |
|
| 499 | |||
| 500 | /** |
||
| 501 | * @param string $relationshipName |
||
| 502 | * @param CompositeExpression $filterLink |
||
| 503 | * @param iterable $relationshipFilters |
||
| 504 | * @param iterable|null $relationshipSorts |
||
| 505 | * |
||
| 506 | * @return self |
||
| 507 | * |
||
| 508 | * @throws DBALException |
||
| 509 | */ |
||
| 510 | 18 | private function addBelongsToFiltersAndSorts( |
|
| 535 | |||
| 536 | /** |
||
| 537 | * @param string $relationshipName |
||
| 538 | * @param CompositeExpression $filterLink |
||
| 539 | * @param iterable $relationshipFilters |
||
| 540 | * @param iterable|null $relationshipSorts |
||
| 541 | * |
||
| 542 | * @return self |
||
| 543 | * |
||
| 544 | * @throws DBALException |
||
| 545 | */ |
||
| 546 | 12 | private function addHasManyFiltersAndSorts( |
|
| 571 | |||
| 572 | /** |
||
| 573 | * @param string $relationshipName |
||
| 574 | * @param CompositeExpression $targetFilterLink |
||
| 575 | * @param iterable $relationshipFilters |
||
| 576 | * @param iterable|null $relationshipSorts |
||
| 577 | * |
||
| 578 | * @return self |
||
| 579 | * |
||
| 580 | * @throws DBALException |
||
| 581 | */ |
||
| 582 | 9 | private function addBelongsToManyFiltersAndSorts( |
|
| 617 | |||
| 618 | /** |
||
| 619 | * @param string $fromAlias |
||
| 620 | * @param string $fromColumn |
||
| 621 | * @param string $targetTable |
||
| 622 | * @param string $targetColumn |
||
| 623 | * @param CompositeExpression|null $targetFilterLink |
||
| 624 | * @param iterable|null $targetFilterParams |
||
| 625 | * @param iterable|null $relationshipSorts |
||
| 626 | * |
||
| 627 | * @return string |
||
| 628 | * |
||
| 629 | * @throws DBALException |
||
| 630 | */ |
||
| 631 | 29 | private function innerJoinOneTable( |
|
| 668 | |||
| 669 | /** @noinspection PhpTooManyParametersInspection |
||
| 670 | * @param string $fromAlias |
||
| 671 | * @param string $fromColumn |
||
| 672 | * @param string $intTable |
||
| 673 | * @param string $intToFromColumn |
||
| 674 | * @param string $intToTargetColumn |
||
| 675 | * @param string $targetTable |
||
| 676 | * @param string $targetColumn |
||
| 677 | * @param CompositeExpression|null $intFilterLink |
||
| 678 | * @param iterable|null $intFilterParams |
||
| 679 | * @param CompositeExpression|null $targetFilterLink |
||
| 680 | * @param iterable|null $targetFilterParams |
||
| 681 | * @param iterable|null $targetSortParams |
||
| 682 | * |
||
| 683 | * @return string |
||
| 684 | * |
||
| 685 | * @SuppressWarnings(PHPMD.ExcessiveParameterList) |
||
| 686 | * |
||
| 687 | * @throws DBALException |
||
| 688 | */ |
||
| 689 | 9 | private function innerJoinTwoSequentialTables( |
|
| 725 | |||
| 726 | /** |
||
| 727 | * @param string $tableName |
||
| 728 | * |
||
| 729 | * @return string |
||
| 730 | */ |
||
| 731 | 59 | private function createAlias(string $tableName): string |
|
| 738 | |||
| 739 | /** |
||
| 740 | * @inheritdoc |
||
| 741 | */ |
||
| 742 | 58 | private function quoteTableName(string $tableName): string |
|
| 746 | |||
| 747 | /** |
||
| 748 | * @inheritdoc |
||
| 749 | */ |
||
| 750 | 9 | private function quoteColumnName(string $columnName): string |
|
| 754 | |||
| 755 | /** |
||
| 756 | * @inheritdoc |
||
| 757 | */ |
||
| 758 | 59 | private function buildColumnName(string $table, string $column): string |
|
| 762 | |||
| 763 | /** |
||
| 764 | * @param string $column |
||
| 765 | * |
||
| 766 | * @return string |
||
| 767 | */ |
||
| 768 | 1 | public function getQuotedMainTableColumn(string $column): string |
|
| 772 | |||
| 773 | /** |
||
| 774 | * @param string $column |
||
| 775 | * |
||
| 776 | * @return string |
||
| 777 | */ |
||
| 778 | 54 | public function getQuotedMainAliasColumn(string $column): string |
|
| 782 | |||
| 783 | /** |
||
| 784 | * @param string $name |
||
| 785 | * |
||
| 786 | * @return Type |
||
| 787 | * |
||
| 788 | * @throws DBALException |
||
| 789 | */ |
||
| 790 | 10 | protected function getDbalType(string $name): Type |
|
| 797 | |||
| 798 | /** |
||
| 799 | * @param CompositeExpression $filterLink |
||
| 800 | * @param string $fullColumnName |
||
| 801 | * @param iterable $operationsWithArgs |
||
| 802 | * |
||
| 803 | * @return void |
||
| 804 | * |
||
| 805 | * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
||
| 806 | * |
||
| 807 | * @throws DBALException |
||
| 808 | */ |
||
| 809 | 55 | private function applyFilter( |
|
| 864 | |||
| 865 | /** |
||
| 866 | * @param iterable $arguments |
||
| 867 | * |
||
| 868 | * @return string |
||
| 869 | * |
||
| 870 | * @throws DBALException |
||
| 871 | */ |
||
| 872 | 52 | private function createSingleNamedParameter(iterable $arguments): string |
|
| 883 | |||
| 884 | /** |
||
| 885 | * @param iterable $arguments |
||
| 886 | * |
||
| 887 | * @return string[] |
||
| 888 | * |
||
| 889 | * @throws DBALException |
||
| 890 | */ |
||
| 891 | 14 | private function createNamedParameterArray(iterable $arguments): array |
|
| 901 | |||
| 902 | /** |
||
| 903 | * @return Closure |
||
| 904 | */ |
||
| 905 | 53 | private function getColumnToDatabaseMapper(): Closure |
|
| 909 | |||
| 910 | /** |
||
| 911 | * @param mixed $value |
||
| 912 | * |
||
| 913 | * @return mixed |
||
| 914 | * |
||
| 915 | * @throws DBALException |
||
| 916 | */ |
||
| 917 | 54 | private function getPdoValue($value) |
|
| 921 | |||
| 922 | /** |
||
| 923 | * @param DateTimeInterface $dateTime |
||
| 924 | * |
||
| 925 | * @return string |
||
| 926 | * |
||
| 927 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 928 | * |
||
| 929 | * @throws DBALException |
||
| 930 | */ |
||
| 931 | 1 | private function convertDataTimeToDatabaseFormat(DateTimeInterface $dateTime): string |
|
| 943 | |||
| 944 | /** |
||
| 945 | * @param mixed $value |
||
| 946 | * |
||
| 947 | * @return int |
||
| 948 | * |
||
| 949 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
| 950 | */ |
||
| 951 | 54 | private function getPdoType($value): int |
|
| 971 | } |
||
| 972 |
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.