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; |
||
| 38 | class ModelQueryBuilder extends QueryBuilder |
||
| 39 | { |
||
| 40 | /** |
||
| 41 | * @var string |
||
| 42 | */ |
||
| 43 | private $modelClass; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var string |
||
| 47 | */ |
||
| 48 | private $mainTableName; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var string |
||
| 52 | */ |
||
| 53 | private $mainAlias; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var Closure |
||
| 57 | */ |
||
| 58 | private $columnMapper; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var ModelSchemeInfoInterface |
||
| 62 | */ |
||
| 63 | private $modelSchemes; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var int |
||
| 67 | */ |
||
| 68 | private $aliasIdCounter = 0; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var array |
||
| 72 | */ |
||
| 73 | private $knownAliases = []; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @param Connection $connection |
||
| 77 | * @param string $modelClass |
||
| 78 | * @param ModelSchemeInfoInterface $modelSchemes |
||
| 79 | * |
||
| 80 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 81 | */ |
||
| 82 | 56 | public function __construct(Connection $connection, string $modelClass, ModelSchemeInfoInterface $modelSchemes) |
|
| 96 | |||
| 97 | /** |
||
| 98 | * @return string |
||
| 99 | */ |
||
| 100 | 56 | public function getModelClass(): string |
|
| 104 | |||
| 105 | /** |
||
| 106 | * Select all fields associated with model. |
||
| 107 | * |
||
| 108 | * @param iterable|null $columns |
||
| 109 | * |
||
| 110 | * @return self |
||
| 111 | */ |
||
| 112 | 50 | public function selectModelColumns(iterable $columns = null): self |
|
| 127 | |||
| 128 | /** |
||
| 129 | * @param Closure $columnMapper |
||
| 130 | * |
||
| 131 | * @return self |
||
| 132 | */ |
||
| 133 | 56 | public function setColumnToDatabaseMapper(Closure $columnMapper): self |
|
| 139 | |||
| 140 | /** |
||
| 141 | * @return self |
||
| 142 | */ |
||
| 143 | 51 | public function fromModelTable(): self |
|
| 152 | |||
| 153 | /** |
||
| 154 | * @param iterable $attributes |
||
| 155 | * |
||
| 156 | * @return self |
||
| 157 | * |
||
| 158 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 159 | */ |
||
| 160 | 4 | public function createModel(iterable $attributes): self |
|
| 172 | |||
| 173 | /** |
||
| 174 | * @param iterable $attributes |
||
| 175 | * |
||
| 176 | * @return self |
||
| 177 | * |
||
| 178 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 179 | */ |
||
| 180 | 5 | public function updateModels(iterable $attributes): self |
|
| 190 | |||
| 191 | /** |
||
| 192 | * @param string $modelClass |
||
| 193 | * @param iterable $attributes |
||
| 194 | * |
||
| 195 | * @return iterable |
||
|
|
|||
| 196 | * |
||
| 197 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 198 | */ |
||
| 199 | 9 | public function bindAttributes(string $modelClass, iterable $attributes): iterable |
|
| 216 | |||
| 217 | /** |
||
| 218 | * @return self |
||
| 219 | */ |
||
| 220 | 5 | public function deleteModels(): self |
|
| 226 | |||
| 227 | /** |
||
| 228 | * @inheritdoc |
||
| 229 | */ |
||
| 230 | 4 | public function prepareCreateInToManyRelationship( |
|
| 247 | |||
| 248 | /** |
||
| 249 | * @inheritdoc |
||
| 250 | */ |
||
| 251 | 2 | public function clearToManyRelationship(string $relationshipName, string $identity): self |
|
| 263 | |||
| 264 | /** |
||
| 265 | * @param iterable $filters |
||
| 266 | * |
||
| 267 | * @return self |
||
| 268 | */ |
||
| 269 | 9 | public function addFiltersWithAndToTable(iterable $filters): self |
|
| 273 | |||
| 274 | /** |
||
| 275 | * @param iterable $filters |
||
| 276 | * |
||
| 277 | * @return self |
||
| 278 | */ |
||
| 279 | 1 | public function addFiltersWithOrToTable(iterable $filters): self |
|
| 283 | |||
| 284 | /** |
||
| 285 | * @param iterable $filters |
||
| 286 | * |
||
| 287 | * @return self |
||
| 288 | */ |
||
| 289 | 38 | public function addFiltersWithAndToAlias(iterable $filters): self |
|
| 293 | |||
| 294 | /** |
||
| 295 | * @param iterable $filters |
||
| 296 | * |
||
| 297 | * @return self |
||
| 298 | */ |
||
| 299 | 2 | public function addFiltersWithOrToAlias(iterable $filters): self |
|
| 303 | |||
| 304 | /** |
||
| 305 | * @param string $relationshipName |
||
| 306 | * @param iterable $relationshipFilters |
||
| 307 | * @param iterable|null $relationshipSorts |
||
| 308 | * |
||
| 309 | * @return self |
||
| 310 | */ |
||
| 311 | 24 | public function addRelationshipFiltersAndSortsWithAnd( |
|
| 325 | |||
| 326 | /** |
||
| 327 | * @return self |
||
| 328 | */ |
||
| 329 | 14 | public function distinct(): self |
|
| 337 | |||
| 338 | /** |
||
| 339 | * @param string $relationshipName |
||
| 340 | * @param iterable $relationshipFilters |
||
| 341 | * @param iterable|null $relationshipSorts |
||
| 342 | * |
||
| 343 | * @return self |
||
| 344 | */ |
||
| 345 | 2 | public function addRelationshipFiltersAndSortsWithOr( |
|
| 359 | |||
| 360 | /** |
||
| 361 | * @param iterable $sortParameters |
||
| 362 | * |
||
| 363 | * @return self |
||
| 364 | */ |
||
| 365 | 8 | public function addSorts(iterable $sortParameters): self |
|
| 376 | |||
| 377 | /** |
||
| 378 | * @param string $tableOrAlias |
||
| 379 | * @param CompositeExpression $filterLink |
||
| 380 | * @param iterable $filters |
||
| 381 | * |
||
| 382 | * @return self |
||
| 383 | */ |
||
| 384 | 43 | private function addFilters(string $tableOrAlias, CompositeExpression $filterLink, iterable $filters): self |
|
| 398 | |||
| 399 | /** |
||
| 400 | * @param string $relationshipName |
||
| 401 | * @param CompositeExpression $filterLink |
||
| 402 | * @param iterable $relationshipFilters |
||
| 403 | * @param iterable|null $relationshipSorts |
||
| 404 | * |
||
| 405 | * @return self |
||
| 406 | */ |
||
| 407 | 26 | private function addRelationshipFiltersAndSorts( |
|
| 447 | |||
| 448 | /** |
||
| 449 | * @return string |
||
| 450 | */ |
||
| 451 | 56 | private function getMainTableName(): string |
|
| 455 | |||
| 456 | /** |
||
| 457 | * @return ModelSchemeInfoInterface |
||
| 458 | */ |
||
| 459 | 56 | private function getModelSchemes(): ModelSchemeInfoInterface |
|
| 463 | |||
| 464 | /** |
||
| 465 | * @return string |
||
| 466 | */ |
||
| 467 | 52 | private function getMainAlias(): string |
|
| 471 | |||
| 472 | /** |
||
| 473 | * @param string $relationshipName |
||
| 474 | * @param CompositeExpression $filterLink |
||
| 475 | * @param iterable $relationshipFilters |
||
| 476 | * @param iterable|null $relationshipSorts |
||
| 477 | * |
||
| 478 | * @return self |
||
| 479 | */ |
||
| 480 | 15 | private function addBelongsToFiltersAndSorts( |
|
| 503 | |||
| 504 | /** |
||
| 505 | * @param string $relationshipName |
||
| 506 | * @param CompositeExpression $filterLink |
||
| 507 | * @param iterable $relationshipFilters |
||
| 508 | * @param iterable|null $relationshipSorts |
||
| 509 | * |
||
| 510 | * @return self |
||
| 511 | */ |
||
| 512 | 12 | private function addHasManyFiltersAndSorts( |
|
| 535 | |||
| 536 | /** |
||
| 537 | * @param string $relationshipName |
||
| 538 | * @param CompositeExpression $targetFilterLink |
||
| 539 | * @param iterable $relationshipFilters |
||
| 540 | * @param iterable|null $relationshipSorts |
||
| 541 | * |
||
| 542 | * @return self |
||
| 543 | */ |
||
| 544 | 9 | private function addBelongsToManyFiltersAndSorts( |
|
| 577 | |||
| 578 | /** |
||
| 579 | * @param string $fromAlias |
||
| 580 | * @param string $fromColumn |
||
| 581 | * @param string $targetTable |
||
| 582 | * @param string $targetColumn |
||
| 583 | * @param CompositeExpression|null $targetFilterLink |
||
| 584 | * @param iterable|null $targetFilterParams |
||
| 585 | * @param iterable|null $relationshipSorts |
||
| 586 | * |
||
| 587 | * @return string |
||
| 588 | */ |
||
| 589 | 26 | private function innerJoinOneTable( |
|
| 626 | |||
| 627 | /** @noinspection PhpTooManyParametersInspection |
||
| 628 | * @param string $fromAlias |
||
| 629 | * @param string $fromColumn |
||
| 630 | * @param string $intTable |
||
| 631 | * @param string $intToFromColumn |
||
| 632 | * @param string $intToTargetColumn |
||
| 633 | * @param string $targetTable |
||
| 634 | * @param string $targetColumn |
||
| 635 | * @param CompositeExpression|null $intFilterLink |
||
| 636 | * @param iterable|null $intFilterParams |
||
| 637 | * @param CompositeExpression|null $targetFilterLink |
||
| 638 | * @param iterable|null $targetFilterParams |
||
| 639 | * @param iterable|null $targetSortParams |
||
| 640 | * |
||
| 641 | * @return string |
||
| 642 | * |
||
| 643 | * @SuppressWarnings(PHPMD.ExcessiveParameterList) |
||
| 644 | */ |
||
| 645 | 9 | private function innerJoinTwoSequentialTables( |
|
| 681 | |||
| 682 | /** |
||
| 683 | * @param string $tableName |
||
| 684 | * |
||
| 685 | * @return string |
||
| 686 | */ |
||
| 687 | 56 | private function createAlias(string $tableName): string |
|
| 694 | |||
| 695 | /** |
||
| 696 | * @inheritdoc |
||
| 697 | */ |
||
| 698 | 55 | private function quoteTableName(string $tableName): string |
|
| 702 | |||
| 703 | /** |
||
| 704 | * @inheritdoc |
||
| 705 | */ |
||
| 706 | 9 | private function quoteColumnName(string $columnName): string |
|
| 710 | |||
| 711 | /** |
||
| 712 | * @inheritdoc |
||
| 713 | */ |
||
| 714 | 56 | private function buildColumnName(string $table, string $column): string |
|
| 718 | |||
| 719 | /** |
||
| 720 | * @param string $column |
||
| 721 | * |
||
| 722 | * @return string |
||
| 723 | */ |
||
| 724 | 1 | public function getQuotedMainTableColumn(string $column): string |
|
| 728 | |||
| 729 | /** |
||
| 730 | * @param string $column |
||
| 731 | * |
||
| 732 | * @return string |
||
| 733 | */ |
||
| 734 | 51 | public function getQuotedMainAliasColumn(string $column): string |
|
| 738 | |||
| 739 | /** |
||
| 740 | * @param CompositeExpression $filterLink |
||
| 741 | * @param string $fullColumnName |
||
| 742 | * @param iterable $operationsWithArgs |
||
| 743 | * |
||
| 744 | * @return void |
||
| 745 | * |
||
| 746 | * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
||
| 747 | */ |
||
| 748 | 53 | private function applyFilter( |
|
| 803 | |||
| 804 | /** |
||
| 805 | * @param iterable $arguments |
||
| 806 | * |
||
| 807 | * @return string |
||
| 808 | */ |
||
| 809 | 51 | private function createSingleNamedParameter(iterable $arguments): string |
|
| 820 | |||
| 821 | /** |
||
| 822 | * @param iterable $arguments |
||
| 823 | * |
||
| 824 | * @return string[] |
||
| 825 | */ |
||
| 826 | 6 | private function createNamedParameterArray(iterable $arguments): array |
|
| 836 | |||
| 837 | /** |
||
| 838 | * @return Closure |
||
| 839 | */ |
||
| 840 | 50 | private function getColumnToDatabaseMapper(): Closure |
|
| 844 | |||
| 845 | /** |
||
| 846 | * @param mixed $value |
||
| 847 | * |
||
| 848 | * @return int |
||
| 849 | * |
||
| 850 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
| 851 | */ |
||
| 852 | 52 | private function getPdoType($value): int |
|
| 870 | } |
||
| 871 |
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.