Complex classes like DefaultControllerMethodsTrait 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 DefaultControllerMethodsTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php namespace Limoncello\Flute\Http\Traits; |
||
| 57 | trait DefaultControllerMethodsTrait |
||
| 58 | { |
||
| 59 | /** @noinspection PhpTooManyParametersInspection |
||
| 60 | * @param array $queryParams |
||
| 61 | * @param UriInterface $requestUri |
||
| 62 | * @param JsonApiQueryValidatingParserInterface $queryParser |
||
| 63 | * @param ParametersMapperInterface $mapper |
||
| 64 | * @param CrudInterface $crud |
||
| 65 | * @param SettingsProviderInterface $provider |
||
| 66 | * @param JsonSchemesInterface $jsonSchemes |
||
| 67 | * @param EncoderInterface $encoder |
||
| 68 | * |
||
| 69 | * @return ResponseInterface |
||
| 70 | */ |
||
| 71 | 12 | protected static function defaultIndexHandler( |
|
| 92 | |||
| 93 | /** @noinspection PhpTooManyParametersInspection |
||
| 94 | * @param string $index |
||
| 95 | * @param array $queryParams |
||
| 96 | * @param UriInterface $requestUri |
||
| 97 | * @param JsonApiQueryValidatingParserInterface $queryParser |
||
| 98 | * @param ParametersMapperInterface $mapper |
||
| 99 | * @param CrudInterface $crud |
||
| 100 | * @param SettingsProviderInterface $provider |
||
| 101 | * @param JsonSchemesInterface $jsonSchemes |
||
| 102 | * @param EncoderInterface $encoder |
||
| 103 | * |
||
| 104 | * @return ResponseInterface |
||
| 105 | */ |
||
| 106 | 1 | protected static function defaultReadHandler( |
|
| 129 | |||
| 130 | /** @noinspection PhpTooManyParametersInspection |
||
| 131 | * @param Closure $apiHandler |
||
| 132 | * @param array $queryParams |
||
| 133 | * @param UriInterface $requestUri |
||
| 134 | * @param JsonApiQueryValidatingParserInterface $queryParser |
||
| 135 | * @param ParametersMapperInterface $mapper |
||
| 136 | * @param CrudInterface $crud |
||
| 137 | * @param SettingsProviderInterface $provider |
||
| 138 | * @param JsonSchemesInterface $jsonSchemes |
||
| 139 | * @param EncoderInterface $encoder |
||
| 140 | * |
||
| 141 | * @return ResponseInterface |
||
| 142 | */ |
||
| 143 | 2 | protected static function defaultReadRelationshipWithClosureHandler( |
|
| 167 | |||
| 168 | /** @noinspection PhpTooManyParametersInspection |
||
| 169 | * @param Closure $apiHandler |
||
| 170 | * @param array $queryParams |
||
| 171 | * @param UriInterface $requestUri |
||
| 172 | * @param JsonApiQueryValidatingParserInterface $queryParser |
||
| 173 | * @param ParametersMapperInterface $mapper |
||
| 174 | * @param CrudInterface $crud |
||
| 175 | * @param SettingsProviderInterface $provider |
||
| 176 | * @param JsonSchemesInterface $jsonSchemes |
||
| 177 | * @param EncoderInterface $encoder |
||
| 178 | * |
||
| 179 | * @return ResponseInterface |
||
| 180 | */ |
||
| 181 | 1 | protected static function defaultReadRelationshipIdentifiersWithClosureHandler( |
|
| 205 | |||
| 206 | /** @noinspection PhpTooManyParametersInspection |
||
| 207 | * @param UriInterface $requestUri |
||
| 208 | * @param string $requestBody |
||
| 209 | * @param string $schemeClass |
||
| 210 | * @param ModelSchemeInfoInterface $schemeInfo |
||
| 211 | * @param JsonApiDataValidatingParserInterface $validator |
||
| 212 | * @param CrudInterface $crud |
||
| 213 | * @param SettingsProviderInterface $provider |
||
| 214 | * @param JsonSchemesInterface $jsonSchemes |
||
| 215 | * @param EncoderInterface $encoder |
||
| 216 | * @param FactoryInterface $errorFactory |
||
| 217 | * @param FormatterFactoryInterface $formatterFactory |
||
| 218 | * |
||
| 219 | * @return ResponseInterface |
||
| 220 | * |
||
| 221 | * @SuppressWarnings(PHPMD.ExcessiveParameterList) |
||
| 222 | */ |
||
| 223 | 1 | protected static function defaultCreateHandler( |
|
| 250 | |||
| 251 | /** |
||
| 252 | * @param string $requestBody |
||
| 253 | * @param string $schemeClass |
||
| 254 | * @param ModelSchemeInfoInterface $schemeInfo |
||
| 255 | * @param JsonApiDataValidatingParserInterface $validator |
||
| 256 | * @param CrudInterface $crud |
||
| 257 | * @param FactoryInterface $errorFactory |
||
| 258 | * @param FormatterFactoryInterface $formatterFactory |
||
| 259 | * |
||
| 260 | * @return ResponseInterface |
||
|
|
|||
| 261 | */ |
||
| 262 | 1 | protected static function defaultCreate( |
|
| 280 | |||
| 281 | /** |
||
| 282 | * @param string $index |
||
| 283 | * @param UriInterface $requestUri |
||
| 284 | * @param CrudInterface $crud |
||
| 285 | * @param SettingsProviderInterface $provider |
||
| 286 | * @param JsonSchemesInterface $jsonSchemes |
||
| 287 | * @param EncoderInterface $encoder |
||
| 288 | * |
||
| 289 | * @return ResponseInterface |
||
| 290 | */ |
||
| 291 | 1 | protected static function defaultCreateResponse( |
|
| 308 | |||
| 309 | /** @noinspection PhpTooManyParametersInspection |
||
| 310 | * @param string $index |
||
| 311 | * @param UriInterface $requestUri |
||
| 312 | * @param string $requestBody |
||
| 313 | * @param string $schemeClass |
||
| 314 | * @param ModelSchemeInfoInterface $schemeInfo |
||
| 315 | * @param JsonApiDataValidatingParserInterface $validator |
||
| 316 | * @param CrudInterface $crud |
||
| 317 | * @param SettingsProviderInterface $provider |
||
| 318 | * @param JsonSchemesInterface $jsonSchemes |
||
| 319 | * @param EncoderInterface $encoder |
||
| 320 | * @param FactoryInterface $errorFactory |
||
| 321 | * @param FormatterFactoryInterface $formatterFactory |
||
| 322 | * @param string $messagesNamespace |
||
| 323 | * @param string $errorMessage |
||
| 324 | * |
||
| 325 | * @return ResponseInterface |
||
| 326 | * |
||
| 327 | * @SuppressWarnings(PHPMD.ExcessiveParameterList) |
||
| 328 | */ |
||
| 329 | 5 | protected static function defaultUpdateHandler( |
|
| 362 | |||
| 363 | /** @noinspection PhpTooManyParametersInspection |
||
| 364 | * @param string $index |
||
| 365 | * @param string $requestBody |
||
| 366 | * @param string $schemeClass |
||
| 367 | * @param ModelSchemeInfoInterface $schemeInfo |
||
| 368 | * @param JsonApiDataValidatingParserInterface $validator |
||
| 369 | * @param CrudInterface $crud |
||
| 370 | * @param FactoryInterface $errorFactory |
||
| 371 | * @param FormatterFactoryInterface $formatterFactory |
||
| 372 | * @param string $messagesNamespace |
||
| 373 | * @param string $errorMessage |
||
| 374 | * |
||
| 375 | * @return int |
||
| 376 | * |
||
| 377 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
| 378 | * @SuppressWarnings(PHPMD.ExcessiveParameterList) |
||
| 379 | */ |
||
| 380 | 5 | protected static function defaultUpdate( |
|
| 416 | |||
| 417 | /** |
||
| 418 | * @param int $updated |
||
| 419 | * @param string $index |
||
| 420 | * @param UriInterface $requestUri |
||
| 421 | * @param CrudInterface $crud |
||
| 422 | * @param SettingsProviderInterface $provider |
||
| 423 | * @param JsonSchemesInterface $jsonSchemes |
||
| 424 | * @param EncoderInterface $encoder |
||
| 425 | * |
||
| 426 | * @return ResponseInterface |
||
| 427 | * |
||
| 428 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
| 429 | */ |
||
| 430 | 2 | protected static function defaultUpdateResponse( |
|
| 451 | |||
| 452 | /** @noinspection PhpTooManyParametersInspection |
||
| 453 | * @param string $parentIndex |
||
| 454 | * @param string $modelRelName |
||
| 455 | * @param string $childIndex |
||
| 456 | * @param UriInterface $requestUri |
||
| 457 | * @param string $requestBody |
||
| 458 | * @param string $childSchemeClass |
||
| 459 | * @param ModelSchemeInfoInterface $schemeInfo |
||
| 460 | * @param JsonApiDataValidatingParserInterface $childValidator |
||
| 461 | * @param CrudInterface $parentCrud |
||
| 462 | * @param CrudInterface $childCrud |
||
| 463 | * @param SettingsProviderInterface $provider |
||
| 464 | * @param JsonSchemesInterface $jsonSchemes |
||
| 465 | * @param EncoderInterface $encoder |
||
| 466 | * @param FactoryInterface $errorFactory |
||
| 467 | * @param FormatterFactoryInterface $formatterFactory |
||
| 468 | * @param string $messagesNamespace |
||
| 469 | * @param string $errorMessage |
||
| 470 | * |
||
| 471 | * @return ResponseInterface |
||
| 472 | * |
||
| 473 | * @SuppressWarnings(PHPMD.ExcessiveParameterList) |
||
| 474 | */ |
||
| 475 | 2 | protected static function defaultUpdateInRelationshipHandler( |
|
| 518 | |||
| 519 | /** |
||
| 520 | * @param string $index |
||
| 521 | * @param UriInterface $requestUri |
||
| 522 | * @param CrudInterface $crud |
||
| 523 | * @param SettingsProviderInterface $provider |
||
| 524 | * @param JsonSchemesInterface $jsonSchemes |
||
| 525 | * @param EncoderInterface $encoder |
||
| 526 | * |
||
| 527 | * @return ResponseInterface |
||
| 528 | */ |
||
| 529 | 1 | protected static function defaultDeleteHandler( |
|
| 545 | |||
| 546 | /** @noinspection PhpTooManyParametersInspection |
||
| 547 | * @param string $parentIndex |
||
| 548 | * @param string $modelRelName |
||
| 549 | * @param string $childIndex |
||
| 550 | * @param UriInterface $requestUri |
||
| 551 | * @param CrudInterface $parentCrud |
||
| 552 | * @param CrudInterface $childCrud |
||
| 553 | * @param SettingsProviderInterface $provider |
||
| 554 | * @param JsonSchemesInterface $jsonSchemes |
||
| 555 | * @param EncoderInterface $encoder |
||
| 556 | * |
||
| 557 | * @return ResponseInterface |
||
| 558 | */ |
||
| 559 | 1 | protected static function defaultDeleteInRelationshipHandler( |
|
| 586 | |||
| 587 | /** |
||
| 588 | * @param ContainerInterface $container |
||
| 589 | * @param string $rulesClass |
||
| 590 | * |
||
| 591 | * @return JsonApiQueryValidatingParserInterface |
||
| 592 | * @throws ContainerExceptionInterface |
||
| 593 | * @throws NotFoundExceptionInterface |
||
| 594 | */ |
||
| 595 | 16 | protected static function defaultCreateQueryParser( |
|
| 607 | |||
| 608 | /** |
||
| 609 | * @param ContainerInterface $container |
||
| 610 | * @param string $rulesClass |
||
| 611 | * |
||
| 612 | * @return JsonApiDataValidatingParserInterface |
||
| 613 | * |
||
| 614 | * @throws ContainerExceptionInterface |
||
| 615 | * @throws NotFoundExceptionInterface |
||
| 616 | */ |
||
| 617 | 8 | protected static function defaultCreateDataParser( |
|
| 629 | |||
| 630 | /** |
||
| 631 | * @param ContainerInterface $container |
||
| 632 | * @param string $schemaClass |
||
| 633 | * |
||
| 634 | * @return ParametersMapperInterface |
||
| 635 | * @throws ContainerExceptionInterface |
||
| 636 | * @throws NotFoundExceptionInterface |
||
| 637 | */ |
||
| 638 | 16 | protected static function defaultCreateParameterMapper( |
|
| 653 | |||
| 654 | /** |
||
| 655 | * @param JsonApiQueryValidatingParserInterface $queryParser |
||
| 656 | * |
||
| 657 | * @return EncodingParametersInterface |
||
| 658 | */ |
||
| 659 | protected static function defaultCreateEncodingParameters( |
||
| 667 | |||
| 668 | /** |
||
| 669 | * @param ContainerInterface $container |
||
| 670 | * @param string|null $class |
||
| 671 | * |
||
| 672 | * @return CrudInterface |
||
| 673 | * |
||
| 674 | * @throws ContainerExceptionInterface |
||
| 675 | * @throws NotFoundExceptionInterface |
||
| 676 | */ |
||
| 677 | 26 | protected static function defaultCreateApi(ContainerInterface $container, string $class): CrudInterface |
|
| 687 | |||
| 688 | /** |
||
| 689 | * @param UriInterface $requestUri |
||
| 690 | * @param SettingsProviderInterface $provider |
||
| 691 | * @param JsonSchemesInterface $jsonSchemes |
||
| 692 | * @param EncoderInterface $encoder |
||
| 693 | * @param EncodingParametersInterface|null $parameters |
||
| 694 | * |
||
| 695 | * @return ResponsesInterface |
||
| 696 | */ |
||
| 697 | 4 | protected static function defaultCreateResponses( |
|
| 717 | |||
| 718 | /** |
||
| 719 | * Developers can override the method in order to add/remove some data for `create`/`update` inputs. |
||
| 720 | * |
||
| 721 | * @param string $requestBody |
||
| 722 | * @param FactoryInterface $errorFactory |
||
| 723 | * @param FormatterFactoryInterface $formatterFactory |
||
| 724 | * @param string $messagesNamespace |
||
| 725 | * @param string $errorMessage |
||
| 726 | * |
||
| 727 | * @return array |
||
| 728 | */ |
||
| 729 | 6 | protected static function readJsonFromRequest( |
|
| 746 | |||
| 747 | /** |
||
| 748 | * Developers can override the method in order to use custom data mapping from a Schema to Model. |
||
| 749 | * |
||
| 750 | * @param array $captures |
||
| 751 | * @param string $schemeClass |
||
| 752 | * @param ModelSchemeInfoInterface $schemeInfo |
||
| 753 | * |
||
| 754 | * @return array |
||
| 755 | */ |
||
| 756 | 3 | protected static function mapSchemeDataToModelData( |
|
| 793 | |||
| 794 | /** |
||
| 795 | * @param null|string $value |
||
| 796 | * |
||
| 797 | * @return void |
||
| 798 | */ |
||
| 799 | 26 | private static function assertClassValueDefined(?string $value): void |
|
| 803 | |||
| 804 | /** |
||
| 805 | * @param string $class |
||
| 806 | * @param string $interface |
||
| 807 | * |
||
| 808 | * @return void |
||
| 809 | */ |
||
| 810 | 26 | private static function assertClassImplements(string $class, string $interface): void |
|
| 817 | } |
||
| 818 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.