Complex classes like MakeCommand 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 MakeCommand, and based on these observations, apply Extract Interface, too.
| 1 | <?php namespace Limoncello\Application\Commands; |
||
| 34 | class MakeCommand implements CommandInterface |
||
| 35 | { |
||
| 36 | /** |
||
| 37 | * Command name. |
||
| 38 | */ |
||
| 39 | const NAME = 'l:make'; |
||
| 40 | |||
| 41 | /** Argument name */ |
||
| 42 | const ARG_ITEM = 'item'; |
||
| 43 | |||
| 44 | /** Argument name */ |
||
| 45 | const ARG_SINGULAR = 'singular'; |
||
| 46 | |||
| 47 | /** Argument name */ |
||
| 48 | const ARG_PLURAL = 'plural'; |
||
| 49 | |||
| 50 | /** Command action */ |
||
| 51 | const ITEM_MIGRATE = 'migrate'; |
||
| 52 | |||
| 53 | /** Command action */ |
||
| 54 | const ITEM_SEED = 'seed'; |
||
| 55 | |||
| 56 | /** Command action */ |
||
| 57 | const ITEM_CONTROLLER = 'controller'; |
||
| 58 | |||
| 59 | /** Command action */ |
||
| 60 | const ITEM_JSONAPI = 'jsonapi'; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Taken from http://php.net/manual/en/language.oop5.basic.php |
||
| 64 | */ |
||
| 65 | protected const VALID_CLASS_NAME_REGEX = '/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/'; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @inheritdoc |
||
| 69 | */ |
||
| 70 | 1 | public static function getName(): string |
|
| 74 | |||
| 75 | /** |
||
| 76 | * @inheritdoc |
||
| 77 | */ |
||
| 78 | 1 | public static function getDescription(): string |
|
| 82 | |||
| 83 | /** |
||
| 84 | * @inheritdoc |
||
| 85 | */ |
||
| 86 | 1 | public static function getHelp(): string |
|
| 90 | |||
| 91 | /** |
||
| 92 | * @inheritdoc |
||
| 93 | */ |
||
| 94 | 1 | public static function getArguments(): array |
|
| 118 | |||
| 119 | /** |
||
| 120 | * @inheritdoc |
||
| 121 | */ |
||
| 122 | 1 | public static function getOptions(): array |
|
| 126 | |||
| 127 | /** |
||
| 128 | * @inheritdoc |
||
| 129 | */ |
||
| 130 | 7 | public static function execute(ContainerInterface $container, IoInterface $inOut): void |
|
| 134 | |||
| 135 | /** |
||
| 136 | * @param ContainerInterface $container |
||
| 137 | * @param IoInterface $inOut |
||
| 138 | * |
||
| 139 | * @return void |
||
| 140 | * |
||
| 141 | * @throws ContainerExceptionInterface |
||
| 142 | * @throws NotFoundExceptionInterface |
||
| 143 | */ |
||
| 144 | 7 | protected function run(ContainerInterface $container, IoInterface $inOut): void |
|
| 194 | |||
| 195 | /** |
||
| 196 | * @param ContainerInterface $container |
||
| 197 | * @param string $singular |
||
| 198 | * @param string $plural |
||
| 199 | * |
||
| 200 | * @return array |
||
| 201 | * @throws ContainerExceptionInterface |
||
| 202 | * @throws NotFoundExceptionInterface |
||
| 203 | */ |
||
| 204 | 2 | private function composeMigrationParameters(ContainerInterface $container, string $singular, string $plural): array |
|
| 215 | |||
| 216 | /** |
||
| 217 | * @param ContainerInterface $container |
||
| 218 | * @param string $singular |
||
| 219 | * @param string $plural |
||
| 220 | * |
||
| 221 | * @return array |
||
| 222 | * @throws ContainerExceptionInterface |
||
| 223 | * @throws NotFoundExceptionInterface |
||
| 224 | */ |
||
| 225 | 2 | private function composeSeedParameters(ContainerInterface $container, string $singular, string $plural): array |
|
| 236 | |||
| 237 | /** |
||
| 238 | * @param ContainerInterface $container |
||
| 239 | * @param string $singular |
||
| 240 | * @param string $plural |
||
| 241 | * |
||
| 242 | * @return array |
||
| 243 | * @throws ContainerExceptionInterface |
||
| 244 | * @throws NotFoundExceptionInterface |
||
| 245 | */ |
||
| 246 | 1 | private function composeModelParameters(ContainerInterface $container, string $singular, string $plural): array |
|
| 260 | |||
| 261 | /** |
||
| 262 | * @param ContainerInterface $container |
||
| 263 | * @param string $singular |
||
| 264 | * @param string $plural |
||
| 265 | * |
||
| 266 | * @return array |
||
| 267 | * @throws ContainerExceptionInterface |
||
| 268 | * @throws NotFoundExceptionInterface |
||
| 269 | */ |
||
| 270 | 1 | private function composeSchemaParameters(ContainerInterface $container, string $singular, string $plural): array |
|
| 281 | |||
| 282 | /** |
||
| 283 | * @param ContainerInterface $container |
||
| 284 | * @param string $singular |
||
| 285 | * @param string $plural |
||
| 286 | * |
||
| 287 | * @return array |
||
| 288 | * @throws ContainerExceptionInterface |
||
| 289 | * @throws NotFoundExceptionInterface |
||
| 290 | */ |
||
| 291 | 1 | private function composeApiParameters(ContainerInterface $container, string $singular, string $plural): array |
|
| 304 | |||
| 305 | /** |
||
| 306 | * @param ContainerInterface $container |
||
| 307 | * @param string $singular |
||
| 308 | * @param string $plural |
||
| 309 | * |
||
| 310 | * @return array |
||
| 311 | * @throws ContainerExceptionInterface |
||
| 312 | * @throws NotFoundExceptionInterface |
||
| 313 | */ |
||
| 314 | 1 | private function composeAuthorizationParameters( |
|
| 331 | |||
| 332 | /** |
||
| 333 | * @param ContainerInterface $container |
||
| 334 | * @param string $singular |
||
| 335 | * @param string $plural |
||
| 336 | * |
||
| 337 | * @return array |
||
| 338 | * @throws ContainerExceptionInterface |
||
| 339 | * @throws NotFoundExceptionInterface |
||
| 340 | */ |
||
| 341 | 1 | private function composeValidationRulesParameters( |
|
| 356 | |||
| 357 | /** |
||
| 358 | * @param ContainerInterface $container |
||
| 359 | * @param string $singular |
||
| 360 | * @param string $plural |
||
| 361 | * |
||
| 362 | * @return array |
||
| 363 | * @throws ContainerExceptionInterface |
||
| 364 | * @throws NotFoundExceptionInterface |
||
| 365 | */ |
||
| 366 | 1 | private function composeValidationOnCreateRuleSetsParameters( |
|
| 382 | |||
| 383 | /** |
||
| 384 | * @param ContainerInterface $container |
||
| 385 | * @param string $singular |
||
| 386 | * @param string $plural |
||
| 387 | * |
||
| 388 | * @return array |
||
| 389 | * @throws ContainerExceptionInterface |
||
| 390 | * @throws NotFoundExceptionInterface |
||
| 391 | */ |
||
| 392 | 1 | private function composeValidationOnUpdateRuleSetsParameters( |
|
| 408 | |||
| 409 | /** |
||
| 410 | * @param ContainerInterface $container |
||
| 411 | * @param string $singular |
||
| 412 | * @param string $plural |
||
| 413 | * |
||
| 414 | * @return array |
||
| 415 | * @throws ContainerExceptionInterface |
||
| 416 | * @throws NotFoundExceptionInterface |
||
| 417 | */ |
||
| 418 | 1 | private function composeJsonControllerParameters( |
|
| 434 | |||
| 435 | /** |
||
| 436 | * @param ContainerInterface $container |
||
| 437 | * @param string $singular |
||
| 438 | * @param string $plural |
||
| 439 | * |
||
| 440 | * @return array |
||
| 441 | * @throws ContainerExceptionInterface |
||
| 442 | * @throws NotFoundExceptionInterface |
||
| 443 | */ |
||
| 444 | 1 | private function composeJsonRouteParameters( |
|
| 460 | |||
| 461 | /** |
||
| 462 | * @param ContainerInterface $container |
||
| 463 | * @param string $singular |
||
| 464 | * @param string $plural |
||
| 465 | * |
||
| 466 | * @return array |
||
| 467 | * @throws ContainerExceptionInterface |
||
| 468 | * @throws NotFoundExceptionInterface |
||
| 469 | */ |
||
| 470 | 1 | private function composeWebControllerParameters( |
|
| 487 | |||
| 488 | /** |
||
| 489 | * @param ContainerInterface $container |
||
| 490 | * @param string $singular |
||
| 491 | * @param string $plural |
||
| 492 | * |
||
| 493 | * @return array |
||
| 494 | * @throws ContainerExceptionInterface |
||
| 495 | * @throws NotFoundExceptionInterface |
||
| 496 | */ |
||
| 497 | 1 | private function composeWebRouteParameters( |
|
| 513 | |||
| 514 | /** |
||
| 515 | * @param FileSystemInterface $fileSystem |
||
| 516 | * @param array $pathsAndParams |
||
| 517 | * |
||
| 518 | * @return void |
||
| 519 | */ |
||
| 520 | 4 | private function createTemplates(FileSystemInterface $fileSystem, array $pathsAndParams): void |
|
| 532 | |||
| 533 | /** |
||
| 534 | * @param FileSystemInterface $fileSystem |
||
| 535 | * @param string $outputPath |
||
| 536 | * @param string $templatePath |
||
| 537 | * @param iterable $parameters |
||
| 538 | * |
||
| 539 | * @return void |
||
| 540 | */ |
||
| 541 | 4 | private function writeByTemplate( |
|
| 551 | |||
| 552 | /** |
||
| 553 | * @param string $template |
||
| 554 | * @param iterable $parameters |
||
| 555 | * |
||
| 556 | * @return string |
||
| 557 | */ |
||
| 558 | 4 | private function replaceInTemplate(string $template, iterable $parameters): string |
|
| 567 | |||
| 568 | /** |
||
| 569 | * @param ContainerInterface $container |
||
| 570 | * |
||
| 571 | * @return FileSystemInterface |
||
| 572 | * |
||
| 573 | * @throws ContainerExceptionInterface |
||
| 574 | * @throws NotFoundExceptionInterface |
||
| 575 | */ |
||
| 576 | 4 | private function getFileSystem(ContainerInterface $container): FileSystemInterface |
|
| 585 | |||
| 586 | /** |
||
| 587 | * @param ContainerInterface $container |
||
| 588 | * |
||
| 589 | * @return SettingsProviderInterface |
||
| 590 | * @throws ContainerExceptionInterface |
||
| 591 | * @throws NotFoundExceptionInterface |
||
| 592 | */ |
||
| 593 | 4 | private function getSettingsProvider(ContainerInterface $container): SettingsProviderInterface |
|
| 602 | |||
| 603 | /** |
||
| 604 | * @param string $name |
||
| 605 | * |
||
| 606 | * @return bool |
||
| 607 | */ |
||
| 608 | 7 | private function isValidShortClassName(string $name): bool |
|
| 612 | |||
| 613 | /** |
||
| 614 | * @param string $fileName |
||
| 615 | * |
||
| 616 | * @return string |
||
| 617 | */ |
||
| 618 | 4 | private function getTemplatePath(string $fileName): string |
|
| 622 | |||
| 623 | /** |
||
| 624 | * @param ContainerInterface $container |
||
| 625 | * |
||
| 626 | * @return array |
||
| 627 | * |
||
| 628 | * @throws ContainerExceptionInterface |
||
| 629 | * @throws NotFoundExceptionInterface |
||
| 630 | */ |
||
| 631 | 3 | private function getDataSettings(ContainerInterface $container): array |
|
| 637 | |||
| 638 | /** |
||
| 639 | * @param ContainerInterface $container |
||
| 640 | * |
||
| 641 | * @return array |
||
| 642 | * |
||
| 643 | * @throws ContainerExceptionInterface |
||
| 644 | * @throws NotFoundExceptionInterface |
||
| 645 | */ |
||
| 646 | 2 | private function getFluteSettings(ContainerInterface $container): array |
|
| 652 | |||
| 653 | /** |
||
| 654 | * @param ContainerInterface $container |
||
| 655 | * |
||
| 656 | * @return array |
||
| 657 | * |
||
| 658 | * @throws ContainerExceptionInterface |
||
| 659 | * @throws NotFoundExceptionInterface |
||
| 660 | */ |
||
| 661 | 1 | private function getAuthorizationSettings(ContainerInterface $container): array |
|
| 667 | |||
| 668 | /** |
||
| 669 | * Folder paths might include masks such as `**`. This function tries to filter them out. |
||
| 670 | * |
||
| 671 | * @param string $folder |
||
| 672 | * |
||
| 673 | * @return string |
||
| 674 | */ |
||
| 675 | 2 | private function filterOutFolderMask(string $folder): string |
|
| 684 | } |
||
| 685 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.