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; |
||
| 37 | class ModelQueryBuilder extends QueryBuilder |
||
| 38 | { |
||
| 39 | /** |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | private $modelClass; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | private $mainTableName; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var string |
||
| 51 | */ |
||
| 52 | private $mainAlias; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var ModelSchemeInfoInterface |
||
| 56 | */ |
||
| 57 | private $modelSchemes; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var int |
||
| 61 | */ |
||
| 62 | private $aliasIdCounter = 0; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var array |
||
| 66 | */ |
||
| 67 | private $knownAliases = []; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @param Connection $connection |
||
| 71 | * @param string $modelClass |
||
| 72 | * @param ModelSchemeInfoInterface $modelSchemes |
||
| 73 | */ |
||
| 74 | 48 | public function __construct(Connection $connection, string $modelClass, ModelSchemeInfoInterface $modelSchemes) |
|
| 86 | |||
| 87 | /** |
||
| 88 | * @return string |
||
| 89 | */ |
||
| 90 | 48 | public function getModelClass(): string |
|
| 94 | |||
| 95 | /** |
||
| 96 | * Select all fields associated with model. |
||
| 97 | * |
||
| 98 | * @param iterable|null $columns |
||
| 99 | * |
||
| 100 | * @return self |
||
| 101 | */ |
||
| 102 | 45 | public function selectModelColumns(iterable $columns = null): self |
|
| 116 | |||
| 117 | /** |
||
| 118 | * @return self |
||
| 119 | */ |
||
| 120 | 46 | public function fromModelTable(): self |
|
| 129 | |||
| 130 | /** |
||
| 131 | * @param iterable $attributes |
||
| 132 | * |
||
| 133 | * @return self |
||
| 134 | * |
||
| 135 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 136 | */ |
||
| 137 | 4 | public function createModel(iterable $attributes): self |
|
| 149 | |||
| 150 | /** |
||
| 151 | * @param iterable $attributes |
||
| 152 | * |
||
| 153 | * @return self |
||
| 154 | * |
||
| 155 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 156 | */ |
||
| 157 | 3 | public function updateModels(iterable $attributes): self |
|
| 167 | |||
| 168 | /** |
||
| 169 | * @param string $modelClass |
||
| 170 | * @param iterable $attributes |
||
| 171 | * |
||
| 172 | * @return iterable |
||
|
|
|||
| 173 | * |
||
| 174 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 175 | */ |
||
| 176 | 7 | public function bindAttributes(string $modelClass, iterable $attributes): iterable |
|
| 193 | |||
| 194 | /** |
||
| 195 | * @return self |
||
| 196 | */ |
||
| 197 | 5 | public function deleteModels(): self |
|
| 203 | |||
| 204 | /** |
||
| 205 | * @inheritdoc |
||
| 206 | */ |
||
| 207 | 4 | public function prepareCreateInToManyRelationship( |
|
| 224 | |||
| 225 | /** |
||
| 226 | * @inheritdoc |
||
| 227 | */ |
||
| 228 | 2 | public function clearToManyRelationship(string $relationshipName, string $identity): self |
|
| 240 | |||
| 241 | /** |
||
| 242 | * @param iterable $filters |
||
| 243 | * |
||
| 244 | * @return self |
||
| 245 | */ |
||
| 246 | 8 | public function addFiltersWithAndToTable(iterable $filters): self |
|
| 250 | |||
| 251 | /** |
||
| 252 | * @param iterable $filters |
||
| 253 | * |
||
| 254 | * @return self |
||
| 255 | */ |
||
| 256 | public function addFiltersWithOrToTable(iterable $filters): self |
||
| 260 | |||
| 261 | /** |
||
| 262 | * @param iterable $filters |
||
| 263 | * |
||
| 264 | * @return self |
||
| 265 | */ |
||
| 266 | 33 | public function addFiltersWithAndToAlias(iterable $filters): self |
|
| 270 | |||
| 271 | /** |
||
| 272 | * @param iterable $filters |
||
| 273 | * |
||
| 274 | * @return self |
||
| 275 | */ |
||
| 276 | 2 | public function addFiltersWithOrToAlias(iterable $filters): self |
|
| 280 | |||
| 281 | /** |
||
| 282 | * @param string $relationshipName |
||
| 283 | * @param iterable $relationshipFilters |
||
| 284 | * @param iterable|null $relationshipSorts |
||
| 285 | * |
||
| 286 | * @return self |
||
| 287 | */ |
||
| 288 | 23 | public function addRelationshipFiltersAndSortsWithAnd( |
|
| 302 | |||
| 303 | /** |
||
| 304 | * @return self |
||
| 305 | */ |
||
| 306 | 13 | public function distinct(): self |
|
| 314 | |||
| 315 | /** |
||
| 316 | * @param string $relationshipName |
||
| 317 | * @param iterable $relationshipFilters |
||
| 318 | * @param iterable|null $relationshipSorts |
||
| 319 | * |
||
| 320 | * @return self |
||
| 321 | */ |
||
| 322 | 1 | public function addRelationshipFiltersAndSortsWithOr( |
|
| 336 | |||
| 337 | /** |
||
| 338 | * @param iterable $sortParameters |
||
| 339 | * |
||
| 340 | * @return self |
||
| 341 | */ |
||
| 342 | 6 | public function addSorts(iterable $sortParameters): self |
|
| 353 | |||
| 354 | /** |
||
| 355 | * @param string $tableOrAlias |
||
| 356 | * @param CompositeExpression $filterLink |
||
| 357 | * @param iterable $filters |
||
| 358 | * |
||
| 359 | * @return self |
||
| 360 | */ |
||
| 361 | 36 | private function addFilters(string $tableOrAlias, CompositeExpression $filterLink, iterable $filters): self |
|
| 375 | |||
| 376 | /** |
||
| 377 | * @param string $relationshipName |
||
| 378 | * @param CompositeExpression $filterLink |
||
| 379 | * @param iterable $relationshipFilters |
||
| 380 | * @param iterable|null $relationshipSorts |
||
| 381 | * |
||
| 382 | * @return self |
||
| 383 | */ |
||
| 384 | 24 | private function addRelationshipFiltersAndSorts( |
|
| 424 | |||
| 425 | /** |
||
| 426 | * @return string |
||
| 427 | */ |
||
| 428 | 48 | private function getMainTableName(): string |
|
| 432 | |||
| 433 | /** |
||
| 434 | * @return ModelSchemeInfoInterface |
||
| 435 | */ |
||
| 436 | 48 | private function getModelSchemes(): ModelSchemeInfoInterface |
|
| 440 | |||
| 441 | /** |
||
| 442 | * @return string |
||
| 443 | */ |
||
| 444 | 46 | private function getMainAlias(): string |
|
| 448 | |||
| 449 | /** |
||
| 450 | * @param string $relationshipName |
||
| 451 | * @param CompositeExpression $filterLink |
||
| 452 | * @param iterable $relationshipFilters |
||
| 453 | * @param iterable|null $relationshipSorts |
||
| 454 | * |
||
| 455 | * @return self |
||
| 456 | */ |
||
| 457 | 13 | private function addBelongsToFiltersAndSorts( |
|
| 480 | |||
| 481 | /** |
||
| 482 | * @param string $relationshipName |
||
| 483 | * @param CompositeExpression $filterLink |
||
| 484 | * @param iterable $relationshipFilters |
||
| 485 | * @param iterable|null $relationshipSorts |
||
| 486 | * |
||
| 487 | * @return self |
||
| 488 | */ |
||
| 489 | 11 | private function addHasManyFiltersAndSorts( |
|
| 512 | |||
| 513 | /** |
||
| 514 | * @param string $relationshipName |
||
| 515 | * @param CompositeExpression $targetFilterLink |
||
| 516 | * @param iterable $relationshipFilters |
||
| 517 | * @param iterable|null $relationshipSorts |
||
| 518 | * |
||
| 519 | * @return self |
||
| 520 | */ |
||
| 521 | 8 | private function addBelongsToManyFiltersAndSorts( |
|
| 554 | |||
| 555 | /** |
||
| 556 | * @param string $fromAlias |
||
| 557 | * @param string $fromColumn |
||
| 558 | * @param string $targetTable |
||
| 559 | * @param string $targetColumn |
||
| 560 | * @param CompositeExpression|null $targetFilterLink |
||
| 561 | * @param iterable|null $targetFilterParams |
||
| 562 | * @param iterable|null $relationshipSorts |
||
| 563 | * |
||
| 564 | * @return string |
||
| 565 | */ |
||
| 566 | 24 | private function innerJoinOneTable( |
|
| 603 | |||
| 604 | /** @noinspection PhpTooManyParametersInspection |
||
| 605 | * @param string $fromAlias |
||
| 606 | * @param string $fromColumn |
||
| 607 | * @param string $intTable |
||
| 608 | * @param string $intToFromColumn |
||
| 609 | * @param string $intToTargetColumn |
||
| 610 | * @param string $targetTable |
||
| 611 | * @param string $targetColumn |
||
| 612 | * @param CompositeExpression|null $intFilterLink |
||
| 613 | * @param iterable|null $intFilterParams |
||
| 614 | * @param CompositeExpression|null $targetFilterLink |
||
| 615 | * @param iterable|null $targetFilterParams |
||
| 616 | * @param iterable|null $targetSortParams |
||
| 617 | * |
||
| 618 | * @return string |
||
| 619 | * |
||
| 620 | * @SuppressWarnings(PHPMD.ExcessiveParameterList) |
||
| 621 | */ |
||
| 622 | 8 | private function innerJoinTwoSequentialTables( |
|
| 658 | |||
| 659 | /** |
||
| 660 | * @param string $tableName |
||
| 661 | * |
||
| 662 | * @return string |
||
| 663 | */ |
||
| 664 | 48 | private function createAlias(string $tableName): string |
|
| 671 | |||
| 672 | /** |
||
| 673 | * @inheritdoc |
||
| 674 | */ |
||
| 675 | 48 | private function quoteTableName(string $tableName): string |
|
| 679 | |||
| 680 | /** |
||
| 681 | * @inheritdoc |
||
| 682 | */ |
||
| 683 | 7 | private function quoteColumnName(string $columnName): string |
|
| 687 | |||
| 688 | /** |
||
| 689 | * @inheritdoc |
||
| 690 | */ |
||
| 691 | 48 | private function buildColumnName(string $table, string $column): string |
|
| 695 | |||
| 696 | /** |
||
| 697 | * @param string $column |
||
| 698 | * |
||
| 699 | * @return string |
||
| 700 | */ |
||
| 701 | public function getQuotedMainTableColumn(string $column): string |
||
| 705 | |||
| 706 | /** |
||
| 707 | * @param string $column |
||
| 708 | * |
||
| 709 | * @return string |
||
| 710 | */ |
||
| 711 | 45 | public function getQuotedMainAliasColumn(string $column): string |
|
| 715 | |||
| 716 | /** |
||
| 717 | * @param CompositeExpression $filterLink |
||
| 718 | * @param string $fullColumnName |
||
| 719 | * @param iterable $operationsWithArgs |
||
| 720 | * |
||
| 721 | * @return void |
||
| 722 | * |
||
| 723 | * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
||
| 724 | */ |
||
| 725 | 46 | private function applyFilter( |
|
| 780 | |||
| 781 | /** |
||
| 782 | * @param iterable $arguments |
||
| 783 | * |
||
| 784 | * @return string |
||
| 785 | */ |
||
| 786 | 44 | private function createSingleNamedParameter(iterable $arguments): string |
|
| 797 | |||
| 798 | /** |
||
| 799 | * @param iterable $arguments |
||
| 800 | * |
||
| 801 | * @return string[] |
||
| 802 | */ |
||
| 803 | 5 | private function createNamedParameterArray(iterable $arguments): array |
|
| 813 | |||
| 814 | /** |
||
| 815 | * @param mixed $value |
||
| 816 | * |
||
| 817 | * @return int |
||
| 818 | * |
||
| 819 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
| 820 | */ |
||
| 821 | 45 | private function getPdoType($value): int |
|
| 839 | } |
||
| 840 |
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.