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; |
||
59 | trait DefaultControllerMethodsTrait |
||
60 | { |
||
61 | /** @noinspection PhpTooManyParametersInspection |
||
62 | * @param array $queryParams |
||
63 | * @param UriInterface $requestUri |
||
64 | * @param JsonApiQueryParserInterface $queryParser |
||
65 | * @param ParametersMapperInterface $mapper |
||
66 | * @param CrudInterface $crud |
||
67 | * @param EncoderInterface $encoder |
||
68 | * |
||
69 | * @return ResponseInterface |
||
70 | */ |
||
71 | 12 | protected static function defaultIndexHandler( |
|
90 | |||
91 | /** @noinspection PhpTooManyParametersInspection |
||
92 | * @param string $index |
||
93 | * @param array $queryParams |
||
94 | * @param UriInterface $requestUri |
||
95 | * @param JsonApiQueryParserInterface $queryParser |
||
96 | * @param ParametersMapperInterface $mapper |
||
97 | * @param CrudInterface $crud |
||
98 | * @param EncoderInterface $encoder |
||
99 | * |
||
100 | * @return ResponseInterface |
||
101 | */ |
||
102 | 1 | protected static function defaultReadHandler( |
|
124 | |||
125 | /** @noinspection PhpTooManyParametersInspection |
||
126 | * @param string $index |
||
127 | * @param Closure $apiHandler |
||
128 | * @param array $queryParams |
||
129 | * @param UriInterface $requestUri |
||
130 | * @param JsonApiQueryParserInterface $queryParser |
||
131 | * @param ParametersMapperInterface $mapper |
||
132 | * @param CrudInterface $crud |
||
133 | * @param EncoderInterface $encoder |
||
134 | * |
||
135 | * @return ResponseInterface |
||
136 | * |
||
137 | * @SuppressWarnings(PHPMD.ExcessiveParameterList) |
||
138 | */ |
||
139 | 2 | protected static function defaultReadRelationshipWithClosureHandler( |
|
162 | |||
163 | /** @noinspection PhpTooManyParametersInspection |
||
164 | * @param string $index |
||
165 | * @param Closure $apiHandler |
||
166 | * @param array $queryParams |
||
167 | * @param UriInterface $requestUri |
||
168 | * @param JsonApiQueryParserInterface $queryParser |
||
169 | * @param ParametersMapperInterface $mapper |
||
170 | * @param CrudInterface $crud |
||
171 | * @param EncoderInterface $encoder |
||
172 | * |
||
173 | * @return ResponseInterface |
||
174 | * |
||
175 | * @SuppressWarnings(PHPMD.ExcessiveParameterList) |
||
176 | */ |
||
177 | 1 | protected static function defaultReadRelationshipIdentifiersWithClosureHandler( |
|
200 | |||
201 | /** @noinspection PhpTooManyParametersInspection |
||
202 | * @param UriInterface $requestUri |
||
203 | * @param string $requestBody |
||
204 | * @param string $schemaClass |
||
205 | * @param ModelSchemaInfoInterface $schemaInfo |
||
206 | * @param JsonApiDataParserInterface $parser |
||
207 | * @param CrudInterface $crud |
||
208 | * @param JsonSchemasInterface $jsonSchemas |
||
209 | * @param EncoderInterface $encoder |
||
210 | * @param FactoryInterface $errorFactory |
||
211 | * @param FormatterFactoryInterface $formatterFactory |
||
212 | * @param string $messagesNamespace |
||
213 | * @param string $errorMessage |
||
214 | * |
||
215 | * @return ResponseInterface |
||
216 | * |
||
217 | * @SuppressWarnings(PHPMD.ExcessiveParameterList) |
||
218 | */ |
||
219 | 2 | protected static function defaultCreateHandler( |
|
249 | |||
250 | /** @noinspection PhpTooManyParametersInspection |
||
251 | * @param string $requestBody |
||
252 | * @param string $schemaClass |
||
253 | * @param ModelSchemaInfoInterface $schemaInfo |
||
254 | * @param JsonApiDataParserInterface $parser |
||
255 | * @param CrudInterface $crud |
||
256 | * @param FactoryInterface $errorFactory |
||
257 | * @param FormatterFactoryInterface $formatterFactory |
||
258 | * @param string $messagesNamespace |
||
259 | * @param string $errorMessage |
||
260 | * |
||
261 | * @return ResponseInterface |
||
|
|||
262 | */ |
||
263 | 2 | protected static function defaultCreate( |
|
301 | |||
302 | /** |
||
303 | * @param string $index |
||
304 | * @param UriInterface $requestUri |
||
305 | * @param CrudInterface $crud |
||
306 | * @param JsonSchemasInterface $jsonSchemas |
||
307 | * @param EncoderInterface $encoder |
||
308 | * |
||
309 | * @return ResponseInterface |
||
310 | */ |
||
311 | 1 | protected static function defaultCreateResponse( |
|
327 | |||
328 | /** @noinspection PhpTooManyParametersInspection |
||
329 | * @param string $index |
||
330 | * @param UriInterface $requestUri |
||
331 | * @param string $requestBody |
||
332 | * @param string $schemaClass |
||
333 | * @param ModelSchemaInfoInterface $schemaInfo |
||
334 | * @param JsonApiDataParserInterface $parser |
||
335 | * @param CrudInterface $crud |
||
336 | * @param EncoderInterface $encoder |
||
337 | * @param FactoryInterface $errorFactory |
||
338 | * @param FormatterFactoryInterface $formatterFactory |
||
339 | * @param string $messagesNamespace |
||
340 | * @param string $errorMessage |
||
341 | * |
||
342 | * @return ResponseInterface |
||
343 | * |
||
344 | * @SuppressWarnings(PHPMD.ExcessiveParameterList) |
||
345 | */ |
||
346 | 6 | protected static function defaultUpdateHandler( |
|
377 | |||
378 | /** @noinspection PhpTooManyParametersInspection |
||
379 | * @param string $index |
||
380 | * @param string $requestBody |
||
381 | * @param string $schemaClass |
||
382 | * @param ModelSchemaInfoInterface $schemaInfo |
||
383 | * @param JsonApiDataParserInterface $parser |
||
384 | * @param CrudInterface $crud |
||
385 | * @param FactoryInterface $errorFactory |
||
386 | * @param FormatterFactoryInterface $formatterFactory |
||
387 | * @param string $messagesNamespace |
||
388 | * @param string $errorMessage |
||
389 | * |
||
390 | * @return int |
||
391 | * |
||
392 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
393 | * @SuppressWarnings(PHPMD.ExcessiveParameterList) |
||
394 | */ |
||
395 | 6 | protected static function defaultUpdate( |
|
449 | |||
450 | /** |
||
451 | * @param int $updated |
||
452 | * @param string $index |
||
453 | * @param UriInterface $requestUri |
||
454 | * @param CrudInterface $crud |
||
455 | * @param EncoderInterface $encoder |
||
456 | * |
||
457 | * @return ResponseInterface |
||
458 | * |
||
459 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
460 | */ |
||
461 | 3 | protected static function defaultUpdateResponse( |
|
478 | |||
479 | /** |
||
480 | * @param string $index |
||
481 | * @param UriInterface $requestUri |
||
482 | * @param JsonApiQueryParserInterface $queryParser |
||
483 | * @param CrudInterface $crud |
||
484 | * @param EncoderInterface $encoder |
||
485 | * |
||
486 | * @return ResponseInterface |
||
487 | */ |
||
488 | 1 | protected static function defaultDeleteHandler( |
|
503 | |||
504 | /** @noinspection PhpTooManyParametersInspection |
||
505 | * @param string $index |
||
506 | * @param string $jsonRelName |
||
507 | * @param string $modelRelName |
||
508 | * @param UriInterface $requestUri |
||
509 | * @param string $requestBody |
||
510 | * @param string $schemaClass |
||
511 | * @param ModelSchemaInfoInterface $schemaInfo |
||
512 | * @param JsonApiQueryParserInterface $queryParser |
||
513 | * @param JsonApiDataParserInterface $dataValidator |
||
514 | * @param CrudInterface $parentCrud |
||
515 | * @param EncoderInterface $encoder |
||
516 | * @param FactoryInterface $errorFactory |
||
517 | * @param FormatterFactoryInterface $formatterFactory |
||
518 | * |
||
519 | * @return ResponseInterface |
||
520 | * |
||
521 | * @SuppressWarnings(PHPMD.ExcessiveParameterList) |
||
522 | */ |
||
523 | 1 | protected static function defaultAddInRelationshipHandler( |
|
556 | |||
557 | /** @noinspection PhpTooManyParametersInspection |
||
558 | * @param string $index |
||
559 | * @param string $jsonRelName |
||
560 | * @param string $modelRelName |
||
561 | * @param UriInterface $requestUri |
||
562 | * @param string $requestBody |
||
563 | * @param string $schemaClass |
||
564 | * @param ModelSchemaInfoInterface $schemaInfo |
||
565 | * @param JsonApiQueryParserInterface $queryParser |
||
566 | * @param JsonApiDataParserInterface $dataValidator |
||
567 | * @param CrudInterface $parentCrud |
||
568 | * @param EncoderInterface $encoder |
||
569 | * @param FactoryInterface $errorFactory |
||
570 | * @param FormatterFactoryInterface $formatterFactory |
||
571 | * |
||
572 | * @return ResponseInterface |
||
573 | * |
||
574 | * @SuppressWarnings(PHPMD.ExcessiveParameterList) |
||
575 | */ |
||
576 | 1 | protected static function defaultDeleteInRelationshipHandler( |
|
609 | |||
610 | /** @noinspection PhpTooManyParametersInspection |
||
611 | * @param string $index |
||
612 | * @param string $jsonRelName |
||
613 | * @param string $modelRelName |
||
614 | * @param UriInterface $requestUri |
||
615 | * @param string $requestBody |
||
616 | * @param string $schemaClass |
||
617 | * @param ModelSchemaInfoInterface $schemaInfo |
||
618 | * @param JsonApiQueryParserInterface $queryParser |
||
619 | * @param JsonApiDataParserInterface $dataValidator |
||
620 | * @param CrudInterface $crud |
||
621 | * @param EncoderInterface $encoder |
||
622 | * @param FactoryInterface $errorFactory |
||
623 | * @param FormatterFactoryInterface $formatterFactory |
||
624 | * |
||
625 | * @return ResponseInterface |
||
626 | * |
||
627 | * @SuppressWarnings(PHPMD.ExcessiveParameterList) |
||
628 | */ |
||
629 | 1 | protected static function defaultReplaceInRelationship( |
|
665 | |||
666 | /** |
||
667 | * @param ContainerInterface $container |
||
668 | * @param string $rulesClass |
||
669 | * |
||
670 | * @return JsonApiQueryParserInterface |
||
671 | * |
||
672 | * @throws ContainerExceptionInterface |
||
673 | * @throws NotFoundExceptionInterface |
||
674 | */ |
||
675 | 20 | protected static function defaultCreateQueryParser( |
|
687 | |||
688 | /** |
||
689 | * @param ContainerInterface $container |
||
690 | * @param string $rulesClass |
||
691 | * |
||
692 | * @return JsonApiDataParserInterface |
||
693 | * |
||
694 | * @throws ContainerExceptionInterface |
||
695 | * @throws NotFoundExceptionInterface |
||
696 | */ |
||
697 | 11 | protected static function defaultCreateDataParser( |
|
709 | |||
710 | /** |
||
711 | * @param ContainerInterface $container |
||
712 | * @param string $rulesClass |
||
713 | * |
||
714 | * @return FormValidatorInterface |
||
715 | * |
||
716 | * @throws ContainerExceptionInterface |
||
717 | * @throws NotFoundExceptionInterface |
||
718 | */ |
||
719 | 1 | protected static function defaultCreateFormValidator( |
|
731 | |||
732 | /** |
||
733 | * @param ContainerInterface $container |
||
734 | * @param string $schemaClass |
||
735 | * |
||
736 | * @return ParametersMapperInterface |
||
737 | * |
||
738 | * @throws ContainerExceptionInterface |
||
739 | * @throws NotFoundExceptionInterface |
||
740 | */ |
||
741 | 16 | protected static function defaultCreateParameterMapper( |
|
756 | |||
757 | /** |
||
758 | * @param JsonApiQueryParserInterface $queryParser |
||
759 | * @param EncoderInterface $encoder |
||
760 | * |
||
761 | * @return void |
||
762 | */ |
||
763 | 14 | protected static function defaultApplyIncludesAndFieldSetsToEncoder( |
|
775 | |||
776 | /** |
||
777 | * @param ContainerInterface $container |
||
778 | * @param string|null $class |
||
779 | * |
||
780 | * @return CrudInterface |
||
781 | * |
||
782 | * @throws ContainerExceptionInterface |
||
783 | * @throws NotFoundExceptionInterface |
||
784 | */ |
||
785 | 28 | protected static function defaultCreateApi(ContainerInterface $container, string $class): CrudInterface |
|
795 | |||
796 | /** |
||
797 | * @param UriInterface $requestUri |
||
798 | * @param EncoderInterface $encoder |
||
799 | * |
||
800 | * @return ResponsesInterface |
||
801 | */ |
||
802 | 21 | protected static function defaultCreateResponses( |
|
814 | |||
815 | /** |
||
816 | * Developers can override the method in order to add/remove some data for `create`/`update` inputs. |
||
817 | * |
||
818 | * @param string $requestBody |
||
819 | * @param FactoryInterface $errorFactory |
||
820 | * @param FormatterFactoryInterface $formatterFactory |
||
821 | * @param string $messagesNamespace |
||
822 | * @param string $errorMessage |
||
823 | * |
||
824 | * @return array |
||
825 | */ |
||
826 | 11 | protected static function readJsonFromRequest( |
|
843 | |||
844 | /** |
||
845 | * Developers can override the method in order to use custom data mapping from a Schema to Model. |
||
846 | * |
||
847 | * @param iterable $captures |
||
848 | * @param string $schemaClass |
||
849 | * @param ModelSchemaInfoInterface $schemaInfo |
||
850 | * |
||
851 | * @return array |
||
852 | */ |
||
853 | 6 | protected static function mapSchemaDataToModelData( |
|
890 | |||
891 | /** |
||
892 | * @param mixed $model |
||
893 | * @param UriInterface $requestUri |
||
894 | * @param JsonSchemasInterface $jsonSchemas |
||
895 | * |
||
896 | * @return string |
||
897 | */ |
||
898 | 1 | protected static function defaultGetResourceUrl( |
|
910 | |||
911 | /** |
||
912 | * @param null|string $value |
||
913 | * |
||
914 | * @return void |
||
915 | */ |
||
916 | 28 | private static function assertClassValueDefined(?string $value): void |
|
920 | |||
921 | /** |
||
922 | * @param string $class |
||
923 | * @param string $interface |
||
924 | * |
||
925 | * @return void |
||
926 | */ |
||
927 | 29 | private static function assertClassImplements(string $class, string $interface): void |
|
934 | } |
||
935 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.