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 Type|null |
||
| 80 | */ |
||
| 81 | private $dateTimeType; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @param Connection $connection |
||
| 85 | * @param string $modelClass |
||
| 86 | * @param ModelSchemeInfoInterface $modelSchemes |
||
| 87 | * |
||
| 88 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 89 | */ |
||
| 90 | 59 | public function __construct(Connection $connection, string $modelClass, ModelSchemeInfoInterface $modelSchemes) |
|
| 104 | |||
| 105 | /** |
||
| 106 | * @return string |
||
| 107 | */ |
||
| 108 | 59 | 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 | 53 | public function selectModelColumns(iterable $columns = null): self |
|
| 135 | |||
| 136 | /** |
||
| 137 | * @return self |
||
| 138 | */ |
||
| 139 | 15 | public function distinct(): self |
|
| 147 | |||
| 148 | /** |
||
| 149 | * @param Closure $columnMapper |
||
| 150 | * |
||
| 151 | * @return self |
||
| 152 | */ |
||
| 153 | 59 | public function setColumnToDatabaseMapper(Closure $columnMapper): self |
|
| 159 | |||
| 160 | /** |
||
| 161 | * @return self |
||
| 162 | */ |
||
| 163 | 54 | public function fromModelTable(): self |
|
| 172 | |||
| 173 | /** |
||
| 174 | * @param iterable $attributes |
||
| 175 | * |
||
| 176 | * @return self |
||
| 177 | * |
||
| 178 | * @throws DBALException |
||
| 179 | * |
||
| 180 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 181 | */ |
||
| 182 | 4 | public function createModel(iterable $attributes): self |
|
| 194 | |||
| 195 | /** |
||
| 196 | * @param iterable $attributes |
||
| 197 | * |
||
| 198 | * @return self |
||
| 199 | * |
||
| 200 | * @throws DBALException |
||
| 201 | * |
||
| 202 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 203 | */ |
||
| 204 | 5 | public function updateModels(iterable $attributes): self |
|
| 214 | |||
| 215 | /** |
||
| 216 | * @param string $modelClass |
||
| 217 | * @param iterable $attributes |
||
| 218 | * |
||
| 219 | * @return iterable |
||
|
|
|||
| 220 | * |
||
| 221 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 222 | * |
||
| 223 | * @throws DBALException |
||
| 224 | */ |
||
| 225 | 9 | public function bindAttributes(string $modelClass, iterable $attributes): iterable |
|
| 241 | |||
| 242 | /** |
||
| 243 | * @return self |
||
| 244 | */ |
||
| 245 | 5 | public function deleteModels(): self |
|
| 251 | |||
| 252 | /** |
||
| 253 | * @param string $relationshipName |
||
| 254 | * @param string $identity |
||
| 255 | * @param string $secondaryIdBindName |
||
| 256 | * |
||
| 257 | * @return self |
||
| 258 | */ |
||
| 259 | 4 | public function prepareCreateInToManyRelationship( |
|
| 276 | |||
| 277 | /** |
||
| 278 | * @param string $relationshipName |
||
| 279 | * @param string $identity |
||
| 280 | * |
||
| 281 | * @return self |
||
| 282 | * |
||
| 283 | * @throws DBALException |
||
| 284 | */ |
||
| 285 | 2 | public function clearToManyRelationship(string $relationshipName, string $identity): self |
|
| 300 | |||
| 301 | /** |
||
| 302 | * @param iterable $filters |
||
| 303 | * |
||
| 304 | * @return self |
||
| 305 | * |
||
| 306 | * @throws DBALException |
||
| 307 | */ |
||
| 308 | 9 | public function addFiltersWithAndToTable(iterable $filters): self |
|
| 316 | |||
| 317 | /** |
||
| 318 | * @param iterable $filters |
||
| 319 | * |
||
| 320 | * @return self |
||
| 321 | * |
||
| 322 | * @throws DBALException |
||
| 323 | */ |
||
| 324 | 1 | public function addFiltersWithOrToTable(iterable $filters): self |
|
| 332 | |||
| 333 | /** |
||
| 334 | * @param iterable $filters |
||
| 335 | * |
||
| 336 | * @return self |
||
| 337 | * |
||
| 338 | * @throws DBALException |
||
| 339 | */ |
||
| 340 | 38 | public function addFiltersWithAndToAlias(iterable $filters): self |
|
| 348 | |||
| 349 | /** |
||
| 350 | * @param iterable $filters |
||
| 351 | * |
||
| 352 | * @return self |
||
| 353 | * |
||
| 354 | * @throws DBALException |
||
| 355 | */ |
||
| 356 | 2 | public function addFiltersWithOrToAlias(iterable $filters): self |
|
| 364 | |||
| 365 | /** |
||
| 366 | * @param string $relationshipName |
||
| 367 | * @param iterable|null $relationshipFilters |
||
| 368 | * @param iterable|null $relationshipSorts |
||
| 369 | * |
||
| 370 | * @return self |
||
| 371 | * |
||
| 372 | * @throws DBALException |
||
| 373 | */ |
||
| 374 | 27 | public function addRelationshipFiltersAndSortsWithAnd( |
|
| 391 | |||
| 392 | /** |
||
| 393 | * @param string $relationshipName |
||
| 394 | * @param iterable $relationshipFilters |
||
| 395 | * @param iterable|null $relationshipSorts |
||
| 396 | * |
||
| 397 | * @return self |
||
| 398 | * |
||
| 399 | * @throws DBALException |
||
| 400 | */ |
||
| 401 | 2 | public function addRelationshipFiltersAndSortsWithOr( |
|
| 418 | |||
| 419 | /** |
||
| 420 | * @param iterable $sortParameters |
||
| 421 | * |
||
| 422 | * @return self |
||
| 423 | */ |
||
| 424 | 8 | public function addSorts(iterable $sortParameters): self |
|
| 428 | |||
| 429 | /** |
||
| 430 | * @param string $column |
||
| 431 | * |
||
| 432 | * @return string |
||
| 433 | */ |
||
| 434 | 1 | public function getQuotedMainTableColumn(string $column): string |
|
| 438 | |||
| 439 | /** |
||
| 440 | * @param string $column |
||
| 441 | * |
||
| 442 | * @return string |
||
| 443 | */ |
||
| 444 | 54 | public function getQuotedMainAliasColumn(string $column): string |
|
| 448 | |||
| 449 | /** |
||
| 450 | * @param string $name |
||
| 451 | * |
||
| 452 | * @return string Table alias. |
||
| 453 | */ |
||
| 454 | 29 | public function createRelationshipAlias(string $name): string |
|
| 503 | |||
| 504 | /** |
||
| 505 | * @return string |
||
| 506 | */ |
||
| 507 | 55 | public function getAlias(): string |
|
| 511 | |||
| 512 | /** |
||
| 513 | * @param CompositeExpression $expression |
||
| 514 | * @param string $tableOrAlias |
||
| 515 | * @param iterable $filters |
||
| 516 | * |
||
| 517 | * @return self |
||
| 518 | * |
||
| 519 | * @throws DBALException |
||
| 520 | * @throws InvalidArgumentException |
||
| 521 | */ |
||
| 522 | 56 | public function applyFilters(CompositeExpression $expression, string $tableOrAlias, iterable $filters): self |
|
| 533 | |||
| 534 | /** |
||
| 535 | * @param string $tableOrAlias |
||
| 536 | * @param iterable $sorts |
||
| 537 | * |
||
| 538 | * @return self |
||
| 539 | */ |
||
| 540 | 27 | public function applySorts(string $tableOrAlias, iterable $sorts): self |
|
| 550 | |||
| 551 | /** |
||
| 552 | * @param string $table |
||
| 553 | * @param string $column |
||
| 554 | * |
||
| 555 | * @return string |
||
| 556 | */ |
||
| 557 | 59 | public function buildColumnName(string $table, string $column): string |
|
| 561 | |||
| 562 | /** |
||
| 563 | * @param $value |
||
| 564 | * |
||
| 565 | * @return string |
||
| 566 | * |
||
| 567 | * @throws DBALException |
||
| 568 | */ |
||
| 569 | 54 | public function createSingleValueNamedParameter($value): string |
|
| 575 | |||
| 576 | /** |
||
| 577 | * @param iterable $values |
||
| 578 | * |
||
| 579 | * @return array |
||
| 580 | * |
||
| 581 | * @throws DBALException |
||
| 582 | */ |
||
| 583 | 14 | public function createArrayValuesNamedParameter(iterable $values): array |
|
| 593 | |||
| 594 | /** |
||
| 595 | * @param string $name |
||
| 596 | * |
||
| 597 | * @return Type |
||
| 598 | * |
||
| 599 | * @throws DBALException |
||
| 600 | */ |
||
| 601 | 9 | protected function getDbalType(string $name): Type |
|
| 608 | |||
| 609 | /** |
||
| 610 | * @return string |
||
| 611 | */ |
||
| 612 | 59 | private function getTableName(): string |
|
| 616 | |||
| 617 | /** |
||
| 618 | * @return ModelSchemeInfoInterface |
||
| 619 | */ |
||
| 620 | 59 | private function getModelSchemes(): ModelSchemeInfoInterface |
|
| 624 | |||
| 625 | /** |
||
| 626 | * @param string $fromAlias |
||
| 627 | * @param string $fromColumn |
||
| 628 | * @param string $targetTable |
||
| 629 | * @param string $targetColumn |
||
| 630 | * |
||
| 631 | * @return string |
||
| 632 | */ |
||
| 633 | 29 | private function innerJoinOneTable( |
|
| 652 | |||
| 653 | /** |
||
| 654 | * @param string $fromAlias |
||
| 655 | * @param string $fromColumn |
||
| 656 | * @param string $intTable |
||
| 657 | * @param string $intToFromColumn |
||
| 658 | * @param string $intToTargetColumn |
||
| 659 | * @param string $targetTable |
||
| 660 | * @param string $targetColumn |
||
| 661 | * |
||
| 662 | * @return string |
||
| 663 | */ |
||
| 664 | 9 | private function innerJoinTwoSequentialTables( |
|
| 678 | |||
| 679 | /** |
||
| 680 | * @param string $tableName |
||
| 681 | * |
||
| 682 | * @return string |
||
| 683 | */ |
||
| 684 | 59 | private function createAlias(string $tableName): string |
|
| 691 | |||
| 692 | /** |
||
| 693 | * @param string $tableName |
||
| 694 | * |
||
| 695 | * @return string |
||
| 696 | */ |
||
| 697 | 58 | private function quoteTableName(string $tableName): string |
|
| 701 | |||
| 702 | /** |
||
| 703 | * @param string $columnName |
||
| 704 | * |
||
| 705 | * @return string |
||
| 706 | */ |
||
| 707 | 9 | private function quoteColumnName(string $columnName): string |
|
| 711 | |||
| 712 | /** |
||
| 713 | * @param string $fullColumnName |
||
| 714 | * @param int $operation |
||
| 715 | * @param iterable $arguments |
||
| 716 | * |
||
| 717 | * @return string |
||
| 718 | * |
||
| 719 | * @throws DBALException |
||
| 720 | * @throws InvalidArgumentException |
||
| 721 | */ |
||
| 722 | 55 | private function createFilterExpression(string $fullColumnName, int $operation, iterable $arguments): string |
|
| 777 | |||
| 778 | /** |
||
| 779 | * @param iterable $arguments |
||
| 780 | * |
||
| 781 | * @return mixed |
||
| 782 | * |
||
| 783 | * @throws InvalidArgumentException |
||
| 784 | */ |
||
| 785 | 52 | private function firstValue(iterable $arguments) |
|
| 794 | |||
| 795 | /** |
||
| 796 | * @return Closure |
||
| 797 | */ |
||
| 798 | 53 | private function getColumnToDatabaseMapper(): Closure |
|
| 802 | |||
| 803 | /** |
||
| 804 | * @param mixed $value |
||
| 805 | * |
||
| 806 | * @return mixed |
||
| 807 | * |
||
| 808 | * @throws DBALException |
||
| 809 | */ |
||
| 810 | 54 | private function getPdoValue($value) |
|
| 814 | |||
| 815 | /** |
||
| 816 | * @param DateTimeInterface $dateTime |
||
| 817 | * |
||
| 818 | * @return string |
||
| 819 | * |
||
| 820 | * @throws DBALException |
||
| 821 | */ |
||
| 822 | 1 | private function convertDataTimeToDatabaseFormat(DateTimeInterface $dateTime): string |
|
| 829 | |||
| 830 | /** |
||
| 831 | * @param mixed $value |
||
| 832 | * |
||
| 833 | * @return int |
||
| 834 | * |
||
| 835 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
| 836 | */ |
||
| 837 | 54 | private function getPdoType($value): int |
|
| 857 | |||
| 858 | /** |
||
| 859 | * @return Type |
||
| 860 | * |
||
| 861 | * @throws DBALException |
||
| 862 | * |
||
| 863 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 864 | */ |
||
| 865 | 1 | private function getDateTimeType(): Type |
|
| 873 | } |
||
| 874 |
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.