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 unsignedInt(string $name, int $default = null): Closure |
|
| 260 | |||
| 261 | /** |
||
| 262 | * @param string $name |
||
| 263 | * @param null|int $default |
||
| 264 | * |
||
| 265 | * @return Closure |
||
| 266 | */ |
||
| 267 | 1 | protected function nullableUnsignedInt(string $name, int $default = null): Closure |
|
| 271 | |||
| 272 | /** |
||
| 273 | * @param string $name |
||
| 274 | * |
||
| 275 | * @return Closure |
||
| 276 | */ |
||
| 277 | 1 | protected function float(string $name): Closure |
|
| 285 | |||
| 286 | /** |
||
| 287 | * @param string $name |
||
| 288 | * |
||
| 289 | * @return Closure |
||
| 290 | */ |
||
| 291 | 1 | protected function string(string $name): Closure |
|
| 298 | |||
| 299 | /** |
||
| 300 | * @param string $name |
||
| 301 | * |
||
| 302 | * @return Closure |
||
| 303 | */ |
||
| 304 | 1 | protected function nullableString(string $name): Closure |
|
| 311 | |||
| 312 | /** |
||
| 313 | * @param string $name |
||
| 314 | * |
||
| 315 | * @return Closure |
||
| 316 | */ |
||
| 317 | 1 | protected function text(string $name): Closure |
|
| 323 | |||
| 324 | /** |
||
| 325 | * @param string $name |
||
| 326 | * |
||
| 327 | * @return Closure |
||
| 328 | */ |
||
| 329 | 1 | protected function nullableText(string $name): Closure |
|
| 335 | |||
| 336 | /** |
||
| 337 | * @param string $name |
||
| 338 | * @param null|bool $default |
||
| 339 | * |
||
| 340 | * @return Closure |
||
| 341 | */ |
||
| 342 | 1 | protected function bool(string $name, $default = null): Closure |
|
| 351 | |||
| 352 | /** |
||
| 353 | * @param string $name |
||
| 354 | * @param array $values |
||
| 355 | * |
||
| 356 | * @return Closure |
||
| 357 | * |
||
| 358 | * @throws DBALException |
||
| 359 | */ |
||
| 360 | 1 | protected function enum(string $name, array $values): Closure |
|
| 366 | |||
| 367 | /** |
||
| 368 | * @param string $name |
||
| 369 | * @param array $values |
||
| 370 | * |
||
| 371 | * @return Closure |
||
| 372 | * |
||
| 373 | * @throws DBALException |
||
| 374 | */ |
||
| 375 | 1 | protected function nullableEnum(string $name, array $values): Closure |
|
| 381 | |||
| 382 | /** |
||
| 383 | * @return Closure |
||
| 384 | */ |
||
| 385 | 1 | protected function timestamps(): Closure |
|
| 411 | |||
| 412 | /** |
||
| 413 | * @param string $name |
||
| 414 | * |
||
| 415 | * @return Closure |
||
| 416 | */ |
||
| 417 | 1 | protected function datetime(string $name): Closure |
|
| 423 | |||
| 424 | /** |
||
| 425 | * @param string $name |
||
| 426 | * |
||
| 427 | * @return Closure |
||
| 428 | */ |
||
| 429 | 1 | protected function nullableDatetime(string $name): Closure |
|
| 435 | |||
| 436 | /** |
||
| 437 | * @param string $name |
||
| 438 | * |
||
| 439 | * @return Closure |
||
| 440 | */ |
||
| 441 | 1 | protected function date(string $name): Closure |
|
| 447 | |||
| 448 | /** |
||
| 449 | * @param string $name |
||
| 450 | * |
||
| 451 | * @return Closure |
||
| 452 | */ |
||
| 453 | 1 | protected function nullableDate(string $name): Closure |
|
| 459 | |||
| 460 | /** |
||
| 461 | * @param string[] $names |
||
| 462 | * |
||
| 463 | * @return Closure |
||
| 464 | */ |
||
| 465 | 1 | protected function unique(array $names): Closure |
|
| 471 | |||
| 472 | /** |
||
| 473 | * @param string[] $names |
||
| 474 | * |
||
| 475 | * @return Closure |
||
| 476 | */ |
||
| 477 | 1 | protected function searchable(array $names): Closure |
|
| 483 | |||
| 484 | /** |
||
| 485 | * @param string $column |
||
| 486 | * @param string $referredClass |
||
| 487 | * @param bool $cascadeDelete |
||
| 488 | * |
||
| 489 | * @return Closure |
||
| 490 | * |
||
| 491 | * @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
||
| 492 | */ |
||
| 493 | 1 | protected function foreignRelationship( |
|
| 517 | |||
| 518 | /** |
||
| 519 | * @param string $column |
||
| 520 | * @param string $referredClass |
||
| 521 | * @param bool $cascadeDelete |
||
| 522 | * |
||
| 523 | * @return Closure |
||
| 524 | * |
||
| 525 | * @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
||
| 526 | */ |
||
| 527 | 1 | protected function nullableForeignRelationship( |
|
| 552 | |||
| 553 | /** @noinspection PhpTooManyParametersInspection |
||
| 554 | * @param string $localKey |
||
| 555 | * @param string $foreignTable |
||
| 556 | * @param string $foreignKey |
||
| 557 | * @param string $type |
||
| 558 | * @param int|null $length |
||
| 559 | * @param bool $cascadeDelete |
||
| 560 | * |
||
| 561 | * @return Closure |
||
| 562 | * |
||
| 563 | * @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
||
| 564 | */ |
||
| 565 | 1 | protected function foreignColumn( |
|
| 575 | |||
| 576 | /** @noinspection PhpTooManyParametersInspection |
||
| 577 | * @param string $localKey |
||
| 578 | * @param string $foreignTable |
||
| 579 | * @param string $foreignKey |
||
| 580 | * @param string $type |
||
| 581 | * @param int|null $length |
||
| 582 | * @param bool $cascadeDelete |
||
| 583 | * |
||
| 584 | * @return Closure |
||
| 585 | * |
||
| 586 | * @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
||
| 587 | */ |
||
| 588 | 1 | protected function nullableForeignColumn( |
|
| 598 | |||
| 599 | /** |
||
| 600 | * @param string $name |
||
| 601 | * @param bool $cascadeDelete |
||
| 602 | * |
||
| 603 | * @return Closure |
||
| 604 | * |
||
| 605 | * @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
||
| 606 | */ |
||
| 607 | 1 | protected function nullableRelationship(string $name, bool $cascadeDelete = false): Closure |
|
| 611 | |||
| 612 | /** |
||
| 613 | * @param string $name |
||
| 614 | * @param bool $cascadeDelete |
||
| 615 | * |
||
| 616 | * @return Closure |
||
| 617 | * |
||
| 618 | * @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
||
| 619 | */ |
||
| 620 | 1 | protected function relationship(string $name, bool $cascadeDelete = false): Closure |
|
| 624 | |||
| 625 | /** |
||
| 626 | * @param string $modelClass |
||
| 627 | * |
||
| 628 | * @return string |
||
| 629 | */ |
||
| 630 | 1 | protected function getTableNameForClass(string $modelClass): string |
|
| 641 | |||
| 642 | /** |
||
| 643 | * @param string $name |
||
| 644 | * @param bool $notNullable |
||
| 645 | * @param null|mixed $default |
||
| 646 | * |
||
| 647 | * @return Closure |
||
| 648 | */ |
||
| 649 | 1 | private function unsignedIntImpl(string $name, bool $notNullable, $default = null): Closure |
|
| 656 | |||
| 657 | /** @noinspection PhpTooManyParametersInspection |
||
| 658 | * @param string $localKey |
||
| 659 | * @param string $foreignTable |
||
| 660 | * @param string $foreignKey |
||
| 661 | * @param string $type |
||
| 662 | * @param int|null $length |
||
| 663 | * @param bool $notNullable |
||
| 664 | * @param bool $cascadeDelete |
||
| 665 | * |
||
| 666 | * @return Closure |
||
| 667 | */ |
||
| 668 | 1 | private function foreignColumnImpl( |
|
| 692 | |||
| 693 | /** |
||
| 694 | * @param string $name |
||
| 695 | * @param bool $notNullable |
||
| 696 | * @param bool $cascadeDelete |
||
| 697 | * |
||
| 698 | * @return Closure |
||
| 699 | */ |
||
| 700 | 1 | private function relationshipImpl(string $name, bool $notNullable, bool $cascadeDelete): Closure |
|
| 743 | } |
||
| 744 |