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; |
||
| 60 | trait DefaultControllerMethodsTrait |
||
| 61 | { |
||
| 62 | /** @noinspection PhpTooManyParametersInspection |
||
| 63 | * @param array $queryParams |
||
| 64 | * @param UriInterface $requestUri |
||
| 65 | * @param JsonApiQueryValidatingParserInterface $queryParser |
||
| 66 | * @param ParametersMapperInterface $mapper |
||
| 67 | * @param CrudInterface $crud |
||
| 68 | * @param SettingsProviderInterface $provider |
||
| 69 | * @param JsonSchemasInterface $jsonSchemas |
||
| 70 | * @param EncoderInterface $encoder |
||
| 71 | * |
||
| 72 | * @return ResponseInterface |
||
| 73 | */ |
||
| 74 | 12 | protected static function defaultIndexHandler( |
|
| 95 | |||
| 96 | /** @noinspection PhpTooManyParametersInspection |
||
| 97 | * @param string $index |
||
| 98 | * @param array $queryParams |
||
| 99 | * @param UriInterface $requestUri |
||
| 100 | * @param JsonApiQueryValidatingParserInterface $queryParser |
||
| 101 | * @param ParametersMapperInterface $mapper |
||
| 102 | * @param CrudInterface $crud |
||
| 103 | * @param SettingsProviderInterface $provider |
||
| 104 | * @param JsonSchemasInterface $jsonSchemas |
||
| 105 | * @param EncoderInterface $encoder |
||
| 106 | * |
||
| 107 | * @return ResponseInterface |
||
| 108 | */ |
||
| 109 | 1 | protected static function defaultReadHandler( |
|
| 132 | |||
| 133 | /** @noinspection PhpTooManyParametersInspection |
||
| 134 | * @param Closure $apiHandler |
||
| 135 | * @param array $queryParams |
||
| 136 | * @param UriInterface $requestUri |
||
| 137 | * @param JsonApiQueryValidatingParserInterface $queryParser |
||
| 138 | * @param ParametersMapperInterface $mapper |
||
| 139 | * @param CrudInterface $crud |
||
| 140 | * @param SettingsProviderInterface $provider |
||
| 141 | * @param JsonSchemasInterface $jsonSchemas |
||
| 142 | * @param EncoderInterface $encoder |
||
| 143 | * |
||
| 144 | * @return ResponseInterface |
||
| 145 | */ |
||
| 146 | 2 | protected static function defaultReadRelationshipWithClosureHandler( |
|
| 170 | |||
| 171 | /** @noinspection PhpTooManyParametersInspection |
||
| 172 | * @param Closure $apiHandler |
||
| 173 | * @param array $queryParams |
||
| 174 | * @param UriInterface $requestUri |
||
| 175 | * @param JsonApiQueryValidatingParserInterface $queryParser |
||
| 176 | * @param ParametersMapperInterface $mapper |
||
| 177 | * @param CrudInterface $crud |
||
| 178 | * @param SettingsProviderInterface $provider |
||
| 179 | * @param JsonSchemasInterface $jsonSchemas |
||
| 180 | * @param EncoderInterface $encoder |
||
| 181 | * |
||
| 182 | * @return ResponseInterface |
||
| 183 | */ |
||
| 184 | 1 | protected static function defaultReadRelationshipIdentifiersWithClosureHandler( |
|
| 208 | |||
| 209 | /** @noinspection PhpTooManyParametersInspection |
||
| 210 | * @param UriInterface $requestUri |
||
| 211 | * @param string $requestBody |
||
| 212 | * @param string $schemaClass |
||
| 213 | * @param ModelSchemaInfoInterface $schemaInfo |
||
| 214 | * @param JsonApiDataValidatingParserInterface $validator |
||
| 215 | * @param CrudInterface $crud |
||
| 216 | * @param SettingsProviderInterface $provider |
||
| 217 | * @param JsonSchemasInterface $jsonSchemas |
||
| 218 | * @param EncoderInterface $encoder |
||
| 219 | * @param FactoryInterface $errorFactory |
||
| 220 | * @param FormatterFactoryInterface $formatterFactory |
||
| 221 | * |
||
| 222 | * @return ResponseInterface |
||
| 223 | * |
||
| 224 | * @SuppressWarnings(PHPMD.ExcessiveParameterList) |
||
| 225 | */ |
||
| 226 | 1 | protected static function defaultCreateHandler( |
|
| 253 | |||
| 254 | /** |
||
| 255 | * @param string $requestBody |
||
| 256 | * @param string $schemaClass |
||
| 257 | * @param ModelSchemaInfoInterface $schemaInfo |
||
| 258 | * @param JsonApiDataValidatingParserInterface $validator |
||
| 259 | * @param CrudInterface $crud |
||
| 260 | * @param FactoryInterface $errorFactory |
||
| 261 | * @param FormatterFactoryInterface $formatterFactory |
||
| 262 | * |
||
| 263 | * @return ResponseInterface |
||
|
|
|||
| 264 | */ |
||
| 265 | 1 | protected static function defaultCreate( |
|
| 283 | |||
| 284 | /** |
||
| 285 | * @param string $index |
||
| 286 | * @param UriInterface $requestUri |
||
| 287 | * @param CrudInterface $crud |
||
| 288 | * @param SettingsProviderInterface $provider |
||
| 289 | * @param JsonSchemasInterface $jsonSchemas |
||
| 290 | * @param EncoderInterface $encoder |
||
| 291 | * |
||
| 292 | * @return ResponseInterface |
||
| 293 | */ |
||
| 294 | 1 | protected static function defaultCreateResponse( |
|
| 311 | |||
| 312 | /** @noinspection PhpTooManyParametersInspection |
||
| 313 | * @param string $index |
||
| 314 | * @param UriInterface $requestUri |
||
| 315 | * @param string $requestBody |
||
| 316 | * @param string $schemaClass |
||
| 317 | * @param ModelSchemaInfoInterface $schemaInfo |
||
| 318 | * @param JsonApiDataValidatingParserInterface $validator |
||
| 319 | * @param CrudInterface $crud |
||
| 320 | * @param SettingsProviderInterface $provider |
||
| 321 | * @param JsonSchemasInterface $jsonSchemas |
||
| 322 | * @param EncoderInterface $encoder |
||
| 323 | * @param FactoryInterface $errorFactory |
||
| 324 | * @param FormatterFactoryInterface $formatterFactory |
||
| 325 | * @param string $messagesNamespace |
||
| 326 | * @param string $errorMessage |
||
| 327 | * |
||
| 328 | * @return ResponseInterface |
||
| 329 | * |
||
| 330 | * @SuppressWarnings(PHPMD.ExcessiveParameterList) |
||
| 331 | */ |
||
| 332 | 6 | protected static function defaultUpdateHandler( |
|
| 365 | |||
| 366 | /** @noinspection PhpTooManyParametersInspection |
||
| 367 | * @param string $index |
||
| 368 | * @param string $requestBody |
||
| 369 | * @param string $schemaClass |
||
| 370 | * @param ModelSchemaInfoInterface $schemaInfo |
||
| 371 | * @param JsonApiDataValidatingParserInterface $validator |
||
| 372 | * @param CrudInterface $crud |
||
| 373 | * @param FactoryInterface $errorFactory |
||
| 374 | * @param FormatterFactoryInterface $formatterFactory |
||
| 375 | * @param string $messagesNamespace |
||
| 376 | * @param string $errorMessage |
||
| 377 | * |
||
| 378 | * @return int |
||
| 379 | * |
||
| 380 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
| 381 | * @SuppressWarnings(PHPMD.ExcessiveParameterList) |
||
| 382 | */ |
||
| 383 | 6 | protected static function defaultUpdate( |
|
| 419 | |||
| 420 | /** |
||
| 421 | * @param int $updated |
||
| 422 | * @param string $index |
||
| 423 | * @param UriInterface $requestUri |
||
| 424 | * @param CrudInterface $crud |
||
| 425 | * @param SettingsProviderInterface $provider |
||
| 426 | * @param JsonSchemasInterface $jsonSchemas |
||
| 427 | * @param EncoderInterface $encoder |
||
| 428 | * |
||
| 429 | * @return ResponseInterface |
||
| 430 | * |
||
| 431 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
| 432 | */ |
||
| 433 | 3 | protected static function defaultUpdateResponse( |
|
| 453 | |||
| 454 | /** @noinspection PhpTooManyParametersInspection |
||
| 455 | * @param string $parentIndex |
||
| 456 | * @param string $modelRelName |
||
| 457 | * @param string $childIndex |
||
| 458 | * @param UriInterface $requestUri |
||
| 459 | * @param string $requestBody |
||
| 460 | * @param string $childSchemaClass |
||
| 461 | * @param ModelSchemaInfoInterface $schemaInfo |
||
| 462 | * @param JsonApiDataValidatingParserInterface $childValidator |
||
| 463 | * @param CrudInterface $parentCrud |
||
| 464 | * @param CrudInterface $childCrud |
||
| 465 | * @param SettingsProviderInterface $provider |
||
| 466 | * @param JsonSchemasInterface $jsonSchemas |
||
| 467 | * @param EncoderInterface $encoder |
||
| 468 | * @param FactoryInterface $errorFactory |
||
| 469 | * @param FormatterFactoryInterface $formatterFactory |
||
| 470 | * @param string $messagesNamespace |
||
| 471 | * @param string $errorMessage |
||
| 472 | * |
||
| 473 | * @return ResponseInterface |
||
| 474 | * |
||
| 475 | * @SuppressWarnings(PHPMD.ExcessiveParameterList) |
||
| 476 | */ |
||
| 477 | 2 | protected static function defaultUpdateInRelationshipHandler( |
|
| 520 | |||
| 521 | /** |
||
| 522 | * @param string $index |
||
| 523 | * @param UriInterface $requestUri |
||
| 524 | * @param CrudInterface $crud |
||
| 525 | * @param SettingsProviderInterface $provider |
||
| 526 | * @param JsonSchemasInterface $jsonSchemas |
||
| 527 | * @param EncoderInterface $encoder |
||
| 528 | * |
||
| 529 | * @return ResponseInterface |
||
| 530 | */ |
||
| 531 | 2 | protected static function defaultDeleteHandler( |
|
| 547 | |||
| 548 | /** @noinspection PhpTooManyParametersInspection |
||
| 549 | * @param string $parentIndex |
||
| 550 | * @param string $modelRelName |
||
| 551 | * @param string $childIndex |
||
| 552 | * @param UriInterface $requestUri |
||
| 553 | * @param CrudInterface $parentCrud |
||
| 554 | * @param CrudInterface $childCrud |
||
| 555 | * @param SettingsProviderInterface $provider |
||
| 556 | * @param JsonSchemasInterface $jsonSchemas |
||
| 557 | * @param EncoderInterface $encoder |
||
| 558 | * |
||
| 559 | * @return ResponseInterface |
||
| 560 | */ |
||
| 561 | 1 | protected static function defaultDeleteInRelationshipHandler( |
|
| 588 | |||
| 589 | /** |
||
| 590 | * @param ContainerInterface $container |
||
| 591 | * @param string $rulesClass |
||
| 592 | * |
||
| 593 | * @return JsonApiQueryValidatingParserInterface |
||
| 594 | * |
||
| 595 | * @throws ContainerExceptionInterface |
||
| 596 | * @throws NotFoundExceptionInterface |
||
| 597 | */ |
||
| 598 | 16 | protected static function defaultCreateQueryParser( |
|
| 610 | |||
| 611 | /** |
||
| 612 | * @param ContainerInterface $container |
||
| 613 | * @param string $rulesClass |
||
| 614 | * |
||
| 615 | * @return JsonApiDataValidatingParserInterface |
||
| 616 | * |
||
| 617 | * @throws ContainerExceptionInterface |
||
| 618 | * @throws NotFoundExceptionInterface |
||
| 619 | */ |
||
| 620 | 8 | protected static function defaultCreateDataParser( |
|
| 632 | |||
| 633 | /** |
||
| 634 | * @param ContainerInterface $container |
||
| 635 | * @param string $rulesClass |
||
| 636 | * |
||
| 637 | * @return FormValidatorInterface |
||
| 638 | * |
||
| 639 | * @throws ContainerExceptionInterface |
||
| 640 | * @throws NotFoundExceptionInterface |
||
| 641 | */ |
||
| 642 | 1 | protected static function defaultCreateFormValidator( |
|
| 654 | |||
| 655 | /** |
||
| 656 | * @param ContainerInterface $container |
||
| 657 | * @param string $schemaClass |
||
| 658 | * |
||
| 659 | * @return ParametersMapperInterface |
||
| 660 | * |
||
| 661 | * @throws ContainerExceptionInterface |
||
| 662 | * @throws NotFoundExceptionInterface |
||
| 663 | */ |
||
| 664 | 16 | protected static function defaultCreateParameterMapper( |
|
| 679 | |||
| 680 | /** |
||
| 681 | * @param JsonApiQueryValidatingParserInterface $queryParser |
||
| 682 | * |
||
| 683 | * @return EncodingParametersInterface |
||
| 684 | */ |
||
| 685 | 14 | protected static function defaultCreateEncodingParameters( |
|
| 693 | |||
| 694 | /** |
||
| 695 | * @param ContainerInterface $container |
||
| 696 | * @param string|null $class |
||
| 697 | * |
||
| 698 | * @return CrudInterface |
||
| 699 | * |
||
| 700 | * @throws ContainerExceptionInterface |
||
| 701 | * @throws NotFoundExceptionInterface |
||
| 702 | */ |
||
| 703 | 26 | protected static function defaultCreateApi(ContainerInterface $container, string $class): CrudInterface |
|
| 713 | |||
| 714 | /** |
||
| 715 | * @param UriInterface $requestUri |
||
| 716 | * @param SettingsProviderInterface $provider |
||
| 717 | * @param JsonSchemasInterface $jsonSchemas |
||
| 718 | * @param EncoderInterface $encoder |
||
| 719 | * @param EncodingParametersInterface|null $parameters |
||
| 720 | * |
||
| 721 | * @return ResponsesInterface |
||
| 722 | */ |
||
| 723 | 21 | protected static function defaultCreateResponses( |
|
| 743 | |||
| 744 | /** |
||
| 745 | * Developers can override the method in order to add/remove some data for `create`/`update` inputs. |
||
| 746 | * |
||
| 747 | * @param string $requestBody |
||
| 748 | * @param FactoryInterface $errorFactory |
||
| 749 | * @param FormatterFactoryInterface $formatterFactory |
||
| 750 | * @param string $messagesNamespace |
||
| 751 | * @param string $errorMessage |
||
| 752 | * |
||
| 753 | * @return array |
||
| 754 | */ |
||
| 755 | 7 | protected static function readJsonFromRequest( |
|
| 772 | |||
| 773 | /** |
||
| 774 | * Developers can override the method in order to use custom data mapping from a Schema to Model. |
||
| 775 | * |
||
| 776 | * @param array $captures |
||
| 777 | * @param string $schemaClass |
||
| 778 | * @param ModelSchemaInfoInterface $schemaInfo |
||
| 779 | * |
||
| 780 | * @return array |
||
| 781 | */ |
||
| 782 | 4 | protected static function mapSchemaDataToModelData( |
|
| 819 | |||
| 820 | /** |
||
| 821 | * @param null|string $value |
||
| 822 | * |
||
| 823 | * @return void |
||
| 824 | */ |
||
| 825 | 26 | private static function assertClassValueDefined(?string $value): void |
|
| 829 | |||
| 830 | /** |
||
| 831 | * @param string $class |
||
| 832 | * @param string $interface |
||
| 833 | * |
||
| 834 | * @return void |
||
| 835 | */ |
||
| 836 | 27 | private static function assertClassImplements(string $class, string $interface): void |
|
| 843 | } |
||
| 844 |
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.