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 ModelSchemeInfoInterface |
||
| 65 | */ |
||
| 66 | private $modelSchemes; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var int |
||
| 70 | */ |
||
| 71 | private $aliasIdCounter = 0; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var array |
||
| 75 | */ |
||
| 76 | private $knownAliases = []; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var null|Closure |
||
| 80 | */ |
||
| 81 | private $dtToDbConverter = null; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @param Connection $connection |
||
| 85 | * @param string $modelClass |
||
| 86 | * @param ModelSchemeInfoInterface $modelSchemes |
||
| 87 | * |
||
| 88 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 89 | */ |
||
| 90 | 58 | public function __construct(Connection $connection, string $modelClass, ModelSchemeInfoInterface $modelSchemes) |
|
| 104 | |||
| 105 | /** |
||
| 106 | * @return string |
||
| 107 | */ |
||
| 108 | 58 | 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 | 52 | public function selectModelColumns(iterable $columns = null): self |
|
| 135 | |||
| 136 | /** |
||
| 137 | * @param Closure $columnMapper |
||
| 138 | * |
||
| 139 | * @return self |
||
| 140 | */ |
||
| 141 | 58 | public function setColumnToDatabaseMapper(Closure $columnMapper): self |
|
| 147 | |||
| 148 | /** |
||
| 149 | * @return self |
||
| 150 | */ |
||
| 151 | 53 | public function fromModelTable(): self |
|
| 160 | |||
| 161 | /** |
||
| 162 | * @param iterable $attributes |
||
| 163 | * |
||
| 164 | * @return self |
||
| 165 | * |
||
| 166 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 167 | */ |
||
| 168 | 4 | public function createModel(iterable $attributes): self |
|
| 180 | |||
| 181 | /** |
||
| 182 | * @param iterable $attributes |
||
| 183 | * |
||
| 184 | * @return self |
||
| 185 | * |
||
| 186 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 187 | */ |
||
| 188 | 5 | public function updateModels(iterable $attributes): self |
|
| 198 | |||
| 199 | /** |
||
| 200 | * @param string $modelClass |
||
| 201 | * @param iterable $attributes |
||
| 202 | * |
||
| 203 | * @return iterable |
||
|
|
|||
| 204 | * |
||
| 205 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 206 | */ |
||
| 207 | 9 | public function bindAttributes(string $modelClass, iterable $attributes): iterable |
|
| 224 | |||
| 225 | /** |
||
| 226 | * @return self |
||
| 227 | */ |
||
| 228 | 5 | public function deleteModels(): self |
|
| 234 | |||
| 235 | /** |
||
| 236 | * @inheritdoc |
||
| 237 | */ |
||
| 238 | 4 | public function prepareCreateInToManyRelationship( |
|
| 255 | |||
| 256 | /** |
||
| 257 | * @inheritdoc |
||
| 258 | */ |
||
| 259 | 2 | public function clearToManyRelationship(string $relationshipName, string $identity): self |
|
| 271 | |||
| 272 | /** |
||
| 273 | * @param iterable $filters |
||
| 274 | * |
||
| 275 | * @return self |
||
| 276 | */ |
||
| 277 | 9 | public function addFiltersWithAndToTable(iterable $filters): self |
|
| 281 | |||
| 282 | /** |
||
| 283 | * @param iterable $filters |
||
| 284 | * |
||
| 285 | * @return self |
||
| 286 | */ |
||
| 287 | 1 | public function addFiltersWithOrToTable(iterable $filters): self |
|
| 291 | |||
| 292 | /** |
||
| 293 | * @param iterable $filters |
||
| 294 | * |
||
| 295 | * @return self |
||
| 296 | */ |
||
| 297 | 38 | public function addFiltersWithAndToAlias(iterable $filters): self |
|
| 301 | |||
| 302 | /** |
||
| 303 | * @param iterable $filters |
||
| 304 | * |
||
| 305 | * @return self |
||
| 306 | */ |
||
| 307 | 2 | public function addFiltersWithOrToAlias(iterable $filters): self |
|
| 311 | |||
| 312 | /** |
||
| 313 | * @param string $relationshipName |
||
| 314 | * @param iterable $relationshipFilters |
||
| 315 | * @param iterable|null $relationshipSorts |
||
| 316 | * |
||
| 317 | * @return self |
||
| 318 | */ |
||
| 319 | 26 | public function addRelationshipFiltersAndSortsWithAnd( |
|
| 333 | |||
| 334 | /** |
||
| 335 | * @return self |
||
| 336 | */ |
||
| 337 | 14 | public function distinct(): self |
|
| 345 | |||
| 346 | /** |
||
| 347 | * @param string $relationshipName |
||
| 348 | * @param iterable $relationshipFilters |
||
| 349 | * @param iterable|null $relationshipSorts |
||
| 350 | * |
||
| 351 | * @return self |
||
| 352 | */ |
||
| 353 | 2 | public function addRelationshipFiltersAndSortsWithOr( |
|
| 367 | |||
| 368 | /** |
||
| 369 | * @param iterable $sortParameters |
||
| 370 | * |
||
| 371 | * @return self |
||
| 372 | */ |
||
| 373 | 8 | public function addSorts(iterable $sortParameters): self |
|
| 384 | |||
| 385 | /** |
||
| 386 | * @param string $tableOrAlias |
||
| 387 | * @param CompositeExpression $filterLink |
||
| 388 | * @param iterable $filters |
||
| 389 | * |
||
| 390 | * @return self |
||
| 391 | */ |
||
| 392 | 43 | private function addFilters(string $tableOrAlias, CompositeExpression $filterLink, iterable $filters): self |
|
| 404 | |||
| 405 | /** |
||
| 406 | * @param string $relationshipName |
||
| 407 | * @param CompositeExpression $filterLink |
||
| 408 | * @param iterable $relationshipFilters |
||
| 409 | * @param iterable|null $relationshipSorts |
||
| 410 | * |
||
| 411 | * @return self |
||
| 412 | */ |
||
| 413 | 28 | private function addRelationshipFiltersAndSorts( |
|
| 453 | |||
| 454 | /** |
||
| 455 | * @return string |
||
| 456 | */ |
||
| 457 | 58 | private function getMainTableName(): string |
|
| 461 | |||
| 462 | /** |
||
| 463 | * @return ModelSchemeInfoInterface |
||
| 464 | */ |
||
| 465 | 58 | private function getModelSchemes(): ModelSchemeInfoInterface |
|
| 469 | |||
| 470 | /** |
||
| 471 | * @return string |
||
| 472 | */ |
||
| 473 | 54 | private function getMainAlias(): string |
|
| 477 | |||
| 478 | /** |
||
| 479 | * @param string $relationshipName |
||
| 480 | * @param CompositeExpression $filterLink |
||
| 481 | * @param iterable $relationshipFilters |
||
| 482 | * @param iterable|null $relationshipSorts |
||
| 483 | * |
||
| 484 | * @return self |
||
| 485 | */ |
||
| 486 | 17 | private function addBelongsToFiltersAndSorts( |
|
| 511 | |||
| 512 | /** |
||
| 513 | * @param string $relationshipName |
||
| 514 | * @param CompositeExpression $filterLink |
||
| 515 | * @param iterable $relationshipFilters |
||
| 516 | * @param iterable|null $relationshipSorts |
||
| 517 | * |
||
| 518 | * @return self |
||
| 519 | */ |
||
| 520 | 12 | private function addHasManyFiltersAndSorts( |
|
| 545 | |||
| 546 | /** |
||
| 547 | * @param string $relationshipName |
||
| 548 | * @param CompositeExpression $targetFilterLink |
||
| 549 | * @param iterable $relationshipFilters |
||
| 550 | * @param iterable|null $relationshipSorts |
||
| 551 | * |
||
| 552 | * @return self |
||
| 553 | */ |
||
| 554 | 9 | private function addBelongsToManyFiltersAndSorts( |
|
| 589 | |||
| 590 | /** |
||
| 591 | * @param string $fromAlias |
||
| 592 | * @param string $fromColumn |
||
| 593 | * @param string $targetTable |
||
| 594 | * @param string $targetColumn |
||
| 595 | * @param CompositeExpression|null $targetFilterLink |
||
| 596 | * @param iterable|null $targetFilterParams |
||
| 597 | * @param iterable|null $relationshipSorts |
||
| 598 | * |
||
| 599 | * @return string |
||
| 600 | */ |
||
| 601 | 28 | private function innerJoinOneTable( |
|
| 638 | |||
| 639 | /** @noinspection PhpTooManyParametersInspection |
||
| 640 | * @param string $fromAlias |
||
| 641 | * @param string $fromColumn |
||
| 642 | * @param string $intTable |
||
| 643 | * @param string $intToFromColumn |
||
| 644 | * @param string $intToTargetColumn |
||
| 645 | * @param string $targetTable |
||
| 646 | * @param string $targetColumn |
||
| 647 | * @param CompositeExpression|null $intFilterLink |
||
| 648 | * @param iterable|null $intFilterParams |
||
| 649 | * @param CompositeExpression|null $targetFilterLink |
||
| 650 | * @param iterable|null $targetFilterParams |
||
| 651 | * @param iterable|null $targetSortParams |
||
| 652 | * |
||
| 653 | * @return string |
||
| 654 | * |
||
| 655 | * @SuppressWarnings(PHPMD.ExcessiveParameterList) |
||
| 656 | */ |
||
| 657 | 9 | private function innerJoinTwoSequentialTables( |
|
| 693 | |||
| 694 | /** |
||
| 695 | * @param string $tableName |
||
| 696 | * |
||
| 697 | * @return string |
||
| 698 | */ |
||
| 699 | 58 | private function createAlias(string $tableName): string |
|
| 706 | |||
| 707 | /** |
||
| 708 | * @inheritdoc |
||
| 709 | */ |
||
| 710 | 57 | private function quoteTableName(string $tableName): string |
|
| 714 | |||
| 715 | /** |
||
| 716 | * @inheritdoc |
||
| 717 | */ |
||
| 718 | 9 | private function quoteColumnName(string $columnName): string |
|
| 722 | |||
| 723 | /** |
||
| 724 | * @inheritdoc |
||
| 725 | */ |
||
| 726 | 58 | private function buildColumnName(string $table, string $column): string |
|
| 730 | |||
| 731 | /** |
||
| 732 | * @param string $column |
||
| 733 | * |
||
| 734 | * @return string |
||
| 735 | */ |
||
| 736 | 1 | public function getQuotedMainTableColumn(string $column): string |
|
| 740 | |||
| 741 | /** |
||
| 742 | * @param string $column |
||
| 743 | * |
||
| 744 | * @return string |
||
| 745 | */ |
||
| 746 | 53 | public function getQuotedMainAliasColumn(string $column): string |
|
| 750 | |||
| 751 | /** |
||
| 752 | * @param CompositeExpression $filterLink |
||
| 753 | * @param string $fullColumnName |
||
| 754 | * @param iterable $operationsWithArgs |
||
| 755 | * |
||
| 756 | * @return void |
||
| 757 | * |
||
| 758 | * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
||
| 759 | */ |
||
| 760 | 54 | private function applyFilter( |
|
| 815 | |||
| 816 | /** |
||
| 817 | * @param iterable $arguments |
||
| 818 | * |
||
| 819 | * @return string |
||
| 820 | */ |
||
| 821 | 52 | private function createSingleNamedParameter(iterable $arguments): string |
|
| 832 | |||
| 833 | /** |
||
| 834 | * @param iterable $arguments |
||
| 835 | * |
||
| 836 | * @return string[] |
||
| 837 | */ |
||
| 838 | 6 | private function createNamedParameterArray(iterable $arguments): array |
|
| 848 | |||
| 849 | /** |
||
| 850 | * @return Closure |
||
| 851 | */ |
||
| 852 | 52 | private function getColumnToDatabaseMapper(): Closure |
|
| 856 | |||
| 857 | /** |
||
| 858 | * @param mixed $value |
||
| 859 | * |
||
| 860 | * @return mixed |
||
| 861 | */ |
||
| 862 | 53 | private function getPdoValue($value) |
|
| 866 | |||
| 867 | /** |
||
| 868 | * @param DateTimeInterface $dateTime |
||
| 869 | * |
||
| 870 | * @return string |
||
| 871 | * |
||
| 872 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 873 | */ |
||
| 874 | 1 | private function convertDataTimeToDatabaseFormat(DateTimeInterface $dateTime): string |
|
| 886 | |||
| 887 | /** |
||
| 888 | * @param mixed $value |
||
| 889 | * |
||
| 890 | * @return int |
||
| 891 | * |
||
| 892 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
| 893 | */ |
||
| 894 | 53 | private function getPdoType($value): int |
|
| 914 | } |
||
| 915 |
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.