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( |
|
| 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 JsonSchemesInterface $jsonSchemes |
||
| 105 | * @param EncoderInterface $encoder |
||
| 106 | * |
||
| 107 | * @return ResponseInterface |
||
| 108 | */ |
||
| 109 | 1 | protected static function defaultReadHandler( |
|
| 135 | |||
| 136 | /** @noinspection PhpTooManyParametersInspection |
||
| 137 | * @param Closure $apiHandler |
||
| 138 | * @param array $queryParams |
||
| 139 | * @param UriInterface $requestUri |
||
| 140 | * @param JsonApiQueryValidatingParserInterface $queryParser |
||
| 141 | * @param ParametersMapperInterface $mapper |
||
| 142 | * @param CrudInterface $crud |
||
| 143 | * @param SettingsProviderInterface $provider |
||
| 144 | * @param JsonSchemesInterface $jsonSchemes |
||
| 145 | * @param EncoderInterface $encoder |
||
| 146 | * |
||
| 147 | * @return ResponseInterface |
||
| 148 | */ |
||
| 149 | 2 | protected static function defaultReadRelationshipWithClosureHandler( |
|
| 176 | |||
| 177 | /** @noinspection PhpTooManyParametersInspection |
||
| 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 JsonSchemesInterface $jsonSchemes |
||
| 186 | * @param EncoderInterface $encoder |
||
| 187 | * |
||
| 188 | * @return ResponseInterface |
||
| 189 | */ |
||
| 190 | 1 | protected static function defaultReadRelationshipIdentifiersWithClosureHandler( |
|
| 217 | |||
| 218 | /** @noinspection PhpTooManyParametersInspection |
||
| 219 | * @param UriInterface $requestUri |
||
| 220 | * @param string $requestBody |
||
| 221 | * @param string $schemeClass |
||
| 222 | * @param ModelSchemeInfoInterface $schemeInfo |
||
| 223 | * @param JsonApiDataValidatingParserInterface $validator |
||
| 224 | * @param CrudInterface $crud |
||
| 225 | * @param SettingsProviderInterface $provider |
||
| 226 | * @param JsonSchemesInterface $jsonSchemes |
||
| 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 $schemeClass |
||
| 266 | * @param ModelSchemeInfoInterface $schemeInfo |
||
| 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 JsonSchemesInterface $jsonSchemes |
||
| 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 $schemeClass |
||
| 326 | * @param ModelSchemeInfoInterface $schemeInfo |
||
| 327 | * @param JsonApiDataValidatingParserInterface $validator |
||
| 328 | * @param CrudInterface $crud |
||
| 329 | * @param SettingsProviderInterface $provider |
||
| 330 | * @param JsonSchemesInterface $jsonSchemes |
||
| 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 $schemeClass |
||
| 379 | * @param ModelSchemeInfoInterface $schemeInfo |
||
| 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 JsonSchemesInterface $jsonSchemes |
||
| 436 | * @param EncoderInterface $encoder |
||
| 437 | * |
||
| 438 | * @return ResponseInterface |
||
| 439 | * |
||
| 440 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
| 441 | */ |
||
| 442 | 2 | protected static function defaultUpdateResponse( |
|
| 463 | |||
| 464 | /** @noinspection PhpTooManyParametersInspection |
||
| 465 | * @param string $parentIndex |
||
| 466 | * @param string $modelRelName |
||
| 467 | * @param string $childIndex |
||
| 468 | * @param UriInterface $requestUri |
||
| 469 | * @param string $requestBody |
||
| 470 | * @param string $childSchemeClass |
||
| 471 | * @param ModelSchemeInfoInterface $schemeInfo |
||
| 472 | * @param JsonApiDataValidatingParserInterface $childValidator |
||
| 473 | * @param CrudInterface $parentCrud |
||
| 474 | * @param CrudInterface $childCrud |
||
| 475 | * @param SettingsProviderInterface $provider |
||
| 476 | * @param JsonSchemesInterface $jsonSchemes |
||
| 477 | * @param EncoderInterface $encoder |
||
| 478 | * @param FactoryInterface $errorFactory |
||
| 479 | * @param FormatterFactoryInterface $formatterFactory |
||
| 480 | * @param string $messagesNamespace |
||
| 481 | * @param string $errorMessage |
||
| 482 | * |
||
| 483 | * @return ResponseInterface |
||
| 484 | * |
||
| 485 | * @SuppressWarnings(PHPMD.ExcessiveParameterList) |
||
| 486 | */ |
||
| 487 | 2 | protected static function defaultUpdateInRelationshipHandler( |
|
| 530 | |||
| 531 | /** |
||
| 532 | * @param string $index |
||
| 533 | * @param UriInterface $requestUri |
||
| 534 | * @param CrudInterface $crud |
||
| 535 | * @param SettingsProviderInterface $provider |
||
| 536 | * @param JsonSchemesInterface $jsonSchemes |
||
| 537 | * @param EncoderInterface $encoder |
||
| 538 | * |
||
| 539 | * @return ResponseInterface |
||
| 540 | */ |
||
| 541 | 1 | protected static function defaultDeleteHandler( |
|
| 557 | |||
| 558 | /** @noinspection PhpTooManyParametersInspection |
||
| 559 | * @param string $parentIndex |
||
| 560 | * @param string $modelRelName |
||
| 561 | * @param string $childIndex |
||
| 562 | * @param UriInterface $requestUri |
||
| 563 | * @param CrudInterface $parentCrud |
||
| 564 | * @param CrudInterface $childCrud |
||
| 565 | * @param SettingsProviderInterface $provider |
||
| 566 | * @param JsonSchemesInterface $jsonSchemes |
||
| 567 | * @param EncoderInterface $encoder |
||
| 568 | * |
||
| 569 | * @return ResponseInterface |
||
| 570 | */ |
||
| 571 | 1 | protected static function defaultDeleteInRelationshipHandler( |
|
| 598 | |||
| 599 | /** |
||
| 600 | * @param ContainerInterface $container |
||
| 601 | * @param string $rulesClass |
||
| 602 | * |
||
| 603 | * @return JsonApiQueryValidatingParserInterface |
||
| 604 | * @throws ContainerExceptionInterface |
||
| 605 | * @throws NotFoundExceptionInterface |
||
| 606 | */ |
||
| 607 | 16 | protected static function defaultCreateQueryParser( |
|
| 619 | |||
| 620 | /** |
||
| 621 | * @param ContainerInterface $container |
||
| 622 | * @param string $rulesClass |
||
| 623 | * |
||
| 624 | * @return JsonApiDataValidatingParserInterface |
||
| 625 | * |
||
| 626 | * @throws ContainerExceptionInterface |
||
| 627 | * @throws NotFoundExceptionInterface |
||
| 628 | */ |
||
| 629 | 8 | protected static function defaultCreateDataParser( |
|
| 641 | |||
| 642 | /** |
||
| 643 | * @param ContainerInterface $container |
||
| 644 | * @param string $schemaClass |
||
| 645 | * |
||
| 646 | * @return ParametersMapperInterface |
||
| 647 | * @throws ContainerExceptionInterface |
||
| 648 | * @throws NotFoundExceptionInterface |
||
| 649 | */ |
||
| 650 | 16 | protected static function defaultCreateParameterMapper( |
|
| 665 | |||
| 666 | /** |
||
| 667 | * @param ContainerInterface $container |
||
| 668 | * @param string|null $class |
||
| 669 | * |
||
| 670 | * @return CrudInterface |
||
| 671 | * |
||
| 672 | * @throws ContainerExceptionInterface |
||
| 673 | * @throws NotFoundExceptionInterface |
||
| 674 | */ |
||
| 675 | 26 | protected static function defaultCreateApi(ContainerInterface $container, string $class): CrudInterface |
|
| 685 | |||
| 686 | /** |
||
| 687 | * @param UriInterface $requestUri |
||
| 688 | * @param SettingsProviderInterface $provider |
||
| 689 | * @param JsonSchemesInterface $jsonSchemes |
||
| 690 | * @param EncoderInterface $encoder |
||
| 691 | * @param EncodingParametersInterface|null $parameters |
||
| 692 | * |
||
| 693 | * @return ResponsesInterface |
||
| 694 | */ |
||
| 695 | 4 | protected static function defaultCreateResponses( |
|
| 715 | |||
| 716 | /** |
||
| 717 | * @param array $captures |
||
| 718 | * @param string $schemeClass |
||
| 719 | * @param ModelSchemeInfoInterface $schemeInfo |
||
| 720 | * |
||
| 721 | * @return array |
||
| 722 | */ |
||
| 723 | 3 | private static function mapSchemeDataToModelData( |
|
| 760 | |||
| 761 | /** |
||
| 762 | * @param string $requestBody |
||
| 763 | * @param FactoryInterface $errorFactory |
||
| 764 | * @param FormatterFactoryInterface $formatterFactory |
||
| 765 | * @param string $messagesNamespace |
||
| 766 | * @param string $errorMessage |
||
| 767 | * |
||
| 768 | * @return array |
||
| 769 | */ |
||
| 770 | 6 | private static function readJsonFromRequest( |
|
| 787 | |||
| 788 | /** |
||
| 789 | * @param null|string $value |
||
| 790 | * |
||
| 791 | * @return void |
||
| 792 | */ |
||
| 793 | 26 | private static function assertClassValueDefined(?string $value): void |
|
| 797 | |||
| 798 | /** |
||
| 799 | * @param string $class |
||
| 800 | * @param string $interface |
||
| 801 | * |
||
| 802 | * @return void |
||
| 803 | */ |
||
| 804 | 26 | private static function assertClassImplements(string $class, string $interface): void |
|
| 811 | } |
||
| 812 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.