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 defaultIndex( |
|
| 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 defaultRead( |
|
| 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 defaultReadRelationshipWithClosure( |
|
| 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 defaultReadRelationshipIdentifiersWithClosure( |
|
| 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 defaultCreate( |
|
| 263 | |||
| 264 | /** @noinspection PhpTooManyParametersInspection |
||
| 265 | * @param string $index |
||
| 266 | * @param UriInterface $requestUri |
||
| 267 | * @param string $requestBody |
||
| 268 | * @param string $schemeClass |
||
| 269 | * @param ModelSchemeInfoInterface $schemeInfo |
||
| 270 | * @param JsonApiDataValidatingParserInterface $validator |
||
| 271 | * @param CrudInterface $crud |
||
| 272 | * @param SettingsProviderInterface $provider |
||
| 273 | * @param JsonSchemesInterface $jsonSchemes |
||
| 274 | * @param EncoderInterface $encoder |
||
| 275 | * @param FactoryInterface $errorFactory |
||
| 276 | * @param FormatterFactoryInterface $formatterFactory |
||
| 277 | * @param string $messagesNamespace |
||
| 278 | * @param string $errorMessage |
||
| 279 | * |
||
| 280 | * @return ResponseInterface |
||
| 281 | * |
||
| 282 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
| 283 | * @SuppressWarnings(PHPMD.ExcessiveParameterList) |
||
| 284 | */ |
||
| 285 | 6 | protected static function defaultUpdate( |
|
| 335 | |||
| 336 | /** @noinspection PhpTooManyParametersInspection |
||
| 337 | * @param string $parentIndex |
||
| 338 | * @param string $modelRelName |
||
| 339 | * @param string $childIndex |
||
| 340 | * @param UriInterface $requestUri |
||
| 341 | * @param string $requestBody |
||
| 342 | * @param string $childSchemeClass |
||
| 343 | * @param ModelSchemeInfoInterface $schemeInfo |
||
| 344 | * @param JsonApiDataValidatingParserInterface $childValidator |
||
| 345 | * @param CrudInterface $parentCrud |
||
| 346 | * @param CrudInterface $childCrud |
||
| 347 | * @param SettingsProviderInterface $provider |
||
| 348 | * @param JsonSchemesInterface $jsonSchemes |
||
| 349 | * @param EncoderInterface $encoder |
||
| 350 | * @param FactoryInterface $errorFactory |
||
| 351 | * @param FormatterFactoryInterface $formatterFactory |
||
| 352 | * @param string $messagesNamespace |
||
| 353 | * @param string $errorMessage |
||
| 354 | * |
||
| 355 | * @return ResponseInterface |
||
| 356 | * |
||
| 357 | * @SuppressWarnings(PHPMD.ExcessiveParameterList) |
||
| 358 | */ |
||
| 359 | 2 | protected static function defaultUpdateInRelationship( |
|
| 402 | |||
| 403 | /** |
||
| 404 | * @param string $index |
||
| 405 | * @param UriInterface $requestUri |
||
| 406 | * @param CrudInterface $crud |
||
| 407 | * @param SettingsProviderInterface $provider |
||
| 408 | * @param JsonSchemesInterface $jsonSchemes |
||
| 409 | * @param EncoderInterface $encoder |
||
| 410 | * |
||
| 411 | * @return ResponseInterface |
||
| 412 | */ |
||
| 413 | 2 | protected static function defaultDelete( |
|
| 429 | |||
| 430 | /** @noinspection PhpTooManyParametersInspection |
||
| 431 | * @param string $parentIndex |
||
| 432 | * @param string $modelRelName |
||
| 433 | * @param string $childIndex |
||
| 434 | * @param UriInterface $requestUri |
||
| 435 | * @param CrudInterface $parentCrud |
||
| 436 | * @param CrudInterface $childCrud |
||
| 437 | * @param SettingsProviderInterface $provider |
||
| 438 | * @param JsonSchemesInterface $jsonSchemes |
||
| 439 | * @param EncoderInterface $encoder |
||
| 440 | * |
||
| 441 | * @return ResponseInterface |
||
| 442 | */ |
||
| 443 | 1 | protected static function defaultDeleteInRelationship( |
|
| 470 | |||
| 471 | /** |
||
| 472 | * @param ContainerInterface $container |
||
| 473 | * @param string $rulesClass |
||
| 474 | * |
||
| 475 | * @return JsonApiQueryValidatingParserInterface |
||
| 476 | * @throws ContainerExceptionInterface |
||
| 477 | * @throws NotFoundExceptionInterface |
||
| 478 | */ |
||
| 479 | 16 | protected static function createQueryParser( |
|
| 480 | ContainerInterface $container, |
||
| 481 | string $rulesClass = DefaultQueryValidationRules::class |
||
| 482 | ): JsonApiQueryValidatingParserInterface { |
||
| 483 | 16 | static::assertClassImplements($rulesClass, JsonApiQueryRulesInterface::class); |
|
| 484 | |||
| 485 | /** @var JsonApiParserFactoryInterface $factory */ |
||
| 486 | 16 | $factory = $container->get(JsonApiParserFactoryInterface::class); |
|
| 487 | 16 | $validator = $factory->createQueryParser($rulesClass); |
|
| 488 | |||
| 489 | 16 | return $validator; |
|
| 490 | } |
||
| 491 | |||
| 492 | /** |
||
| 493 | * @param ContainerInterface $container |
||
| 494 | * @param string $rulesClass |
||
| 495 | * |
||
| 496 | * @return JsonApiDataValidatingParserInterface |
||
| 497 | * |
||
| 498 | * @throws ContainerExceptionInterface |
||
| 499 | * @throws NotFoundExceptionInterface |
||
| 500 | */ |
||
| 501 | 8 | protected static function createDataParser( |
|
| 502 | ContainerInterface $container, |
||
| 503 | string $rulesClass |
||
| 504 | ): JsonApiDataValidatingParserInterface { |
||
| 505 | 8 | static::assertClassImplements($rulesClass, JsonApiDataRulesInterface::class); |
|
| 506 | |||
| 507 | /** @var JsonApiParserFactoryInterface $factory */ |
||
| 508 | 8 | $factory = $container->get(JsonApiParserFactoryInterface::class); |
|
| 509 | 8 | $validator = $factory->createDataParser($rulesClass); |
|
| 510 | |||
| 511 | 8 | return $validator; |
|
| 512 | } |
||
| 513 | |||
| 514 | /** |
||
| 515 | * @param string $requestBody |
||
| 516 | * @param FactoryInterface $errorFactory |
||
| 517 | * @param FormatterFactoryInterface $formatterFactory |
||
| 518 | * @param string $messagesNamespace |
||
| 519 | * @param string $errorMessage |
||
| 520 | * |
||
| 521 | * @return array |
||
| 522 | */ |
||
| 523 | 7 | protected static function readJsonFromRequest( |
|
| 540 | |||
| 541 | /** |
||
| 542 | * @param ContainerInterface $container |
||
| 543 | * @param string $schemaClass |
||
| 544 | * |
||
| 545 | * @return ParametersMapperInterface |
||
| 546 | * @throws ContainerExceptionInterface |
||
| 547 | * @throws NotFoundExceptionInterface |
||
| 548 | */ |
||
| 549 | 16 | protected static function createParameterMapper( |
|
| 564 | |||
| 565 | /** |
||
| 566 | * @param array $captures |
||
| 567 | * @param string $schemeClass |
||
| 568 | * @param ModelSchemeInfoInterface $schemeInfo |
||
| 569 | * |
||
| 570 | * @return array |
||
| 571 | */ |
||
| 572 | 4 | protected static function mapSchemeDataToModelData( |
|
| 609 | |||
| 610 | /** |
||
| 611 | * @param ContainerInterface $container |
||
| 612 | * @param string|null $class |
||
| 613 | * |
||
| 614 | * @return CrudInterface |
||
| 615 | * |
||
| 616 | * @throws ContainerExceptionInterface |
||
| 617 | * @throws NotFoundExceptionInterface |
||
| 618 | */ |
||
| 619 | 26 | protected static function createApi(ContainerInterface $container, string $class): CrudInterface |
|
| 629 | |||
| 630 | /** |
||
| 631 | * @param UriInterface $requestUri |
||
| 632 | * @param SettingsProviderInterface $provider |
||
| 633 | * @param JsonSchemesInterface $jsonSchemes |
||
| 634 | * @param EncoderInterface $encoder |
||
| 635 | * @param EncodingParametersInterface|null $parameters |
||
| 636 | * |
||
| 637 | * @return ResponsesInterface |
||
| 638 | */ |
||
| 639 | 21 | protected static function createResponses( |
|
| 659 | |||
| 660 | /** |
||
| 661 | * @param null|string $value |
||
| 662 | * |
||
| 663 | * @return void |
||
| 664 | */ |
||
| 665 | 26 | private static function assertClassValueDefined(?string $value): void |
|
| 669 | |||
| 670 | /** |
||
| 671 | * @param string $class |
||
| 672 | * @param string $interface |
||
| 673 | * |
||
| 674 | * @return void |
||
| 675 | */ |
||
| 676 | 26 | private static function assertClassImplements(string $class, string $interface): void |
|
| 683 | } |
||
| 684 |
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.