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 | * Condition joining method. |
||
| 45 | */ |
||
| 46 | public const AND = 0; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Condition joining method. |
||
| 50 | */ |
||
| 51 | public const OR = self::AND + 1; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | private $modelClass; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var string |
||
| 60 | */ |
||
| 61 | private $mainTableName; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var string |
||
| 65 | */ |
||
| 66 | private $mainAlias; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var Closure |
||
| 70 | */ |
||
| 71 | private $columnMapper; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var ModelSchemaInfoInterface |
||
| 75 | */ |
||
| 76 | private $modelSchemas; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var int |
||
| 80 | */ |
||
| 81 | private $aliasIdCounter = 0; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var array |
||
| 85 | */ |
||
| 86 | private $knownAliases = []; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var Type|null |
||
| 90 | */ |
||
| 91 | private $dateTimeType; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @param Connection $connection |
||
| 95 | * @param string $modelClass |
||
| 96 | * @param ModelSchemaInfoInterface $modelSchemas |
||
| 97 | * |
||
| 98 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 99 | */ |
||
| 100 | 66 | public function __construct(Connection $connection, string $modelClass, ModelSchemaInfoInterface $modelSchemas) |
|
| 114 | |||
| 115 | /** |
||
| 116 | * @return string |
||
| 117 | */ |
||
| 118 | 66 | public function getModelClass(): string |
|
| 122 | |||
| 123 | /** |
||
| 124 | * @param string|null $tableAlias |
||
| 125 | * @param string|null $modelClass |
||
| 126 | * |
||
| 127 | * @return array |
||
| 128 | */ |
||
| 129 | public function getModelColumns(string $tableAlias = null, string $modelClass = null): array |
||
| 150 | |||
| 151 | /** |
||
| 152 | 57 | * Select all fields associated with model. |
|
| 153 | * |
||
| 154 | 57 | * @param iterable|null $columns |
|
| 155 | * |
||
| 156 | * @return self |
||
| 157 | * |
||
| 158 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
| 159 | */ |
||
| 160 | 14 | public function selectModelColumns(iterable $columns = null): self |
|
| 175 | |||
| 176 | 66 | /** |
|
| 177 | * @return self |
||
| 178 | 66 | */ |
|
| 179 | public function distinct(): self |
||
| 187 | 57 | ||
| 188 | 57 | /** |
|
| 189 | * @param Closure $columnMapper |
||
| 190 | * |
||
| 191 | 57 | * @return self |
|
| 192 | */ |
||
| 193 | public function setColumnToDatabaseMapper(Closure $columnMapper): self |
||
| 199 | |||
| 200 | /** |
||
| 201 | * @return self |
||
| 202 | */ |
||
| 203 | 5 | public function fromModelTable(): self |
|
| 212 | |||
| 213 | 5 | /** |
|
| 214 | * @param iterable $attributes |
||
| 215 | * |
||
| 216 | * @return self |
||
| 217 | * |
||
| 218 | * @throws DBALException |
||
| 219 | * |
||
| 220 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 221 | */ |
||
| 222 | public function createModel(iterable $attributes): self |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @param iterable $attributes |
||
| 237 | * |
||
| 238 | * @return self |
||
| 239 | * |
||
| 240 | * @throws DBALException |
||
| 241 | * |
||
| 242 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 243 | */ |
||
| 244 | public function updateModels(iterable $attributes): self |
||
| 254 | 11 | ||
| 255 | 11 | /** |
|
| 256 | 11 | * @param string $modelClass |
|
| 257 | 11 | * @param iterable $attributes |
|
| 258 | * |
||
| 259 | 11 | * @return iterable |
|
|
|
|||
| 260 | * |
||
| 261 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 262 | * |
||
| 263 | * @throws DBALException |
||
| 264 | */ |
||
| 265 | public function bindAttributes(string $modelClass, iterable $attributes): iterable |
||
| 281 | |||
| 282 | /** |
||
| 283 | * @return self |
||
| 284 | */ |
||
| 285 | public function deleteModels(): self |
||
| 291 | 5 | ||
| 292 | 5 | /** |
|
| 293 | * @param string $relationshipName |
||
| 294 | * @param string $identity |
||
| 295 | 5 | * @param string $secondaryIdBindName |
|
| 296 | * |
||
| 297 | * @return self |
||
| 298 | */ |
||
| 299 | public function prepareCreateInToManyRelationship( |
||
| 316 | 1 | ||
| 317 | 1 | /** |
|
| 318 | * @param string $relationshipName |
||
| 319 | * @param string $identity |
||
| 320 | 1 | * @param iterable $secondaryIds |
|
| 321 | * |
||
| 322 | 1 | * @return ModelQueryBuilder |
|
| 323 | 1 | * |
|
| 324 | * @throws DBALException |
||
| 325 | 1 | */ |
|
| 326 | public function prepareDeleteInToManyRelationship( |
||
| 348 | |||
| 349 | 2 | /** |
|
| 350 | * @param string $relationshipName |
||
| 351 | 2 | * @param string $identity |
|
| 352 | * |
||
| 353 | * @return self |
||
| 354 | * |
||
| 355 | * @throws DBALException |
||
| 356 | */ |
||
| 357 | public function clearToManyRelationship(string $relationshipName, string $identity): self |
||
| 372 | |||
| 373 | /** |
||
| 374 | * @param iterable $filters |
||
| 375 | * |
||
| 376 | * @return self |
||
| 377 | 1 | * |
|
| 378 | * @throws DBALException |
||
| 379 | 1 | */ |
|
| 380 | 1 | public function addFiltersWithAndToTable(iterable $filters): self |
|
| 388 | |||
| 389 | /** |
||
| 390 | * @param iterable $filters |
||
| 391 | * |
||
| 392 | * @return self |
||
| 393 | 39 | * |
|
| 394 | * @throws DBALException |
||
| 395 | 39 | */ |
|
| 396 | 39 | public function addFiltersWithOrToTable(iterable $filters): self |
|
| 404 | |||
| 405 | /** |
||
| 406 | * @param iterable $filters |
||
| 407 | * |
||
| 408 | * @return self |
||
| 409 | 2 | * |
|
| 410 | * @throws DBALException |
||
| 411 | 2 | */ |
|
| 412 | 2 | public function addFiltersWithAndToAlias(iterable $filters): self |
|
| 420 | |||
| 421 | /** |
||
| 422 | * @param iterable $filters |
||
| 423 | * |
||
| 424 | * @return self |
||
| 425 | * |
||
| 426 | * @throws DBALException |
||
| 427 | */ |
||
| 428 | public function addFiltersWithOrToAlias(iterable $filters): self |
||
| 436 | |||
| 437 | /** |
||
| 438 | * @param string $relationshipName |
||
| 439 | * @param iterable|null $relationshipFilters |
||
| 440 | 29 | * @param iterable|null $relationshipSorts |
|
| 441 | * @param int $joinIndividuals |
||
| 442 | 29 | * @param int $joinRelationship |
|
| 443 | 29 | * |
|
| 444 | 29 | * @return self |
|
| 445 | * |
||
| 446 | * @throws DBALException |
||
| 447 | 29 | * |
|
| 448 | 29 | * @SuppressWarnings(PHPMD.ElseExpression) |
|
| 449 | * @SuppressWarnings(PHPMD.NPathComplexity) |
||
| 450 | 29 | * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
|
| 451 | */ |
||
| 452 | 29 | public function addRelationshipFiltersAndSorts( |
|
| 514 | 1 | ||
| 515 | /** |
||
| 516 | * @param iterable $sortParameters |
||
| 517 | * |
||
| 518 | * @return self |
||
| 519 | */ |
||
| 520 | public function addSorts(iterable $sortParameters): self |
||
| 524 | 58 | ||
| 525 | /** |
||
| 526 | * @param string $column |
||
| 527 | * |
||
| 528 | * @return string |
||
| 529 | */ |
||
| 530 | public function getQuotedMainTableColumn(string $column): string |
||
| 534 | 21 | ||
| 535 | /** |
||
| 536 | 21 | * @param string $column |
|
| 537 | * |
||
| 538 | 4 | * @return string |
|
| 539 | 4 | */ |
|
| 540 | 4 | public function getQuotedMainAliasColumn(string $column): string |
|
| 544 | |||
| 545 | 4 | /** |
|
| 546 | * @param string $name |
||
| 547 | 18 | * |
|
| 548 | * @return string Table alias. |
||
| 549 | 13 | */ |
|
| 550 | 13 | public function createRelationshipAlias(string $name): string |
|
| 599 | |||
| 600 | 46 | /** |
|
| 601 | * @return string |
||
| 602 | 46 | */ |
|
| 603 | 46 | public function getAlias(): string |
|
| 607 | 46 | ||
| 608 | 46 | /** |
|
| 609 | 46 | * @param CompositeExpression $expression |
|
| 610 | 46 | * @param string $tableOrAlias |
|
| 611 | 46 | * @param iterable $filters |
|
| 612 | 46 | * |
|
| 613 | * @return self |
||
| 614 | 46 | * |
|
| 615 | * @throws DBALException |
||
| 616 | * @throws InvalidArgumentException |
||
| 617 | */ |
||
| 618 | 45 | public function applyFilters(CompositeExpression $expression, string $tableOrAlias, iterable $filters): self |
|
| 638 | |||
| 639 | /** |
||
| 640 | * @param string $tableOrAlias |
||
| 641 | * @param iterable $sorts |
||
| 642 | * |
||
| 643 | * @return self |
||
| 644 | 64 | */ |
|
| 645 | public function applySorts(string $tableOrAlias, iterable $sorts): self |
||
| 655 | |||
| 656 | 58 | /** |
|
| 657 | * @param string $table |
||
| 658 | 58 | * @param string $column |
|
| 659 | * |
||
| 660 | 58 | * @return string |
|
| 661 | */ |
||
| 662 | public function buildColumnName(string $table, string $column): string |
||
| 666 | |||
| 667 | /** |
||
| 668 | * @param $value |
||
| 669 | * |
||
| 670 | 18 | * @return string |
|
| 671 | * |
||
| 672 | 18 | * @throws DBALException |
|
| 673 | */ |
||
| 674 | 18 | public function createSingleValueNamedParameter($value): string |
|
| 680 | |||
| 681 | /** |
||
| 682 | * @param iterable $values |
||
| 683 | * |
||
| 684 | * @return array |
||
| 685 | * |
||
| 686 | 66 | * @throws DBALException |
|
| 687 | */ |
||
| 688 | 66 | public function createArrayValuesNamedParameter(iterable $values): array |
|
| 698 | |||
| 699 | /** |
||
| 700 | * @param string $tableName |
||
| 701 | * |
||
| 702 | 21 | * @return string |
|
| 703 | */ |
||
| 704 | public function createAlias(string $tableName): string |
||
| 711 | |||
| 712 | 21 | /** |
|
| 713 | 21 | * @param string $fromAlias |
|
| 714 | 21 | * @param string $fromColumn |
|
| 715 | 21 | * @param string $targetTable |
|
| 716 | 21 | * @param string $targetColumn |
|
| 717 | * |
||
| 718 | * @return string |
||
| 719 | 21 | */ |
|
| 720 | public function innerJoinOneTable( |
||
| 739 | |||
| 740 | /** |
||
| 741 | * @param string $fromAlias |
||
| 742 | 10 | * @param string $fromColumn |
|
| 743 | 10 | * @param string $intTable |
|
| 744 | * @param string $intToFromColumn |
||
| 745 | 10 | * @param string $intToTargetColumn |
|
| 746 | * @param string $targetTable |
||
| 747 | * @param string $targetColumn |
||
| 748 | * |
||
| 749 | * @return string |
||
| 750 | */ |
||
| 751 | public function innerJoinTwoSequentialTables( |
||
| 765 | |||
| 766 | /** |
||
| 767 | * @param string $name |
||
| 768 | 66 | * |
|
| 769 | * @return Type |
||
| 770 | 66 | * |
|
| 771 | * @throws DBALException |
||
| 772 | * |
||
| 773 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 774 | */ |
||
| 775 | protected function getDbalType(string $name): Type |
||
| 782 | |||
| 783 | /** |
||
| 784 | * @return string |
||
| 785 | */ |
||
| 786 | 66 | private function getTableName(): string |
|
| 790 | |||
| 791 | /** |
||
| 792 | * @return ModelSchemaInfoInterface |
||
| 793 | */ |
||
| 794 | private function getModelSchemas(): ModelSchemaInfoInterface |
||
| 798 | 66 | ||
| 799 | /** |
||
| 800 | * @param string $tableName |
||
| 801 | * |
||
| 802 | * @return string |
||
| 803 | */ |
||
| 804 | private function quoteTableName(string $tableName): string |
||
| 808 | |||
| 809 | /** |
||
| 810 | * @param string $columnName |
||
| 811 | * |
||
| 812 | * @return string |
||
| 813 | */ |
||
| 814 | 59 | private function quoteColumnName(string $columnName): string |
|
| 818 | 48 | ||
| 819 | 47 | /** |
|
| 820 | 47 | * @param string $fullColumnName |
|
| 821 | 30 | * @param int $operation |
|
| 822 | 1 | * @param iterable $arguments |
|
| 823 | 1 | * |
|
| 824 | 1 | * @return string |
|
| 825 | 30 | * |
|
| 826 | 6 | * @throws DBALException |
|
| 827 | 6 | * @throws InvalidArgumentException |
|
| 828 | 6 | * |
|
| 829 | 30 | * @SuppressWarnings(PHPMD.StaticAccess) |
|
| 830 | 7 | * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
|
| 831 | 7 | */ |
|
| 832 | 7 | private function createFilterExpression(string $fullColumnName, int $operation, iterable $arguments): string |
|
| 887 | |||
| 888 | /** |
||
| 889 | * @param iterable $arguments |
||
| 890 | 57 | * |
|
| 891 | * @return mixed |
||
| 892 | 57 | * |
|
| 893 | * @throws InvalidArgumentException |
||
| 894 | */ |
||
| 895 | private function firstValue(iterable $arguments) |
||
| 904 | 58 | ||
| 905 | /** |
||
| 906 | * @return Closure |
||
| 907 | */ |
||
| 908 | private function getColumnToDatabaseMapper(): Closure |
||
| 912 | |||
| 913 | /** |
||
| 914 | 1 | * @param mixed $value |
|
| 915 | * |
||
| 916 | 1 | * @return mixed |
|
| 917 | 1 | * |
|
| 918 | 1 | * @throws DBALException |
|
| 919 | */ |
||
| 920 | private function getPdoValue($value) |
||
| 924 | |||
| 925 | /** |
||
| 926 | * @param DateTimeInterface $dateTime |
||
| 927 | * |
||
| 928 | * @return string |
||
| 929 | 58 | * |
|
| 930 | * @throws DBALException |
||
| 931 | 58 | */ |
|
| 932 | 46 | private function convertDataTimeToDatabaseFormat(DateTimeInterface $dateTime): string |
|
| 939 | 25 | ||
| 940 | /** |
||
| 941 | 25 | * @param mixed $value |
|
| 942 | * |
||
| 943 | 25 | * @return int |
|
| 944 | 25 | * |
|
| 945 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
| 946 | */ |
||
| 947 | 58 | private function getPdoType($value): int |
|
| 967 | |||
| 968 | /** |
||
| 969 | * @return Type |
||
| 970 | * |
||
| 971 | * @throws DBALException |
||
| 972 | * |
||
| 973 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 974 | */ |
||
| 975 | private function getDateTimeType(): Type |
||
| 983 | } |
||
| 984 |
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.