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 declare (strict_types = 1); |
||
| 42 | trait MigrationTrait |
||
| 43 | { |
||
| 44 | /** |
||
| 45 | * @var ContainerInterface |
||
| 46 | */ |
||
| 47 | private $container; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var array |
||
| 51 | */ |
||
| 52 | private $enumerations = []; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @inheritdoc |
||
| 56 | */ |
||
| 57 | 6 | public function init(ContainerInterface $container): MigrationInterface |
|
| 66 | |||
| 67 | /** |
||
| 68 | * @return ContainerInterface |
||
| 69 | */ |
||
| 70 | 6 | protected function getContainer(): ContainerInterface |
|
| 74 | |||
| 75 | /** |
||
| 76 | * @return Connection |
||
| 77 | */ |
||
| 78 | 6 | protected function getConnection(): Connection |
|
| 84 | |||
| 85 | /** |
||
| 86 | * @return ModelSchemaInfoInterface |
||
| 87 | */ |
||
| 88 | 2 | protected function getModelSchemas(): ModelSchemaInfoInterface |
|
| 94 | |||
| 95 | /** |
||
| 96 | * @return AbstractSchemaManager |
||
| 97 | */ |
||
| 98 | 2 | protected function getSchemaManager(): AbstractSchemaManager |
|
| 102 | |||
| 103 | /** |
||
| 104 | * @param string $modelClass |
||
| 105 | * @param Closure[] $expressions |
||
| 106 | * |
||
| 107 | * @return Table |
||
| 108 | * |
||
| 109 | * @throws DBALException |
||
| 110 | */ |
||
| 111 | 2 | protected function createTable(string $modelClass, array $expressions = []): Table |
|
| 129 | |||
| 130 | /** |
||
| 131 | * @param string $modelClass |
||
| 132 | * |
||
| 133 | * @return void |
||
| 134 | */ |
||
| 135 | 1 | protected function dropTableIfExists(string $modelClass): void |
|
| 148 | |||
| 149 | /** |
||
| 150 | * @param string $name |
||
| 151 | * @param array $values |
||
| 152 | * |
||
| 153 | * @return void |
||
| 154 | * |
||
| 155 | * @throws DBALException |
||
| 156 | */ |
||
| 157 | 2 | protected function createEnum(string $name, array $values): void |
|
| 184 | |||
| 185 | /** |
||
| 186 | * @param string $name |
||
| 187 | * |
||
| 188 | * @return void |
||
| 189 | * |
||
| 190 | * @throws DBALException |
||
| 191 | */ |
||
| 192 | 1 | protected function dropEnumIfExists(string $name): void |
|
| 203 | |||
| 204 | /** |
||
| 205 | * @param string $columnName |
||
| 206 | * @param string $enumName |
||
| 207 | * @param bool $notNullable |
||
| 208 | * |
||
| 209 | * @return Closure |
||
| 210 | * |
||
| 211 | * @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
||
| 212 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
| 213 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 214 | */ |
||
| 215 | 2 | protected function useEnum(string $columnName, string $enumName, bool $notNullable = true): Closure |
|
| 237 | |||
| 238 | /** |
||
| 239 | * @param string $name |
||
| 240 | * |
||
| 241 | * @return Closure |
||
| 242 | */ |
||
| 243 | 2 | protected function primaryInt(string $name): Closure |
|
| 250 | |||
| 251 | /** |
||
| 252 | * @param string $name |
||
| 253 | * |
||
| 254 | * @return Closure |
||
| 255 | */ |
||
| 256 | 1 | protected function primaryString(string $name): Closure |
|
| 264 | |||
| 265 | /** |
||
| 266 | * @param string $name |
||
| 267 | * @param null|int $default |
||
| 268 | * |
||
| 269 | * @return Closure |
||
| 270 | */ |
||
| 271 | 1 | protected function int(string $name, int $default = null): Closure |
|
| 278 | |||
| 279 | /** |
||
| 280 | * @param string $name |
||
| 281 | * @param null|int $default |
||
| 282 | * |
||
| 283 | * @return Closure |
||
| 284 | */ |
||
| 285 | 1 | protected function nullableInt(string $name, int $default = null): Closure |
|
| 291 | |||
| 292 | /** |
||
| 293 | * @param string $name |
||
| 294 | * @param null|int $default |
||
| 295 | * |
||
| 296 | * @return Closure |
||
| 297 | */ |
||
| 298 | 1 | protected function unsignedInt(string $name, int $default = null): Closure |
|
| 302 | |||
| 303 | /** |
||
| 304 | * @param string $name |
||
| 305 | * @param null|int $default |
||
| 306 | * |
||
| 307 | * @return Closure |
||
| 308 | */ |
||
| 309 | 1 | protected function nullableUnsignedInt(string $name, int $default = null): Closure |
|
| 313 | |||
| 314 | /** |
||
| 315 | * @param string $name |
||
| 316 | * |
||
| 317 | * @return Closure |
||
| 318 | */ |
||
| 319 | 1 | protected function float(string $name): Closure |
|
| 327 | |||
| 328 | /** |
||
| 329 | * @param string $name |
||
| 330 | * |
||
| 331 | * @return Closure |
||
| 332 | */ |
||
| 333 | 1 | protected function string(string $name): Closure |
|
| 340 | |||
| 341 | /** |
||
| 342 | * @param string $name |
||
| 343 | * |
||
| 344 | * @return Closure |
||
| 345 | */ |
||
| 346 | 1 | protected function nullableString(string $name): Closure |
|
| 353 | |||
| 354 | /** |
||
| 355 | * @param string $name |
||
| 356 | * |
||
| 357 | * @return Closure |
||
| 358 | */ |
||
| 359 | 1 | protected function text(string $name): Closure |
|
| 365 | |||
| 366 | /** |
||
| 367 | * @param string $name |
||
| 368 | * |
||
| 369 | * @return Closure |
||
| 370 | */ |
||
| 371 | 1 | protected function nullableText(string $name): Closure |
|
| 377 | |||
| 378 | /** |
||
| 379 | * @param string $name |
||
| 380 | * @param null|bool $default |
||
| 381 | * |
||
| 382 | * @return Closure |
||
| 383 | */ |
||
| 384 | 1 | protected function bool(string $name, bool $default = null): Closure |
|
| 393 | |||
| 394 | /** |
||
| 395 | * @param string $name |
||
| 396 | * |
||
| 397 | * @return Closure |
||
| 398 | */ |
||
| 399 | 1 | protected function binary(string $name): Closure |
|
| 405 | |||
| 406 | /** |
||
| 407 | * @param string $name |
||
| 408 | * @param array $values |
||
| 409 | * |
||
| 410 | * @return Closure |
||
| 411 | * |
||
| 412 | * @throws DBALException |
||
| 413 | */ |
||
| 414 | 1 | protected function enum(string $name, array $values): Closure |
|
| 420 | |||
| 421 | /** |
||
| 422 | * @param string $name |
||
| 423 | * @param array $values |
||
| 424 | * |
||
| 425 | * @return Closure |
||
| 426 | * |
||
| 427 | * @throws DBALException |
||
| 428 | */ |
||
| 429 | 1 | protected function nullableEnum(string $name, array $values): Closure |
|
| 435 | |||
| 436 | /** |
||
| 437 | * @return Closure |
||
| 438 | */ |
||
| 439 | 1 | protected function timestamps(): Closure |
|
| 465 | |||
| 466 | /** |
||
| 467 | * @param string $name |
||
| 468 | * |
||
| 469 | * @return Closure |
||
| 470 | */ |
||
| 471 | 1 | protected function datetime(string $name): Closure |
|
| 477 | |||
| 478 | /** |
||
| 479 | * @param string $name |
||
| 480 | * |
||
| 481 | * @return Closure |
||
| 482 | */ |
||
| 483 | 1 | protected function nullableDatetime(string $name): Closure |
|
| 489 | |||
| 490 | /** |
||
| 491 | * @param string $name |
||
| 492 | * |
||
| 493 | * @return Closure |
||
| 494 | */ |
||
| 495 | 1 | protected function date(string $name): Closure |
|
| 501 | |||
| 502 | /** |
||
| 503 | * @param string $name |
||
| 504 | * |
||
| 505 | * @return Closure |
||
| 506 | */ |
||
| 507 | 1 | protected function nullableDate(string $name): Closure |
|
| 513 | |||
| 514 | /** |
||
| 515 | * @param string[] $names |
||
| 516 | * |
||
| 517 | * @return Closure |
||
| 518 | */ |
||
| 519 | 1 | protected function unique(array $names): Closure |
|
| 525 | |||
| 526 | /** |
||
| 527 | * @param string[] $names |
||
| 528 | * |
||
| 529 | * @return Closure |
||
| 530 | */ |
||
| 531 | 1 | protected function searchable(array $names): Closure |
|
| 537 | |||
| 538 | /** |
||
| 539 | * @param string $column |
||
| 540 | * @param string $referredClass |
||
| 541 | * @param string $onDeleteRestriction |
||
| 542 | * |
||
| 543 | * @return Closure |
||
| 544 | * |
||
| 545 | * @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
||
| 546 | */ |
||
| 547 | 1 | protected function foreignRelationship( |
|
| 578 | |||
| 579 | /** |
||
| 580 | * @param string $column |
||
| 581 | * @param string $referredClass |
||
| 582 | * @param string $onDeleteRestriction |
||
| 583 | * |
||
| 584 | * @return Closure |
||
| 585 | * |
||
| 586 | * @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
||
| 587 | */ |
||
| 588 | 1 | protected function nullableForeignRelationship( |
|
| 613 | |||
| 614 | /** @noinspection PhpTooManyParametersInspection |
||
| 615 | * @param string $localKey |
||
| 616 | * @param string $foreignTable |
||
| 617 | * @param string $foreignKey |
||
| 618 | * @param string $type |
||
| 619 | * @param int|null $length |
||
| 620 | * @param string $onDeleteRestriction |
||
| 621 | * |
||
| 622 | * @return Closure |
||
| 623 | * |
||
| 624 | * @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
||
| 625 | */ |
||
| 626 | 1 | protected function foreignColumn( |
|
| 644 | |||
| 645 | /** @noinspection PhpTooManyParametersInspection |
||
| 646 | * @param string $localKey |
||
| 647 | * @param string $foreignTable |
||
| 648 | * @param string $foreignKey |
||
| 649 | * @param string $type |
||
| 650 | * @param int|null $length |
||
| 651 | * @param string $onDeleteRestriction |
||
| 652 | * |
||
| 653 | * @return Closure |
||
| 654 | * |
||
| 655 | * @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
||
| 656 | */ |
||
| 657 | 1 | protected function nullableForeignColumn( |
|
| 675 | |||
| 676 | /** |
||
| 677 | * @param string $name |
||
| 678 | * @param string $onDeleteRestriction |
||
| 679 | * |
||
| 680 | * @return Closure |
||
| 681 | * |
||
| 682 | * @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
||
| 683 | */ |
||
| 684 | 1 | protected function nullableRelationship( |
|
| 690 | |||
| 691 | /** |
||
| 692 | * @param string $name |
||
| 693 | * @param string $onDeleteRestriction |
||
| 694 | * |
||
| 695 | * @return Closure |
||
| 696 | * |
||
| 697 | * @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
||
| 698 | */ |
||
| 699 | 1 | protected function relationship( |
|
| 705 | |||
| 706 | /** |
||
| 707 | * @param string $modelClass |
||
| 708 | * |
||
| 709 | * @return string |
||
| 710 | */ |
||
| 711 | 1 | protected function getTableNameForClass(string $modelClass): string |
|
| 722 | |||
| 723 | /** |
||
| 724 | * @param string $name |
||
| 725 | * @param mixed $value |
||
| 726 | * |
||
| 727 | * @return Closure |
||
| 728 | */ |
||
| 729 | 1 | protected function defaultValue(string $name, $value): Closure |
|
| 736 | |||
| 737 | /** |
||
| 738 | * @param string $name |
||
| 739 | * |
||
| 740 | * @return Closure |
||
| 741 | */ |
||
| 742 | 1 | protected function nullableValue(string $name): Closure |
|
| 749 | |||
| 750 | /** |
||
| 751 | * @param string $name |
||
| 752 | * |
||
| 753 | * @return Closure |
||
| 754 | */ |
||
| 755 | 1 | protected function notNullableValue(string $name): Closure |
|
| 762 | |||
| 763 | /** |
||
| 764 | * @param string $name |
||
| 765 | * @param bool $notNullable |
||
| 766 | * @param null|mixed $default |
||
| 767 | * |
||
| 768 | * @return Closure |
||
| 769 | */ |
||
| 770 | 1 | private function unsignedIntImpl(string $name, bool $notNullable, $default = null): Closure |
|
| 777 | |||
| 778 | /** @noinspection PhpTooManyParametersInspection |
||
| 779 | * @param string $localKey |
||
| 780 | * @param string $foreignTable |
||
| 781 | * @param string $foreignKey |
||
| 782 | * @param string $type |
||
| 783 | * @param int|null $length |
||
| 784 | * @param bool $notNullable |
||
| 785 | * @param string $onDeleteRestriction |
||
| 786 | * |
||
| 787 | * @return Closure |
||
| 788 | */ |
||
| 789 | 1 | private function foreignColumnImpl( |
|
| 817 | |||
| 818 | /** |
||
| 819 | * @param string $name |
||
| 820 | * @param bool $notNullable |
||
| 821 | * @param string $onDeleteRestriction |
||
| 822 | * |
||
| 823 | * @return Closure |
||
| 824 | */ |
||
| 825 | 1 | private function relationshipImpl(string $name, bool $notNullable, string $onDeleteRestriction): Closure |
|
| 872 | } |
||
| 873 |