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 declare (strict_types = 1); |
||
| 51 | class ModelQueryBuilder extends QueryBuilder |
||
| 52 | { |
||
| 53 | /** |
||
| 54 | * Condition joining method. |
||
| 55 | */ |
||
| 56 | public const AND = 0; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Condition joining method. |
||
| 60 | */ |
||
| 61 | public const OR = self::AND + 1; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var string |
||
| 65 | */ |
||
| 66 | private $modelClass; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var string |
||
| 70 | */ |
||
| 71 | private $mainTableName; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var string |
||
| 75 | */ |
||
| 76 | private $mainAlias; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var Closure |
||
| 80 | */ |
||
| 81 | private $columnMapper; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var ModelSchemaInfoInterface |
||
| 85 | */ |
||
| 86 | private $modelSchemas; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var int |
||
| 90 | */ |
||
| 91 | private $aliasIdCounter = 0; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var array |
||
| 95 | */ |
||
| 96 | private $knownAliases = []; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @var Type|null |
||
| 100 | */ |
||
| 101 | private $dateTimeType; |
||
| 102 | 66 | ||
| 103 | /** |
||
| 104 | 66 | * @param Connection $connection |
|
| 105 | * @param string $modelClass |
||
| 106 | 66 | * @param ModelSchemaInfoInterface $modelSchemas |
|
| 107 | * |
||
| 108 | 66 | * @SuppressWarnings(PHPMD.StaticAccess) |
|
| 109 | 66 | */ |
|
| 110 | public function __construct(Connection $connection, string $modelClass, ModelSchemaInfoInterface $modelSchemas) |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @return string |
||
| 127 | */ |
||
| 128 | public function getModelClass(): string |
||
| 132 | |||
| 133 | 52 | /** |
|
| 134 | 52 | * @param string|null $tableAlias |
|
| 135 | * @param string|null $modelClass |
||
| 136 | 52 | * |
|
| 137 | * @return array |
||
| 138 | 52 | * |
|
| 139 | 52 | * @throws DBALException |
|
| 140 | 52 | */ |
|
| 141 | 52 | public function getModelColumns(string $tableAlias = null, string $modelClass = null): array |
|
| 166 | 57 | ||
| 167 | 5 | /** |
|
| 168 | 5 | * Select all fields associated with model. |
|
| 169 | 5 | * |
|
| 170 | * @param iterable|null $columns |
||
| 171 | * |
||
| 172 | 52 | * @return self |
|
| 173 | * |
||
| 174 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
| 175 | 57 | * |
|
| 176 | * @throws DBALException |
||
| 177 | 57 | */ |
|
| 178 | public function selectModelColumns(iterable $columns = null): self |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @return self |
||
| 196 | * |
||
| 197 | * @throws DBALException |
||
| 198 | */ |
||
| 199 | 66 | public function distinct(): self |
|
| 207 | |||
| 208 | /** |
||
| 209 | * @param Closure $columnMapper |
||
| 210 | * |
||
| 211 | 57 | * @return self |
|
| 212 | */ |
||
| 213 | 57 | public function setColumnToDatabaseMapper(Closure $columnMapper): self |
|
| 219 | |||
| 220 | /** |
||
| 221 | * @return self |
||
| 222 | * |
||
| 223 | * @throws DBALException |
||
| 224 | */ |
||
| 225 | public function fromModelTable(): self |
||
| 234 | 5 | ||
| 235 | 5 | /** |
|
| 236 | 5 | * @param iterable $attributes |
|
| 237 | * |
||
| 238 | 5 | * @return self |
|
| 239 | * |
||
| 240 | 5 | * @throws DBALException |
|
| 241 | * |
||
| 242 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 243 | */ |
||
| 244 | public function createModel(iterable $attributes): self |
||
| 256 | 6 | ||
| 257 | 6 | /** |
|
| 258 | * @param iterable $attributes |
||
| 259 | * |
||
| 260 | 6 | * @return self |
|
| 261 | * |
||
| 262 | * @throws DBALException |
||
| 263 | * |
||
| 264 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 265 | */ |
||
| 266 | public function updateModels(iterable $attributes): self |
||
| 276 | 11 | ||
| 277 | /** |
||
| 278 | 11 | * @param string $modelClass |
|
| 279 | 11 | * @param iterable $attributes |
|
| 280 | * |
||
| 281 | 11 | * @return iterable |
|
|
|
|||
| 282 | 11 | * |
|
| 283 | 11 | * @SuppressWarnings(PHPMD.StaticAccess) |
|
| 284 | 11 | * |
|
| 285 | * @throws DBALException |
||
| 286 | 11 | */ |
|
| 287 | public function bindAttributes(string $modelClass, iterable $attributes): iterable |
||
| 303 | |||
| 304 | /** |
||
| 305 | * @return self |
||
| 306 | * |
||
| 307 | * @throws DBALException |
||
| 308 | */ |
||
| 309 | public function deleteModels(): self |
||
| 315 | |||
| 316 | /** |
||
| 317 | 5 | * @param string $relationshipName |
|
| 318 | * @param string $identity |
||
| 319 | * @param string $secondaryIdBindName |
||
| 320 | 5 | * |
|
| 321 | 5 | * @return self |
|
| 322 | 5 | * |
|
| 323 | 5 | * @throws DBALException |
|
| 324 | */ |
||
| 325 | public function prepareCreateInToManyRelationship( |
||
| 342 | |||
| 343 | /** |
||
| 344 | 1 | * @param string $relationshipName |
|
| 345 | * @param string $identity |
||
| 346 | * @param iterable $secondaryIds |
||
| 347 | 1 | * |
|
| 348 | 1 | * @return ModelQueryBuilder |
|
| 349 | * |
||
| 350 | * @throws DBALException |
||
| 351 | 1 | */ |
|
| 352 | public function prepareDeleteInToManyRelationship( |
||
| 374 | 2 | ||
| 375 | 2 | /** |
|
| 376 | * @param string $relationshipName |
||
| 377 | 2 | * @param string $identity |
|
| 378 | 2 | * |
|
| 379 | * @return self |
||
| 380 | 2 | * |
|
| 381 | * @throws DBALException |
||
| 382 | 2 | */ |
|
| 383 | public function clearToManyRelationship(string $relationshipName, string $identity): self |
||
| 398 | 9 | ||
| 399 | /** |
||
| 400 | * @param iterable $filters |
||
| 401 | * |
||
| 402 | * @return self |
||
| 403 | * |
||
| 404 | * @throws DBALException |
||
| 405 | */ |
||
| 406 | public function addFiltersWithAndToTable(iterable $filters): self |
||
| 414 | 1 | ||
| 415 | /** |
||
| 416 | * @param iterable $filters |
||
| 417 | * |
||
| 418 | * @return self |
||
| 419 | * |
||
| 420 | * @throws DBALException |
||
| 421 | */ |
||
| 422 | public function addFiltersWithOrToTable(iterable $filters): self |
||
| 430 | 38 | ||
| 431 | /** |
||
| 432 | * @param iterable $filters |
||
| 433 | * |
||
| 434 | * @return self |
||
| 435 | * |
||
| 436 | * @throws DBALException |
||
| 437 | */ |
||
| 438 | public function addFiltersWithAndToAlias(iterable $filters): self |
||
| 446 | 2 | ||
| 447 | /** |
||
| 448 | * @param iterable $filters |
||
| 449 | * |
||
| 450 | * @return self |
||
| 451 | * |
||
| 452 | * @throws DBALException |
||
| 453 | */ |
||
| 454 | public function addFiltersWithOrToAlias(iterable $filters): self |
||
| 462 | |||
| 463 | /** |
||
| 464 | 29 | * @param string $relationshipName |
|
| 465 | * @param iterable|null $relationshipFilters |
||
| 466 | * @param iterable|null $relationshipSorts |
||
| 467 | * @param int $joinIndividuals |
||
| 468 | * @param int $joinRelationship |
||
| 469 | * |
||
| 470 | * @return self |
||
| 471 | 29 | * |
|
| 472 | * @throws DBALException |
||
| 473 | 29 | * |
|
| 474 | 29 | * @SuppressWarnings(PHPMD.ElseExpression) |
|
| 475 | 29 | * @SuppressWarnings(PHPMD.NPathComplexity) |
|
| 476 | * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
||
| 477 | */ |
||
| 478 | 29 | public function addRelationshipFiltersAndSorts( |
|
| 540 | |||
| 541 | /** |
||
| 542 | * @param iterable $sortParameters |
||
| 543 | * |
||
| 544 | * @return self |
||
| 545 | * |
||
| 546 | 1 | * @throws DBALException |
|
| 547 | */ |
||
| 548 | 1 | public function addSorts(iterable $sortParameters): self |
|
| 552 | |||
| 553 | /** |
||
| 554 | * @param string $column |
||
| 555 | * |
||
| 556 | * @return string |
||
| 557 | * |
||
| 558 | 23 | * @throws DBALException |
|
| 559 | */ |
||
| 560 | 23 | public function getQuotedMainTableColumn(string $column): string |
|
| 564 | |||
| 565 | /** |
||
| 566 | * @param string $column |
||
| 567 | * |
||
| 568 | * @return string |
||
| 569 | * |
||
| 570 | 21 | * @throws DBALException |
|
| 571 | */ |
||
| 572 | 21 | public function getQuotedMainAliasColumn(string $column): string |
|
| 576 | 4 | ||
| 577 | 4 | /** |
|
| 578 | 4 | * @param string $name |
|
| 579 | 4 | * |
|
| 580 | 4 | * @return string Table alias. |
|
| 581 | 4 | * |
|
| 582 | * @throws DBALException |
||
| 583 | 4 | */ |
|
| 584 | public function createRelationshipAlias(string $name): string |
||
| 633 | |||
| 634 | /** |
||
| 635 | * @return string |
||
| 636 | */ |
||
| 637 | public function getAlias(): string |
||
| 641 | 46 | ||
| 642 | 46 | /** |
|
| 643 | 46 | * @param CompositeExpression $expression |
|
| 644 | * @param string $tableOrAlias |
||
| 645 | 46 | * @param iterable $filters |
|
| 646 | 46 | * |
|
| 647 | 46 | * @return self |
|
| 648 | 46 | * |
|
| 649 | 46 | * @throws DBALException |
|
| 650 | 46 | * @throws InvalidArgumentException |
|
| 651 | */ |
||
| 652 | 46 | public function applyFilters(CompositeExpression $expression, string $tableOrAlias, iterable $filters): self |
|
| 672 | 8 | ||
| 673 | /** |
||
| 674 | * @param string $tableOrAlias |
||
| 675 | 8 | * @param iterable $sorts |
|
| 676 | * |
||
| 677 | * @return self |
||
| 678 | * |
||
| 679 | * @throws DBALException |
||
| 680 | */ |
||
| 681 | public function applySorts(string $tableOrAlias, iterable $sorts): self |
||
| 691 | |||
| 692 | /** |
||
| 693 | * @param string $tableOrColumn |
||
| 694 | * |
||
| 695 | * @return string |
||
| 696 | * |
||
| 697 | * @throws DBALException |
||
| 698 | 64 | */ |
|
| 699 | public function quoteSingleIdentifier(string $tableOrColumn): string |
||
| 703 | |||
| 704 | /** |
||
| 705 | * @param string $tableOrAlias |
||
| 706 | * @param string $column |
||
| 707 | * |
||
| 708 | * @return string |
||
| 709 | * |
||
| 710 | * @throws DBALException |
||
| 711 | */ |
||
| 712 | 58 | public function quoteDoubleIdentifier(string $tableOrAlias, string $column): string |
|
| 718 | |||
| 719 | /** |
||
| 720 | * @param $value |
||
| 721 | * |
||
| 722 | * @return string |
||
| 723 | * |
||
| 724 | * @throws DBALException |
||
| 725 | */ |
||
| 726 | 18 | public function createSingleValueNamedParameter($value): string |
|
| 732 | |||
| 733 | /** |
||
| 734 | 18 | * @param iterable $values |
|
| 735 | * |
||
| 736 | * @return array |
||
| 737 | * |
||
| 738 | * @throws DBALException |
||
| 739 | */ |
||
| 740 | public function createArrayValuesNamedParameter(iterable $values): array |
||
| 750 | |||
| 751 | /** |
||
| 752 | * @param string $tableName |
||
| 753 | * |
||
| 754 | * @return string |
||
| 755 | */ |
||
| 756 | public function createAlias(string $tableName): string |
||
| 763 | |||
| 764 | /** |
||
| 765 | * @param string $fromAlias |
||
| 766 | 21 | * @param string $fromColumn |
|
| 767 | 21 | * @param string $targetTable |
|
| 768 | 21 | * @param string $targetColumn |
|
| 769 | * |
||
| 770 | 21 | * @return string |
|
| 771 | 21 | * |
|
| 772 | 21 | * @throws DBALException |
|
| 773 | 21 | */ |
|
| 774 | 21 | public function innerJoinOneTable( |
|
| 793 | 10 | ||
| 794 | /** |
||
| 795 | * @param string $fromAlias |
||
| 796 | * @param string $fromColumn |
||
| 797 | * @param string $intTable |
||
| 798 | * @param string $intToFromColumn |
||
| 799 | * @param string $intToTargetColumn |
||
| 800 | * @param string $targetTable |
||
| 801 | * @param string $targetColumn |
||
| 802 | 10 | * |
|
| 803 | 10 | * @return string |
|
| 804 | * |
||
| 805 | 10 | * @throws DBALException |
|
| 806 | */ |
||
| 807 | public function innerJoinTwoSequentialTables( |
||
| 821 | |||
| 822 | 11 | /** |
|
| 823 | * @param string $name |
||
| 824 | * |
||
| 825 | * @return Type |
||
| 826 | * |
||
| 827 | * @throws DBALException |
||
| 828 | 66 | * |
|
| 829 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 830 | 66 | */ |
|
| 831 | protected function getDbalType(string $name): Type |
||
| 838 | 66 | ||
| 839 | /** |
||
| 840 | * @return string |
||
| 841 | */ |
||
| 842 | private function getTableName(): string |
||
| 846 | |||
| 847 | /** |
||
| 848 | * @return ModelSchemaInfoInterface |
||
| 849 | */ |
||
| 850 | private function getModelSchemas(): ModelSchemaInfoInterface |
||
| 854 | 59 | ||
| 855 | /** |
||
| 856 | * @param string $fullColumnName |
||
| 857 | 59 | * @param int $operation |
|
| 858 | 48 | * @param iterable $arguments |
|
| 859 | 47 | * |
|
| 860 | 47 | * @return string |
|
| 861 | 30 | * |
|
| 862 | 1 | * @throws DBALException |
|
| 863 | 1 | * @throws InvalidArgumentException |
|
| 864 | 1 | * |
|
| 865 | 30 | * @SuppressWarnings(PHPMD.StaticAccess) |
|
| 866 | 6 | * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
|
| 867 | 6 | */ |
|
| 868 | 6 | private function createFilterExpression(string $fullColumnName, int $operation, iterable $arguments): string |
|
| 923 | |||
| 924 | 1 | /** |
|
| 925 | * @param iterable $arguments |
||
| 926 | * |
||
| 927 | * @return mixed |
||
| 928 | * |
||
| 929 | * @throws InvalidArgumentException |
||
| 930 | 52 | */ |
|
| 931 | private function firstValue(iterable $arguments) |
||
| 940 | |||
| 941 | /** |
||
| 942 | 58 | * @return Closure |
|
| 943 | */ |
||
| 944 | 58 | private function getColumnToDatabaseMapper(): Closure |
|
| 948 | |||
| 949 | /** |
||
| 950 | * @param mixed $value |
||
| 951 | * |
||
| 952 | * @return mixed |
||
| 953 | * |
||
| 954 | 1 | * @throws DBALException |
|
| 955 | */ |
||
| 956 | 1 | private function getPdoValue($value) |
|
| 960 | |||
| 961 | /** |
||
| 962 | * @param DateTimeInterface $dateTime |
||
| 963 | * |
||
| 964 | * @return string |
||
| 965 | * |
||
| 966 | * @throws DBALException |
||
| 967 | */ |
||
| 968 | private function convertDataTimeToDatabaseFormat(DateTimeInterface $dateTime): string |
||
| 975 | 37 | ||
| 976 | 1 | /** |
|
| 977 | * @param mixed $value |
||
| 978 | 36 | * |
|
| 979 | 36 | * @return int |
|
| 980 | * |
||
| 981 | 36 | * @SuppressWarnings(PHPMD.ElseExpression) |
|
| 982 | */ |
||
| 983 | 36 | private function getPdoType($value): int |
|
| 1003 | 1 | ||
| 1004 | /** |
||
| 1005 | * @return Type |
||
| 1006 | * |
||
| 1007 | * @throws DBALException |
||
| 1008 | * |
||
| 1009 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 1010 | */ |
||
| 1011 | private function getDateTimeType(): Type |
||
| 1019 | } |
||
| 1020 |
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.