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 | * |
||
| 382 | * @return Closure |
||
| 383 | */ |
||
| 384 | 1 | protected function binary(string $name): Closure |
|
| 390 | |||
| 391 | /** |
||
| 392 | * @param string $name |
||
| 393 | * @param array $values |
||
| 394 | * |
||
| 395 | * @return Closure |
||
| 396 | * |
||
| 397 | * @throws DBALException |
||
| 398 | */ |
||
| 399 | 1 | protected function enum(string $name, array $values): Closure |
|
| 405 | |||
| 406 | /** |
||
| 407 | * @param string $name |
||
| 408 | * @param array $values |
||
| 409 | * |
||
| 410 | * @return Closure |
||
| 411 | * |
||
| 412 | * @throws DBALException |
||
| 413 | */ |
||
| 414 | 1 | protected function nullableEnum(string $name, array $values): Closure |
|
| 420 | |||
| 421 | /** |
||
| 422 | * @return Closure |
||
| 423 | */ |
||
| 424 | 1 | protected function timestamps(): Closure |
|
| 450 | |||
| 451 | /** |
||
| 452 | * @param string $name |
||
| 453 | * |
||
| 454 | * @return Closure |
||
| 455 | */ |
||
| 456 | 1 | protected function datetime(string $name): Closure |
|
| 462 | |||
| 463 | /** |
||
| 464 | * @param string $name |
||
| 465 | * |
||
| 466 | * @return Closure |
||
| 467 | */ |
||
| 468 | 1 | protected function nullableDatetime(string $name): Closure |
|
| 474 | |||
| 475 | /** |
||
| 476 | * @param string $name |
||
| 477 | * |
||
| 478 | * @return Closure |
||
| 479 | */ |
||
| 480 | 1 | protected function date(string $name): Closure |
|
| 486 | |||
| 487 | /** |
||
| 488 | * @param string $name |
||
| 489 | * |
||
| 490 | * @return Closure |
||
| 491 | */ |
||
| 492 | 1 | protected function nullableDate(string $name): Closure |
|
| 498 | |||
| 499 | /** |
||
| 500 | * @param string[] $names |
||
| 501 | * |
||
| 502 | * @return Closure |
||
| 503 | */ |
||
| 504 | 1 | protected function unique(array $names): Closure |
|
| 510 | |||
| 511 | /** |
||
| 512 | * @param string[] $names |
||
| 513 | * |
||
| 514 | * @return Closure |
||
| 515 | */ |
||
| 516 | 1 | protected function searchable(array $names): Closure |
|
| 522 | |||
| 523 | /** |
||
| 524 | * @param string $column |
||
| 525 | * @param string $referredClass |
||
| 526 | * @param bool $cascadeDelete |
||
| 527 | * |
||
| 528 | * @return Closure |
||
| 529 | * |
||
| 530 | * @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
||
| 531 | */ |
||
| 532 | 1 | protected function foreignRelationship( |
|
| 556 | |||
| 557 | /** |
||
| 558 | * @param string $column |
||
| 559 | * @param string $referredClass |
||
| 560 | * @param bool $cascadeDelete |
||
| 561 | * |
||
| 562 | * @return Closure |
||
| 563 | * |
||
| 564 | * @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
||
| 565 | */ |
||
| 566 | 1 | protected function nullableForeignRelationship( |
|
| 591 | |||
| 592 | /** @noinspection PhpTooManyParametersInspection |
||
| 593 | * @param string $localKey |
||
| 594 | * @param string $foreignTable |
||
| 595 | * @param string $foreignKey |
||
| 596 | * @param string $type |
||
| 597 | * @param int|null $length |
||
| 598 | * @param bool $cascadeDelete |
||
| 599 | * |
||
| 600 | * @return Closure |
||
| 601 | * |
||
| 602 | * @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
||
| 603 | */ |
||
| 604 | 1 | protected function foreignColumn( |
|
| 614 | |||
| 615 | /** @noinspection PhpTooManyParametersInspection |
||
| 616 | * @param string $localKey |
||
| 617 | * @param string $foreignTable |
||
| 618 | * @param string $foreignKey |
||
| 619 | * @param string $type |
||
| 620 | * @param int|null $length |
||
| 621 | * @param bool $cascadeDelete |
||
| 622 | * |
||
| 623 | * @return Closure |
||
| 624 | * |
||
| 625 | * @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
||
| 626 | */ |
||
| 627 | 1 | protected function nullableForeignColumn( |
|
| 637 | |||
| 638 | /** |
||
| 639 | * @param string $name |
||
| 640 | * @param bool $cascadeDelete |
||
| 641 | * |
||
| 642 | * @return Closure |
||
| 643 | * |
||
| 644 | * @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
||
| 645 | */ |
||
| 646 | 1 | protected function nullableRelationship(string $name, bool $cascadeDelete = false): Closure |
|
| 650 | |||
| 651 | /** |
||
| 652 | * @param string $name |
||
| 653 | * @param bool $cascadeDelete |
||
| 654 | * |
||
| 655 | * @return Closure |
||
| 656 | * |
||
| 657 | * @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
||
| 658 | */ |
||
| 659 | 1 | protected function relationship(string $name, bool $cascadeDelete = false): Closure |
|
| 663 | |||
| 664 | /** |
||
| 665 | * @param string $modelClass |
||
| 666 | * |
||
| 667 | * @return string |
||
| 668 | */ |
||
| 669 | 1 | protected function getTableNameForClass(string $modelClass): string |
|
| 680 | |||
| 681 | /** |
||
| 682 | * @param string $name |
||
| 683 | * @param mixed $value |
||
| 684 | * |
||
| 685 | * @return Closure |
||
| 686 | */ |
||
| 687 | 1 | protected function defaultValue(string $name, $value): Closure |
|
| 694 | |||
| 695 | /** |
||
| 696 | * @param string $name |
||
| 697 | * |
||
| 698 | * @return Closure |
||
| 699 | */ |
||
| 700 | 1 | protected function nullableValue(string $name): Closure |
|
| 707 | |||
| 708 | /** |
||
| 709 | * @param string $name |
||
| 710 | * |
||
| 711 | * @return Closure |
||
| 712 | */ |
||
| 713 | 1 | protected function notNullableValue(string $name): Closure |
|
| 720 | |||
| 721 | /** |
||
| 722 | * @param string $name |
||
| 723 | * @param bool $notNullable |
||
| 724 | * @param null|mixed $default |
||
| 725 | * |
||
| 726 | * @return Closure |
||
| 727 | */ |
||
| 728 | 1 | private function unsignedIntImpl(string $name, bool $notNullable, $default = null): Closure |
|
| 735 | |||
| 736 | /** @noinspection PhpTooManyParametersInspection |
||
| 737 | * @param string $localKey |
||
| 738 | * @param string $foreignTable |
||
| 739 | * @param string $foreignKey |
||
| 740 | * @param string $type |
||
| 741 | * @param int|null $length |
||
| 742 | * @param bool $notNullable |
||
| 743 | * @param bool $cascadeDelete |
||
| 744 | * |
||
| 745 | * @return Closure |
||
| 746 | */ |
||
| 747 | 1 | private function foreignColumnImpl( |
|
| 771 | |||
| 772 | /** |
||
| 773 | * @param string $name |
||
| 774 | * @param bool $notNullable |
||
| 775 | * @param bool $cascadeDelete |
||
| 776 | * |
||
| 777 | * @return Closure |
||
| 778 | */ |
||
| 779 | 1 | private function relationshipImpl(string $name, bool $notNullable, bool $cascadeDelete): Closure |
|
| 822 | } |
||
| 823 |