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 | 52 | public function getModelColumns(string $tableAlias = null, string $modelClass = null): array |
|
150 | |||
151 | /** |
||
152 | * Select all fields associated with model. |
||
153 | * |
||
154 | * @param iterable|null $columns |
||
155 | * |
||
156 | * @return self |
||
157 | * |
||
158 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
159 | * |
||
160 | * @throws DBALException |
||
161 | */ |
||
162 | 57 | public function selectModelColumns(iterable $columns = null): self |
|
177 | |||
178 | /** |
||
179 | * @return self |
||
180 | * |
||
181 | * @throws DBALException |
||
182 | */ |
||
183 | 14 | public function distinct(): self |
|
191 | |||
192 | /** |
||
193 | * @param Closure $columnMapper |
||
194 | * |
||
195 | * @return self |
||
196 | */ |
||
197 | 66 | public function setColumnToDatabaseMapper(Closure $columnMapper): self |
|
203 | |||
204 | /** |
||
205 | * @return self |
||
206 | * |
||
207 | * @throws DBALException |
||
208 | */ |
||
209 | 57 | public function fromModelTable(): self |
|
218 | |||
219 | /** |
||
220 | * @param iterable $attributes |
||
221 | * |
||
222 | * @return self |
||
223 | * |
||
224 | * @throws DBALException |
||
225 | * |
||
226 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
227 | */ |
||
228 | 5 | public function createModel(iterable $attributes): self |
|
240 | |||
241 | /** |
||
242 | * @param iterable $attributes |
||
243 | * |
||
244 | * @return self |
||
245 | * |
||
246 | * @throws DBALException |
||
247 | * |
||
248 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
249 | */ |
||
250 | 6 | public function updateModels(iterable $attributes): self |
|
260 | |||
261 | /** |
||
262 | * @param string $modelClass |
||
263 | * @param iterable $attributes |
||
264 | * |
||
265 | * @return iterable |
||
|
|||
266 | * |
||
267 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
268 | * |
||
269 | * @throws DBALException |
||
270 | */ |
||
271 | 11 | public function bindAttributes(string $modelClass, iterable $attributes): iterable |
|
287 | |||
288 | /** |
||
289 | * @return self |
||
290 | * |
||
291 | * @throws DBALException |
||
292 | */ |
||
293 | 4 | public function deleteModels(): self |
|
299 | |||
300 | /** |
||
301 | * @param string $relationshipName |
||
302 | * @param string $identity |
||
303 | * @param string $secondaryIdBindName |
||
304 | * |
||
305 | * @return self |
||
306 | * |
||
307 | * @throws DBALException |
||
308 | */ |
||
309 | 5 | public function prepareCreateInToManyRelationship( |
|
326 | |||
327 | /** |
||
328 | * @param string $relationshipName |
||
329 | * @param string $identity |
||
330 | * @param iterable $secondaryIds |
||
331 | * |
||
332 | * @return ModelQueryBuilder |
||
333 | * |
||
334 | * @throws DBALException |
||
335 | */ |
||
336 | 1 | public function prepareDeleteInToManyRelationship( |
|
358 | |||
359 | /** |
||
360 | * @param string $relationshipName |
||
361 | * @param string $identity |
||
362 | * |
||
363 | * @return self |
||
364 | * |
||
365 | * @throws DBALException |
||
366 | */ |
||
367 | 2 | public function clearToManyRelationship(string $relationshipName, string $identity): self |
|
382 | |||
383 | /** |
||
384 | * @param iterable $filters |
||
385 | * |
||
386 | * @return self |
||
387 | * |
||
388 | * @throws DBALException |
||
389 | */ |
||
390 | 9 | public function addFiltersWithAndToTable(iterable $filters): self |
|
398 | |||
399 | /** |
||
400 | * @param iterable $filters |
||
401 | * |
||
402 | * @return self |
||
403 | * |
||
404 | * @throws DBALException |
||
405 | */ |
||
406 | 1 | public function addFiltersWithOrToTable(iterable $filters): self |
|
414 | |||
415 | /** |
||
416 | * @param iterable $filters |
||
417 | * |
||
418 | * @return self |
||
419 | * |
||
420 | * @throws DBALException |
||
421 | */ |
||
422 | 39 | public function addFiltersWithAndToAlias(iterable $filters): self |
|
430 | |||
431 | /** |
||
432 | * @param iterable $filters |
||
433 | * |
||
434 | * @return self |
||
435 | * |
||
436 | * @throws DBALException |
||
437 | */ |
||
438 | 2 | public function addFiltersWithOrToAlias(iterable $filters): self |
|
446 | |||
447 | /** |
||
448 | * @param string $relationshipName |
||
449 | * @param iterable|null $relationshipFilters |
||
450 | * @param iterable|null $relationshipSorts |
||
451 | * @param int $joinIndividuals |
||
452 | * @param int $joinRelationship |
||
453 | * |
||
454 | * @return self |
||
455 | * |
||
456 | * @throws DBALException |
||
457 | * |
||
458 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
459 | * @SuppressWarnings(PHPMD.NPathComplexity) |
||
460 | * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
||
461 | */ |
||
462 | 29 | public function addRelationshipFiltersAndSorts( |
|
524 | |||
525 | /** |
||
526 | * @param iterable $sortParameters |
||
527 | * |
||
528 | * @return self |
||
529 | * |
||
530 | * @throws DBALException |
||
531 | */ |
||
532 | 8 | public function addSorts(iterable $sortParameters): self |
|
536 | |||
537 | /** |
||
538 | * @param string $column |
||
539 | * |
||
540 | * @return string |
||
541 | * |
||
542 | * @throws DBALException |
||
543 | */ |
||
544 | 1 | public function getQuotedMainTableColumn(string $column): string |
|
548 | |||
549 | /** |
||
550 | * @param string $column |
||
551 | * |
||
552 | * @return string |
||
553 | * |
||
554 | * @throws DBALException |
||
555 | */ |
||
556 | 23 | public function getQuotedMainAliasColumn(string $column): string |
|
560 | |||
561 | /** |
||
562 | * @param string $name |
||
563 | * |
||
564 | * @return string Table alias. |
||
565 | * |
||
566 | * @throws DBALException |
||
567 | */ |
||
568 | 21 | public function createRelationshipAlias(string $name): string |
|
617 | |||
618 | /** |
||
619 | * @return string |
||
620 | */ |
||
621 | 58 | public function getAlias(): string |
|
625 | |||
626 | /** |
||
627 | * @param CompositeExpression $expression |
||
628 | * @param string $tableOrAlias |
||
629 | * @param iterable $filters |
||
630 | * |
||
631 | * @return self |
||
632 | * |
||
633 | * @throws DBALException |
||
634 | * @throws InvalidArgumentException |
||
635 | */ |
||
636 | 46 | public function applyFilters(CompositeExpression $expression, string $tableOrAlias, iterable $filters): self |
|
656 | |||
657 | /** |
||
658 | * @param string $tableOrAlias |
||
659 | * @param iterable $sorts |
||
660 | * |
||
661 | * @return self |
||
662 | * |
||
663 | * @throws DBALException |
||
664 | */ |
||
665 | 8 | public function applySorts(string $tableOrAlias, iterable $sorts): self |
|
675 | |||
676 | /** |
||
677 | * @param string $tableOrColumn |
||
678 | * |
||
679 | * @return string |
||
680 | * |
||
681 | * @throws DBALException |
||
682 | */ |
||
683 | 65 | public function quoteSingleIdentifier(string $tableOrColumn): string |
|
687 | |||
688 | /** |
||
689 | * @param string $tableOrAlias |
||
690 | * @param string $column |
||
691 | * |
||
692 | * @return string |
||
693 | * |
||
694 | * @throws DBALException |
||
695 | */ |
||
696 | 64 | public function quoteDoubleIdentifier(string $tableOrAlias, string $column): string |
|
702 | |||
703 | /** |
||
704 | * @param $value |
||
705 | * |
||
706 | * @return string |
||
707 | * |
||
708 | * @throws DBALException |
||
709 | */ |
||
710 | 58 | public function createSingleValueNamedParameter($value): string |
|
716 | |||
717 | /** |
||
718 | * @param iterable $values |
||
719 | * |
||
720 | * @return array |
||
721 | * |
||
722 | * @throws DBALException |
||
723 | */ |
||
724 | 18 | public function createArrayValuesNamedParameter(iterable $values): array |
|
734 | |||
735 | /** |
||
736 | * @param string $tableName |
||
737 | * |
||
738 | * @return string |
||
739 | */ |
||
740 | 66 | public function createAlias(string $tableName): string |
|
747 | |||
748 | /** |
||
749 | * @param string $fromAlias |
||
750 | * @param string $fromColumn |
||
751 | * @param string $targetTable |
||
752 | * @param string $targetColumn |
||
753 | * |
||
754 | * @return string |
||
755 | * |
||
756 | * @throws DBALException |
||
757 | */ |
||
758 | 21 | public function innerJoinOneTable( |
|
777 | |||
778 | /** |
||
779 | * @param string $fromAlias |
||
780 | * @param string $fromColumn |
||
781 | * @param string $intTable |
||
782 | * @param string $intToFromColumn |
||
783 | * @param string $intToTargetColumn |
||
784 | * @param string $targetTable |
||
785 | * @param string $targetColumn |
||
786 | * |
||
787 | * @return string |
||
788 | * |
||
789 | * @throws DBALException |
||
790 | */ |
||
791 | 10 | public function innerJoinTwoSequentialTables( |
|
805 | |||
806 | /** |
||
807 | * @param string $name |
||
808 | * |
||
809 | * @return Type |
||
810 | * |
||
811 | * @throws DBALException |
||
812 | * |
||
813 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
814 | */ |
||
815 | 11 | protected function getDbalType(string $name): Type |
|
822 | |||
823 | /** |
||
824 | * @return string |
||
825 | */ |
||
826 | 66 | private function getTableName(): string |
|
830 | |||
831 | /** |
||
832 | * @return ModelSchemaInfoInterface |
||
833 | */ |
||
834 | 66 | private function getModelSchemas(): ModelSchemaInfoInterface |
|
838 | |||
839 | /** |
||
840 | * @param string $fullColumnName |
||
841 | * @param int $operation |
||
842 | * @param iterable $arguments |
||
843 | * |
||
844 | * @return string |
||
845 | * |
||
846 | * @throws DBALException |
||
847 | * @throws InvalidArgumentException |
||
848 | * |
||
849 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
850 | * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
||
851 | */ |
||
852 | 59 | private function createFilterExpression(string $fullColumnName, int $operation, iterable $arguments): string |
|
907 | |||
908 | /** |
||
909 | * @param iterable $arguments |
||
910 | * |
||
911 | * @return mixed |
||
912 | * |
||
913 | * @throws InvalidArgumentException |
||
914 | */ |
||
915 | 55 | private function firstValue(iterable $arguments) |
|
924 | |||
925 | /** |
||
926 | * @return Closure |
||
927 | */ |
||
928 | 52 | private function getColumnToDatabaseMapper(): Closure |
|
932 | |||
933 | /** |
||
934 | * @param mixed $value |
||
935 | * |
||
936 | * @return mixed |
||
937 | * |
||
938 | * @throws DBALException |
||
939 | */ |
||
940 | 58 | private function getPdoValue($value) |
|
944 | |||
945 | /** |
||
946 | * @param DateTimeInterface $dateTime |
||
947 | * |
||
948 | * @return string |
||
949 | * |
||
950 | * @throws DBALException |
||
951 | */ |
||
952 | 1 | private function convertDataTimeToDatabaseFormat(DateTimeInterface $dateTime): string |
|
959 | |||
960 | /** |
||
961 | * @param mixed $value |
||
962 | * |
||
963 | * @return int |
||
964 | * |
||
965 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
966 | */ |
||
967 | 58 | private function getPdoType($value): int |
|
987 | |||
988 | /** |
||
989 | * @return Type |
||
990 | * |
||
991 | * @throws DBALException |
||
992 | * |
||
993 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
994 | */ |
||
995 | 1 | private function getDateTimeType(): Type |
|
1003 | } |
||
1004 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.