Complex classes like MigrationTrait 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 MigrationTrait, and based on these observations, apply Extract Interface, too.
1 | <?php namespace Limoncello\Data\Migrations; |
||
35 | trait MigrationTrait |
||
36 | { |
||
37 | /** |
||
38 | * @var ContainerInterface |
||
39 | */ |
||
40 | private $container; |
||
41 | |||
42 | /** |
||
43 | * @var array |
||
44 | */ |
||
45 | private $enumerations = []; |
||
46 | |||
47 | /** |
||
48 | * @inheritdoc |
||
49 | */ |
||
50 | 6 | public function init(ContainerInterface $container): MigrationInterface |
|
59 | |||
60 | /** |
||
61 | * @return ContainerInterface |
||
62 | */ |
||
63 | 6 | protected function getContainer(): ContainerInterface |
|
67 | |||
68 | /** |
||
69 | * @return Connection |
||
70 | */ |
||
71 | 6 | protected function getConnection(): Connection |
|
77 | |||
78 | /** |
||
79 | * @return ModelSchemaInfoInterface |
||
80 | */ |
||
81 | 2 | protected function getModelSchemas(): ModelSchemaInfoInterface |
|
87 | |||
88 | /** |
||
89 | * @return AbstractSchemaManager |
||
90 | */ |
||
91 | 2 | protected function getSchemaManager(): AbstractSchemaManager |
|
95 | |||
96 | /** |
||
97 | * @param string $modelClass |
||
98 | * @param Closure[] $expressions |
||
99 | * |
||
100 | * @return Table |
||
101 | * |
||
102 | * @throws DBALException |
||
103 | */ |
||
104 | 2 | protected function createTable(string $modelClass, array $expressions = []): Table |
|
118 | |||
119 | /** |
||
120 | * @param string $modelClass |
||
121 | * |
||
122 | * @return void |
||
123 | */ |
||
124 | 1 | protected function dropTableIfExists(string $modelClass): void |
|
133 | |||
134 | /** |
||
135 | * @param string $name |
||
136 | * @param array $values |
||
137 | * |
||
138 | * @return void |
||
139 | * |
||
140 | * @throws DBALException |
||
141 | */ |
||
142 | 2 | protected function createEnum(string $name, array $values): void |
|
169 | |||
170 | /** |
||
171 | * @param string $name |
||
172 | * |
||
173 | * @return void |
||
174 | * |
||
175 | * @throws DBALException |
||
176 | */ |
||
177 | 1 | protected function dropEnumIfExists(string $name): void |
|
188 | |||
189 | /** |
||
190 | * @param string $columnName |
||
191 | * @param string $enumName |
||
192 | * @param bool $notNullable |
||
193 | * |
||
194 | * @return Closure |
||
195 | * |
||
196 | * @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
||
197 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
198 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
199 | */ |
||
200 | 2 | protected function useEnum(string $columnName, string $enumName, bool $notNullable = true): Closure |
|
222 | |||
223 | /** |
||
224 | * @param string $name |
||
225 | * |
||
226 | * @return Closure |
||
227 | */ |
||
228 | 2 | protected function primaryInt(string $name): Closure |
|
235 | |||
236 | /** |
||
237 | * @param string $name |
||
238 | * |
||
239 | * @return Closure |
||
240 | */ |
||
241 | 1 | protected function primaryString(string $name): Closure |
|
249 | |||
250 | /** |
||
251 | * @param string $name |
||
252 | * @param null|int $default |
||
253 | * |
||
254 | * @return Closure |
||
255 | */ |
||
256 | 1 | protected function int(string $name, int $default = null): Closure |
|
263 | |||
264 | /** |
||
265 | * @param string $name |
||
266 | * @param null|int $default |
||
267 | * |
||
268 | * @return Closure |
||
269 | */ |
||
270 | 1 | protected function nullableInt(string $name, int $default = null): Closure |
|
276 | |||
277 | /** |
||
278 | * @param string $name |
||
279 | * @param null|int $default |
||
280 | * |
||
281 | * @return Closure |
||
282 | */ |
||
283 | 1 | protected function unsignedInt(string $name, int $default = null): Closure |
|
287 | |||
288 | /** |
||
289 | * @param string $name |
||
290 | * @param null|int $default |
||
291 | * |
||
292 | * @return Closure |
||
293 | */ |
||
294 | 1 | protected function nullableUnsignedInt(string $name, int $default = null): Closure |
|
298 | |||
299 | /** |
||
300 | * @param string $name |
||
301 | * |
||
302 | * @return Closure |
||
303 | */ |
||
304 | 1 | protected function float(string $name): Closure |
|
312 | |||
313 | /** |
||
314 | * @param string $name |
||
315 | * |
||
316 | * @return Closure |
||
317 | */ |
||
318 | 1 | protected function string(string $name): Closure |
|
325 | |||
326 | /** |
||
327 | * @param string $name |
||
328 | * |
||
329 | * @return Closure |
||
330 | */ |
||
331 | 1 | protected function nullableString(string $name): Closure |
|
338 | |||
339 | /** |
||
340 | * @param string $name |
||
341 | * |
||
342 | * @return Closure |
||
343 | */ |
||
344 | 1 | protected function text(string $name): Closure |
|
350 | |||
351 | /** |
||
352 | * @param string $name |
||
353 | * |
||
354 | * @return Closure |
||
355 | */ |
||
356 | 1 | protected function nullableText(string $name): Closure |
|
362 | |||
363 | /** |
||
364 | * @param string $name |
||
365 | * @param null|bool $default |
||
366 | * |
||
367 | * @return Closure |
||
368 | */ |
||
369 | 1 | protected function bool(string $name, $default = null): Closure |
|
378 | |||
379 | /** |
||
380 | * @param string $name |
||
381 | * @param array $values |
||
382 | * |
||
383 | * @return Closure |
||
384 | * |
||
385 | * @throws DBALException |
||
386 | */ |
||
387 | 1 | protected function enum(string $name, array $values): Closure |
|
393 | |||
394 | /** |
||
395 | * @param string $name |
||
396 | * @param array $values |
||
397 | * |
||
398 | * @return Closure |
||
399 | * |
||
400 | * @throws DBALException |
||
401 | */ |
||
402 | 1 | protected function nullableEnum(string $name, array $values): Closure |
|
408 | |||
409 | /** |
||
410 | * @return Closure |
||
411 | */ |
||
412 | 1 | protected function timestamps(): Closure |
|
438 | |||
439 | /** |
||
440 | * @param string $name |
||
441 | * |
||
442 | * @return Closure |
||
443 | */ |
||
444 | 1 | protected function datetime(string $name): Closure |
|
450 | |||
451 | /** |
||
452 | * @param string $name |
||
453 | * |
||
454 | * @return Closure |
||
455 | */ |
||
456 | 1 | protected function nullableDatetime(string $name): Closure |
|
462 | |||
463 | /** |
||
464 | * @param string $name |
||
465 | * |
||
466 | * @return Closure |
||
467 | */ |
||
468 | 1 | protected function date(string $name): Closure |
|
474 | |||
475 | /** |
||
476 | * @param string $name |
||
477 | * |
||
478 | * @return Closure |
||
479 | */ |
||
480 | 1 | protected function nullableDate(string $name): Closure |
|
486 | |||
487 | /** |
||
488 | * @param string[] $names |
||
489 | * |
||
490 | * @return Closure |
||
491 | */ |
||
492 | 1 | protected function unique(array $names): Closure |
|
498 | |||
499 | /** |
||
500 | * @param string[] $names |
||
501 | * |
||
502 | * @return Closure |
||
503 | */ |
||
504 | 1 | protected function searchable(array $names): Closure |
|
510 | |||
511 | /** |
||
512 | * @param string $column |
||
513 | * @param string $referredClass |
||
514 | * @param bool $cascadeDelete |
||
515 | * |
||
516 | * @return Closure |
||
517 | * |
||
518 | * @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
||
519 | */ |
||
520 | 1 | protected function foreignRelationship( |
|
544 | |||
545 | /** |
||
546 | * @param string $column |
||
547 | * @param string $referredClass |
||
548 | * @param bool $cascadeDelete |
||
549 | * |
||
550 | * @return Closure |
||
551 | * |
||
552 | * @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
||
553 | */ |
||
554 | 1 | protected function nullableForeignRelationship( |
|
579 | |||
580 | /** @noinspection PhpTooManyParametersInspection |
||
581 | * @param string $localKey |
||
582 | * @param string $foreignTable |
||
583 | * @param string $foreignKey |
||
584 | * @param string $type |
||
585 | * @param int|null $length |
||
586 | * @param bool $cascadeDelete |
||
587 | * |
||
588 | * @return Closure |
||
589 | * |
||
590 | * @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
||
591 | */ |
||
592 | 1 | protected function foreignColumn( |
|
602 | |||
603 | /** @noinspection PhpTooManyParametersInspection |
||
604 | * @param string $localKey |
||
605 | * @param string $foreignTable |
||
606 | * @param string $foreignKey |
||
607 | * @param string $type |
||
608 | * @param int|null $length |
||
609 | * @param bool $cascadeDelete |
||
610 | * |
||
611 | * @return Closure |
||
612 | * |
||
613 | * @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
||
614 | */ |
||
615 | 1 | protected function nullableForeignColumn( |
|
625 | |||
626 | /** |
||
627 | * @param string $name |
||
628 | * @param bool $cascadeDelete |
||
629 | * |
||
630 | * @return Closure |
||
631 | * |
||
632 | * @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
||
633 | */ |
||
634 | 1 | protected function nullableRelationship(string $name, bool $cascadeDelete = false): Closure |
|
638 | |||
639 | /** |
||
640 | * @param string $name |
||
641 | * @param bool $cascadeDelete |
||
642 | * |
||
643 | * @return Closure |
||
644 | * |
||
645 | * @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
||
646 | */ |
||
647 | 1 | protected function relationship(string $name, bool $cascadeDelete = false): Closure |
|
651 | |||
652 | /** |
||
653 | * @param string $modelClass |
||
654 | * |
||
655 | * @return string |
||
656 | */ |
||
657 | 1 | protected function getTableNameForClass(string $modelClass): string |
|
668 | |||
669 | /** |
||
670 | * @param string $name |
||
671 | * @param mixed $value |
||
672 | * |
||
673 | * @return Closure |
||
674 | */ |
||
675 | 1 | protected function defaultValue(string $name, $value): Closure |
|
682 | |||
683 | /** |
||
684 | * @param string $name |
||
685 | * |
||
686 | * @return Closure |
||
687 | */ |
||
688 | 1 | protected function nullableValue(string $name): Closure |
|
695 | |||
696 | /** |
||
697 | * @param string $name |
||
698 | * |
||
699 | * @return Closure |
||
700 | */ |
||
701 | 1 | protected function notNullableValue(string $name): Closure |
|
708 | |||
709 | /** |
||
710 | * @param string $name |
||
711 | * @param bool $notNullable |
||
712 | * @param null|mixed $default |
||
713 | * |
||
714 | * @return Closure |
||
715 | */ |
||
716 | 1 | private function unsignedIntImpl(string $name, bool $notNullable, $default = null): Closure |
|
723 | |||
724 | /** @noinspection PhpTooManyParametersInspection |
||
725 | * @param string $localKey |
||
726 | * @param string $foreignTable |
||
727 | * @param string $foreignKey |
||
728 | * @param string $type |
||
729 | * @param int|null $length |
||
730 | * @param bool $notNullable |
||
731 | * @param bool $cascadeDelete |
||
732 | * |
||
733 | * @return Closure |
||
734 | */ |
||
735 | 1 | private function foreignColumnImpl( |
|
759 | |||
760 | /** |
||
761 | * @param string $name |
||
762 | * @param bool $notNullable |
||
763 | * @param bool $cascadeDelete |
||
764 | * |
||
765 | * @return Closure |
||
766 | */ |
||
767 | 1 | private function relationshipImpl(string $name, bool $notNullable, bool $cascadeDelete): Closure |
|
810 | } |
||
811 |