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 | 52 | public function __construct(Connection $connection, string $modelClass, ModelSchemeInfoInterface $modelSchemes) |
|
| 94 | |||
| 95 | /** |
||
| 96 | * @return string |
||
| 97 | */ |
||
| 98 | 52 | public function getModelClass(): string |
|
| 102 | |||
| 103 | /** |
||
| 104 | * Select all fields associated with model. |
||
| 105 | * |
||
| 106 | * @param iterable|null $columns |
||
| 107 | * |
||
| 108 | * @return self |
||
| 109 | */ |
||
| 110 | 49 | public function selectModelColumns(iterable $columns = null): self |
|
| 125 | |||
| 126 | /** |
||
| 127 | * @param Closure $columnMapper |
||
| 128 | * |
||
| 129 | * @return self |
||
| 130 | */ |
||
| 131 | 52 | public function setColumnToDatabaseMapper(Closure $columnMapper): self |
|
| 137 | |||
| 138 | /** |
||
| 139 | * @return self |
||
| 140 | */ |
||
| 141 | 50 | public function fromModelTable(): self |
|
| 150 | |||
| 151 | /** |
||
| 152 | * @param iterable $attributes |
||
| 153 | * |
||
| 154 | * @return self |
||
| 155 | * |
||
| 156 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 157 | */ |
||
| 158 | 4 | public function createModel(iterable $attributes): self |
|
| 170 | |||
| 171 | /** |
||
| 172 | * @param iterable $attributes |
||
| 173 | * |
||
| 174 | * @return self |
||
| 175 | * |
||
| 176 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 177 | */ |
||
| 178 | 3 | public function updateModels(iterable $attributes): self |
|
| 188 | |||
| 189 | /** |
||
| 190 | * @param string $modelClass |
||
| 191 | * @param iterable $attributes |
||
| 192 | * |
||
| 193 | * @return iterable |
||
|
|
|||
| 194 | * |
||
| 195 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 196 | */ |
||
| 197 | 7 | public function bindAttributes(string $modelClass, iterable $attributes): iterable |
|
| 214 | |||
| 215 | /** |
||
| 216 | * @return self |
||
| 217 | */ |
||
| 218 | 5 | public function deleteModels(): self |
|
| 224 | |||
| 225 | /** |
||
| 226 | * @inheritdoc |
||
| 227 | */ |
||
| 228 | 4 | public function prepareCreateInToManyRelationship( |
|
| 245 | |||
| 246 | /** |
||
| 247 | * @inheritdoc |
||
| 248 | */ |
||
| 249 | 2 | public function clearToManyRelationship(string $relationshipName, string $identity): self |
|
| 261 | |||
| 262 | /** |
||
| 263 | * @param iterable $filters |
||
| 264 | * |
||
| 265 | * @return self |
||
| 266 | */ |
||
| 267 | 8 | public function addFiltersWithAndToTable(iterable $filters): self |
|
| 271 | |||
| 272 | /** |
||
| 273 | * @param iterable $filters |
||
| 274 | * |
||
| 275 | * @return self |
||
| 276 | */ |
||
| 277 | public function addFiltersWithOrToTable(iterable $filters): self |
||
| 281 | |||
| 282 | /** |
||
| 283 | * @param iterable $filters |
||
| 284 | * |
||
| 285 | * @return self |
||
| 286 | */ |
||
| 287 | 37 | public function addFiltersWithAndToAlias(iterable $filters): self |
|
| 291 | |||
| 292 | /** |
||
| 293 | * @param iterable $filters |
||
| 294 | * |
||
| 295 | * @return self |
||
| 296 | */ |
||
| 297 | 2 | public function addFiltersWithOrToAlias(iterable $filters): self |
|
| 301 | |||
| 302 | /** |
||
| 303 | * @param string $relationshipName |
||
| 304 | * @param iterable $relationshipFilters |
||
| 305 | * @param iterable|null $relationshipSorts |
||
| 306 | * |
||
| 307 | * @return self |
||
| 308 | */ |
||
| 309 | 24 | public function addRelationshipFiltersAndSortsWithAnd( |
|
| 323 | |||
| 324 | /** |
||
| 325 | * @return self |
||
| 326 | */ |
||
| 327 | 13 | public function distinct(): self |
|
| 335 | |||
| 336 | /** |
||
| 337 | * @param string $relationshipName |
||
| 338 | * @param iterable $relationshipFilters |
||
| 339 | * @param iterable|null $relationshipSorts |
||
| 340 | * |
||
| 341 | * @return self |
||
| 342 | */ |
||
| 343 | 1 | public function addRelationshipFiltersAndSortsWithOr( |
|
| 357 | |||
| 358 | /** |
||
| 359 | * @param iterable $sortParameters |
||
| 360 | * |
||
| 361 | * @return self |
||
| 362 | */ |
||
| 363 | 7 | public function addSorts(iterable $sortParameters): self |
|
| 374 | |||
| 375 | /** |
||
| 376 | * @param string $tableOrAlias |
||
| 377 | * @param CompositeExpression $filterLink |
||
| 378 | * @param iterable $filters |
||
| 379 | * |
||
| 380 | * @return self |
||
| 381 | */ |
||
| 382 | 40 | private function addFilters(string $tableOrAlias, CompositeExpression $filterLink, iterable $filters): self |
|
| 396 | |||
| 397 | /** |
||
| 398 | * @param string $relationshipName |
||
| 399 | * @param CompositeExpression $filterLink |
||
| 400 | * @param iterable $relationshipFilters |
||
| 401 | * @param iterable|null $relationshipSorts |
||
| 402 | * |
||
| 403 | * @return self |
||
| 404 | */ |
||
| 405 | 25 | private function addRelationshipFiltersAndSorts( |
|
| 445 | |||
| 446 | /** |
||
| 447 | * @return string |
||
| 448 | */ |
||
| 449 | 52 | private function getMainTableName(): string |
|
| 453 | |||
| 454 | /** |
||
| 455 | * @return ModelSchemeInfoInterface |
||
| 456 | */ |
||
| 457 | 52 | private function getModelSchemes(): ModelSchemeInfoInterface |
|
| 461 | |||
| 462 | /** |
||
| 463 | * @return string |
||
| 464 | */ |
||
| 465 | 50 | private function getMainAlias(): string |
|
| 469 | |||
| 470 | /** |
||
| 471 | * @param string $relationshipName |
||
| 472 | * @param CompositeExpression $filterLink |
||
| 473 | * @param iterable $relationshipFilters |
||
| 474 | * @param iterable|null $relationshipSorts |
||
| 475 | * |
||
| 476 | * @return self |
||
| 477 | */ |
||
| 478 | 14 | private function addBelongsToFiltersAndSorts( |
|
| 501 | |||
| 502 | /** |
||
| 503 | * @param string $relationshipName |
||
| 504 | * @param CompositeExpression $filterLink |
||
| 505 | * @param iterable $relationshipFilters |
||
| 506 | * @param iterable|null $relationshipSorts |
||
| 507 | * |
||
| 508 | * @return self |
||
| 509 | */ |
||
| 510 | 12 | private function addHasManyFiltersAndSorts( |
|
| 533 | |||
| 534 | /** |
||
| 535 | * @param string $relationshipName |
||
| 536 | * @param CompositeExpression $targetFilterLink |
||
| 537 | * @param iterable $relationshipFilters |
||
| 538 | * @param iterable|null $relationshipSorts |
||
| 539 | * |
||
| 540 | * @return self |
||
| 541 | */ |
||
| 542 | 9 | private function addBelongsToManyFiltersAndSorts( |
|
| 575 | |||
| 576 | /** |
||
| 577 | * @param string $fromAlias |
||
| 578 | * @param string $fromColumn |
||
| 579 | * @param string $targetTable |
||
| 580 | * @param string $targetColumn |
||
| 581 | * @param CompositeExpression|null $targetFilterLink |
||
| 582 | * @param iterable|null $targetFilterParams |
||
| 583 | * @param iterable|null $relationshipSorts |
||
| 584 | * |
||
| 585 | * @return string |
||
| 586 | */ |
||
| 587 | 25 | private function innerJoinOneTable( |
|
| 624 | |||
| 625 | /** @noinspection PhpTooManyParametersInspection |
||
| 626 | * @param string $fromAlias |
||
| 627 | * @param string $fromColumn |
||
| 628 | * @param string $intTable |
||
| 629 | * @param string $intToFromColumn |
||
| 630 | * @param string $intToTargetColumn |
||
| 631 | * @param string $targetTable |
||
| 632 | * @param string $targetColumn |
||
| 633 | * @param CompositeExpression|null $intFilterLink |
||
| 634 | * @param iterable|null $intFilterParams |
||
| 635 | * @param CompositeExpression|null $targetFilterLink |
||
| 636 | * @param iterable|null $targetFilterParams |
||
| 637 | * @param iterable|null $targetSortParams |
||
| 638 | * |
||
| 639 | * @return string |
||
| 640 | * |
||
| 641 | * @SuppressWarnings(PHPMD.ExcessiveParameterList) |
||
| 642 | */ |
||
| 643 | 9 | private function innerJoinTwoSequentialTables( |
|
| 679 | |||
| 680 | /** |
||
| 681 | * @param string $tableName |
||
| 682 | * |
||
| 683 | * @return string |
||
| 684 | */ |
||
| 685 | 52 | private function createAlias(string $tableName): string |
|
| 692 | |||
| 693 | /** |
||
| 694 | * @inheritdoc |
||
| 695 | */ |
||
| 696 | 52 | private function quoteTableName(string $tableName): string |
|
| 700 | |||
| 701 | /** |
||
| 702 | * @inheritdoc |
||
| 703 | */ |
||
| 704 | 7 | private function quoteColumnName(string $columnName): string |
|
| 708 | |||
| 709 | /** |
||
| 710 | * @inheritdoc |
||
| 711 | */ |
||
| 712 | 52 | private function buildColumnName(string $table, string $column): string |
|
| 716 | |||
| 717 | /** |
||
| 718 | * @param string $column |
||
| 719 | * |
||
| 720 | * @return string |
||
| 721 | */ |
||
| 722 | public function getQuotedMainTableColumn(string $column): string |
||
| 726 | |||
| 727 | /** |
||
| 728 | * @param string $column |
||
| 729 | * |
||
| 730 | * @return string |
||
| 731 | */ |
||
| 732 | 49 | public function getQuotedMainAliasColumn(string $column): string |
|
| 736 | |||
| 737 | /** |
||
| 738 | * @param CompositeExpression $filterLink |
||
| 739 | * @param string $fullColumnName |
||
| 740 | * @param iterable $operationsWithArgs |
||
| 741 | * |
||
| 742 | * @return void |
||
| 743 | * |
||
| 744 | * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
||
| 745 | */ |
||
| 746 | 50 | private function applyFilter( |
|
| 801 | |||
| 802 | /** |
||
| 803 | * @param iterable $arguments |
||
| 804 | * |
||
| 805 | * @return string |
||
| 806 | */ |
||
| 807 | 48 | private function createSingleNamedParameter(iterable $arguments): string |
|
| 818 | |||
| 819 | /** |
||
| 820 | * @param iterable $arguments |
||
| 821 | * |
||
| 822 | * @return string[] |
||
| 823 | */ |
||
| 824 | 5 | private function createNamedParameterArray(iterable $arguments): array |
|
| 834 | |||
| 835 | /** |
||
| 836 | * @return Closure |
||
| 837 | */ |
||
| 838 | 49 | private function getColumnToDatabaseMapper(): Closure |
|
| 842 | |||
| 843 | /** |
||
| 844 | * @param mixed $value |
||
| 845 | * |
||
| 846 | * @return int |
||
| 847 | * |
||
| 848 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
| 849 | */ |
||
| 850 | 49 | private function getPdoType($value): int |
|
| 868 | } |
||
| 869 |
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.