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( |
|
| 133 | |||
| 134 | /** @noinspection PhpTooManyParametersInspection |
||
| 135 | * @param string $index |
||
| 136 | * @param Closure $apiHandler |
||
| 137 | * @param array $queryParams |
||
| 138 | * @param UriInterface $requestUri |
||
| 139 | * @param JsonApiQueryValidatingParserInterface $queryParser |
||
| 140 | * @param ParametersMapperInterface $mapper |
||
| 141 | * @param CrudInterface $crud |
||
| 142 | * @param SettingsProviderInterface $provider |
||
| 143 | * @param JsonSchemasInterface $jsonSchemas |
||
| 144 | * @param EncoderInterface $encoder |
||
| 145 | * |
||
| 146 | * @return ResponseInterface |
||
| 147 | * |
||
| 148 | * @SuppressWarnings(PHPMD.ExcessiveParameterList) |
||
| 149 | */ |
||
| 150 | 2 | protected static function defaultReadRelationshipWithClosureHandler( |
|
| 175 | |||
| 176 | /** @noinspection PhpTooManyParametersInspection |
||
| 177 | * @param string $index |
||
| 178 | * @param Closure $apiHandler |
||
| 179 | * @param array $queryParams |
||
| 180 | * @param UriInterface $requestUri |
||
| 181 | * @param JsonApiQueryValidatingParserInterface $queryParser |
||
| 182 | * @param ParametersMapperInterface $mapper |
||
| 183 | * @param CrudInterface $crud |
||
| 184 | * @param SettingsProviderInterface $provider |
||
| 185 | * @param JsonSchemasInterface $jsonSchemas |
||
| 186 | * @param EncoderInterface $encoder |
||
| 187 | * |
||
| 188 | * @return ResponseInterface |
||
| 189 | * |
||
| 190 | * @SuppressWarnings(PHPMD.ExcessiveParameterList) |
||
| 191 | */ |
||
| 192 | 1 | protected static function defaultReadRelationshipIdentifiersWithClosureHandler( |
|
| 217 | |||
| 218 | /** @noinspection PhpTooManyParametersInspection |
||
| 219 | * @param UriInterface $requestUri |
||
| 220 | * @param string $requestBody |
||
| 221 | * @param string $schemaClass |
||
| 222 | * @param ModelSchemaInfoInterface $schemaInfo |
||
| 223 | * @param JsonApiDataValidatingParserInterface $validator |
||
| 224 | * @param CrudInterface $crud |
||
| 225 | * @param SettingsProviderInterface $provider |
||
| 226 | * @param JsonSchemasInterface $jsonSchemas |
||
| 227 | * @param EncoderInterface $encoder |
||
| 228 | * @param FactoryInterface $errorFactory |
||
| 229 | * @param FormatterFactoryInterface $formatterFactory |
||
| 230 | * |
||
| 231 | * @return ResponseInterface |
||
| 232 | * |
||
| 233 | * @SuppressWarnings(PHPMD.ExcessiveParameterList) |
||
| 234 | */ |
||
| 235 | 1 | protected static function defaultCreateHandler( |
|
| 262 | |||
| 263 | /** |
||
| 264 | * @param string $requestBody |
||
| 265 | * @param string $schemaClass |
||
| 266 | * @param ModelSchemaInfoInterface $schemaInfo |
||
| 267 | * @param JsonApiDataValidatingParserInterface $validator |
||
| 268 | * @param CrudInterface $crud |
||
| 269 | * @param FactoryInterface $errorFactory |
||
| 270 | * @param FormatterFactoryInterface $formatterFactory |
||
| 271 | * |
||
| 272 | * @return ResponseInterface |
||
|
|
|||
| 273 | */ |
||
| 274 | 1 | protected static function defaultCreate( |
|
| 292 | |||
| 293 | /** |
||
| 294 | * @param string $index |
||
| 295 | * @param UriInterface $requestUri |
||
| 296 | * @param CrudInterface $crud |
||
| 297 | * @param SettingsProviderInterface $provider |
||
| 298 | * @param JsonSchemasInterface $jsonSchemas |
||
| 299 | * @param EncoderInterface $encoder |
||
| 300 | * |
||
| 301 | * @return ResponseInterface |
||
| 302 | */ |
||
| 303 | 1 | protected static function defaultCreateResponse( |
|
| 320 | |||
| 321 | /** @noinspection PhpTooManyParametersInspection |
||
| 322 | * @param string $index |
||
| 323 | * @param UriInterface $requestUri |
||
| 324 | * @param string $requestBody |
||
| 325 | * @param string $schemaClass |
||
| 326 | * @param ModelSchemaInfoInterface $schemaInfo |
||
| 327 | * @param JsonApiDataValidatingParserInterface $validator |
||
| 328 | * @param CrudInterface $crud |
||
| 329 | * @param SettingsProviderInterface $provider |
||
| 330 | * @param JsonSchemasInterface $jsonSchemas |
||
| 331 | * @param EncoderInterface $encoder |
||
| 332 | * @param FactoryInterface $errorFactory |
||
| 333 | * @param FormatterFactoryInterface $formatterFactory |
||
| 334 | * @param string $messagesNamespace |
||
| 335 | * @param string $errorMessage |
||
| 336 | * |
||
| 337 | * @return ResponseInterface |
||
| 338 | * |
||
| 339 | * @SuppressWarnings(PHPMD.ExcessiveParameterList) |
||
| 340 | */ |
||
| 341 | 5 | protected static function defaultUpdateHandler( |
|
| 374 | |||
| 375 | /** @noinspection PhpTooManyParametersInspection |
||
| 376 | * @param string $index |
||
| 377 | * @param string $requestBody |
||
| 378 | * @param string $schemaClass |
||
| 379 | * @param ModelSchemaInfoInterface $schemaInfo |
||
| 380 | * @param JsonApiDataValidatingParserInterface $validator |
||
| 381 | * @param CrudInterface $crud |
||
| 382 | * @param FactoryInterface $errorFactory |
||
| 383 | * @param FormatterFactoryInterface $formatterFactory |
||
| 384 | * @param string $messagesNamespace |
||
| 385 | * @param string $errorMessage |
||
| 386 | * |
||
| 387 | * @return int |
||
| 388 | * |
||
| 389 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
| 390 | * @SuppressWarnings(PHPMD.ExcessiveParameterList) |
||
| 391 | */ |
||
| 392 | 5 | protected static function defaultUpdate( |
|
| 428 | |||
| 429 | /** |
||
| 430 | * @param int $updated |
||
| 431 | * @param string $index |
||
| 432 | * @param UriInterface $requestUri |
||
| 433 | * @param CrudInterface $crud |
||
| 434 | * @param SettingsProviderInterface $provider |
||
| 435 | * @param JsonSchemasInterface $jsonSchemas |
||
| 436 | * @param EncoderInterface $encoder |
||
| 437 | * |
||
| 438 | * @return ResponseInterface |
||
| 439 | * |
||
| 440 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
| 441 | */ |
||
| 442 | 3 | protected static function defaultUpdateResponse( |
|
| 462 | |||
| 463 | /** |
||
| 464 | * @param string $index |
||
| 465 | * @param UriInterface $requestUri |
||
| 466 | * @param JsonApiQueryValidatingParserInterface $queryParser |
||
| 467 | * @param CrudInterface $crud |
||
| 468 | * @param SettingsProviderInterface $provider |
||
| 469 | * @param JsonSchemasInterface $jsonSchemas |
||
| 470 | * @param EncoderInterface $encoder |
||
| 471 | * |
||
| 472 | * @return ResponseInterface |
||
| 473 | */ |
||
| 474 | 1 | protected static function defaultDeleteHandler( |
|
| 492 | |||
| 493 | /** @noinspection PhpTooManyParametersInspection |
||
| 494 | * @param string $index |
||
| 495 | * @param string $jsonRelName |
||
| 496 | * @param string $modelRelName |
||
| 497 | * @param UriInterface $requestUri |
||
| 498 | * @param string $requestBody |
||
| 499 | * @param string $schemaClass |
||
| 500 | * @param ModelSchemaInfoInterface $schemaInfo |
||
| 501 | * @param JsonApiQueryValidatingParserInterface $queryParser |
||
| 502 | * @param JsonApiDataValidatingParserInterface $dataValidator |
||
| 503 | * @param CrudInterface $parentCrud |
||
| 504 | * @param SettingsProviderInterface $provider |
||
| 505 | * @param JsonSchemasInterface $jsonSchemas |
||
| 506 | * @param EncoderInterface $encoder |
||
| 507 | * @param FactoryInterface $errorFactory |
||
| 508 | * @param FormatterFactoryInterface $formatterFactory |
||
| 509 | * |
||
| 510 | * @return ResponseInterface |
||
| 511 | * |
||
| 512 | * @SuppressWarnings(PHPMD.ExcessiveParameterList) |
||
| 513 | */ |
||
| 514 | 1 | protected static function defaultAddInRelationshipHandler( |
|
| 550 | |||
| 551 | /** @noinspection PhpTooManyParametersInspection |
||
| 552 | * @param string $index |
||
| 553 | * @param string $jsonRelName |
||
| 554 | * @param string $modelRelName |
||
| 555 | * @param UriInterface $requestUri |
||
| 556 | * @param string $requestBody |
||
| 557 | * @param string $schemaClass |
||
| 558 | * @param ModelSchemaInfoInterface $schemaInfo |
||
| 559 | * @param JsonApiQueryValidatingParserInterface $queryParser |
||
| 560 | * @param JsonApiDataValidatingParserInterface $dataValidator |
||
| 561 | * @param CrudInterface $parentCrud |
||
| 562 | * @param SettingsProviderInterface $provider |
||
| 563 | * @param JsonSchemasInterface $jsonSchemas |
||
| 564 | * @param EncoderInterface $encoder |
||
| 565 | * @param FactoryInterface $errorFactory |
||
| 566 | * @param FormatterFactoryInterface $formatterFactory |
||
| 567 | * |
||
| 568 | * @return ResponseInterface |
||
| 569 | * |
||
| 570 | * @SuppressWarnings(PHPMD.ExcessiveParameterList) |
||
| 571 | */ |
||
| 572 | 1 | protected static function defaultDeleteInRelationshipHandler( |
|
| 608 | |||
| 609 | /** @noinspection PhpTooManyParametersInspection |
||
| 610 | * @param string $index |
||
| 611 | * @param string $jsonRelName |
||
| 612 | * @param string $modelRelName |
||
| 613 | * @param UriInterface $requestUri |
||
| 614 | * @param string $requestBody |
||
| 615 | * @param string $schemaClass |
||
| 616 | * @param ModelSchemaInfoInterface $schemaInfo |
||
| 617 | * @param JsonApiQueryValidatingParserInterface $queryParser |
||
| 618 | * @param JsonApiDataValidatingParserInterface $dataValidator |
||
| 619 | * @param CrudInterface $crud |
||
| 620 | * @param SettingsProviderInterface $provider |
||
| 621 | * @param JsonSchemasInterface $jsonSchemas |
||
| 622 | * @param EncoderInterface $encoder |
||
| 623 | * @param FactoryInterface $errorFactory |
||
| 624 | * @param FormatterFactoryInterface $formatterFactory |
||
| 625 | * |
||
| 626 | * @return ResponseInterface |
||
| 627 | * |
||
| 628 | * @SuppressWarnings(PHPMD.ExcessiveParameterList) |
||
| 629 | */ |
||
| 630 | 1 | protected static function defaultReplaceInRelationship( |
|
| 668 | |||
| 669 | /** |
||
| 670 | * @param ContainerInterface $container |
||
| 671 | * @param string $rulesClass |
||
| 672 | * |
||
| 673 | * @return JsonApiQueryValidatingParserInterface |
||
| 674 | * |
||
| 675 | * @throws ContainerExceptionInterface |
||
| 676 | * @throws NotFoundExceptionInterface |
||
| 677 | */ |
||
| 678 | 20 | protected static function defaultCreateQueryParser( |
|
| 690 | |||
| 691 | /** |
||
| 692 | * @param ContainerInterface $container |
||
| 693 | * @param string $rulesClass |
||
| 694 | * |
||
| 695 | * @return JsonApiDataValidatingParserInterface |
||
| 696 | * |
||
| 697 | * @throws ContainerExceptionInterface |
||
| 698 | * @throws NotFoundExceptionInterface |
||
| 699 | */ |
||
| 700 | 9 | protected static function defaultCreateDataParser( |
|
| 712 | |||
| 713 | /** |
||
| 714 | * @param ContainerInterface $container |
||
| 715 | * @param string $rulesClass |
||
| 716 | * |
||
| 717 | * @return FormValidatorInterface |
||
| 718 | * |
||
| 719 | * @throws ContainerExceptionInterface |
||
| 720 | * @throws NotFoundExceptionInterface |
||
| 721 | */ |
||
| 722 | 1 | protected static function defaultCreateFormValidator( |
|
| 734 | |||
| 735 | /** |
||
| 736 | * @param ContainerInterface $container |
||
| 737 | * @param string $schemaClass |
||
| 738 | * |
||
| 739 | * @return ParametersMapperInterface |
||
| 740 | * |
||
| 741 | * @throws ContainerExceptionInterface |
||
| 742 | * @throws NotFoundExceptionInterface |
||
| 743 | */ |
||
| 744 | 16 | protected static function defaultCreateParameterMapper( |
|
| 759 | |||
| 760 | /** |
||
| 761 | * @param JsonApiQueryValidatingParserInterface $queryParser |
||
| 762 | * |
||
| 763 | * @return EncodingParametersInterface |
||
| 764 | */ |
||
| 765 | 14 | protected static function defaultCreateEncodingParameters( |
|
| 773 | |||
| 774 | /** |
||
| 775 | * @param ContainerInterface $container |
||
| 776 | * @param string|null $class |
||
| 777 | * |
||
| 778 | * @return CrudInterface |
||
| 779 | * |
||
| 780 | * @throws ContainerExceptionInterface |
||
| 781 | * @throws NotFoundExceptionInterface |
||
| 782 | */ |
||
| 783 | 26 | protected static function defaultCreateApi(ContainerInterface $container, string $class): CrudInterface |
|
| 793 | |||
| 794 | /** |
||
| 795 | * @param UriInterface $requestUri |
||
| 796 | * @param SettingsProviderInterface $provider |
||
| 797 | * @param JsonSchemasInterface $jsonSchemas |
||
| 798 | * @param EncoderInterface $encoder |
||
| 799 | * @param EncodingParametersInterface|null $parameters |
||
| 800 | * |
||
| 801 | * @return ResponsesInterface |
||
| 802 | */ |
||
| 803 | 21 | protected static function defaultCreateResponses( |
|
| 823 | |||
| 824 | /** |
||
| 825 | * Developers can override the method in order to add/remove some data for `create`/`update` inputs. |
||
| 826 | * |
||
| 827 | * @param string $requestBody |
||
| 828 | * @param FactoryInterface $errorFactory |
||
| 829 | * @param FormatterFactoryInterface $formatterFactory |
||
| 830 | * @param string $messagesNamespace |
||
| 831 | * @param string $errorMessage |
||
| 832 | * |
||
| 833 | * @return array |
||
| 834 | */ |
||
| 835 | 9 | protected static function readJsonFromRequest( |
|
| 852 | |||
| 853 | /** |
||
| 854 | * Developers can override the method in order to use custom data mapping from a Schema to Model. |
||
| 855 | * |
||
| 856 | * @param array $captures |
||
| 857 | * @param string $schemaClass |
||
| 858 | * @param ModelSchemaInfoInterface $schemaInfo |
||
| 859 | * |
||
| 860 | * @return array |
||
| 861 | */ |
||
| 862 | 4 | protected static function mapSchemaDataToModelData( |
|
| 899 | |||
| 900 | /** |
||
| 901 | * @param null|string $value |
||
| 902 | * |
||
| 903 | * @return void |
||
| 904 | */ |
||
| 905 | 26 | private static function assertClassValueDefined(?string $value): void |
|
| 909 | |||
| 910 | /** |
||
| 911 | * @param string $class |
||
| 912 | * @param string $interface |
||
| 913 | * |
||
| 914 | * @return void |
||
| 915 | */ |
||
| 916 | 27 | private static function assertClassImplements(string $class, string $interface): void |
|
| 923 | } |
||
| 924 |
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.