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 declare (strict_types = 1); |
||
65 | trait DefaultControllerMethodsTrait |
||
66 | { |
||
67 | /** @noinspection PhpTooManyParametersInspection |
||
68 | * @param array $queryParams |
||
69 | * @param UriInterface $requestUri |
||
70 | * @param JsonApiQueryParserInterface $queryParser |
||
71 | * @param ParametersMapperInterface $mapper |
||
72 | 12 | * @param CrudInterface $crud |
|
73 | * @param EncoderInterface $encoder |
||
74 | * |
||
75 | * @return ResponseInterface |
||
76 | */ |
||
77 | protected static function defaultIndexHandler( |
||
96 | |||
97 | /** @noinspection PhpTooManyParametersInspection |
||
98 | * @param string $index |
||
99 | * @param array $queryParams |
||
100 | * @param UriInterface $requestUri |
||
101 | * @param JsonApiQueryParserInterface $queryParser |
||
102 | * @param ParametersMapperInterface $mapper |
||
103 | 1 | * @param CrudInterface $crud |
|
104 | * @param EncoderInterface $encoder |
||
105 | * |
||
106 | * @return ResponseInterface |
||
107 | */ |
||
108 | protected static function defaultReadHandler( |
||
130 | |||
131 | /** @noinspection PhpTooManyParametersInspection |
||
132 | * @param string $index |
||
133 | * @param Closure $apiHandler |
||
134 | * @param array $queryParams |
||
135 | * @param UriInterface $requestUri |
||
136 | * @param JsonApiQueryParserInterface $queryParser |
||
137 | * @param ParametersMapperInterface $mapper |
||
138 | * @param CrudInterface $crud |
||
139 | * @param EncoderInterface $encoder |
||
140 | 2 | * |
|
141 | * @return ResponseInterface |
||
142 | * |
||
143 | * @SuppressWarnings(PHPMD.ExcessiveParameterList) |
||
144 | */ |
||
145 | protected static function defaultReadRelationshipWithClosureHandler( |
||
168 | |||
169 | /** @noinspection PhpTooManyParametersInspection |
||
170 | * @param string $index |
||
171 | * @param Closure $apiHandler |
||
172 | * @param array $queryParams |
||
173 | * @param UriInterface $requestUri |
||
174 | * @param JsonApiQueryParserInterface $queryParser |
||
175 | * @param ParametersMapperInterface $mapper |
||
176 | * @param CrudInterface $crud |
||
177 | * @param EncoderInterface $encoder |
||
178 | 1 | * |
|
179 | * @return ResponseInterface |
||
180 | * |
||
181 | * @SuppressWarnings(PHPMD.ExcessiveParameterList) |
||
182 | */ |
||
183 | protected static function defaultReadRelationshipIdentifiersWithClosureHandler( |
||
206 | |||
207 | /** @noinspection PhpTooManyParametersInspection |
||
208 | * @param UriInterface $requestUri |
||
209 | * @param string $requestBody |
||
210 | * @param string $schemaClass |
||
211 | * @param ModelSchemaInfoInterface $schemaInfo |
||
212 | * @param JsonApiDataParserInterface $parser |
||
213 | * @param CrudInterface $crud |
||
214 | * @param JsonSchemasInterface $jsonSchemas |
||
215 | * @param EncoderInterface $encoder |
||
216 | * @param FactoryInterface $errorFactory |
||
217 | * @param FormatterFactoryInterface $formatterFactory |
||
218 | 2 | * |
|
219 | * @return ResponseInterface |
||
220 | * |
||
221 | * @SuppressWarnings(PHPMD.ExcessiveParameterList) |
||
222 | */ |
||
223 | 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 | 2 | * @param FactoryInterface $errorFactory |
|
257 | * @param FormatterFactoryInterface $formatterFactory |
||
258 | * |
||
259 | * @return ResponseInterface |
||
|
|||
260 | */ |
||
261 | protected static function defaultCreate( |
||
295 | |||
296 | /** |
||
297 | * @param string $index |
||
298 | * @param UriInterface $requestUri |
||
299 | * @param CrudInterface $crud |
||
300 | 1 | * @param JsonSchemasInterface $jsonSchemas |
|
301 | * @param EncoderInterface $encoder |
||
302 | * |
||
303 | * @return ResponseInterface |
||
304 | */ |
||
305 | protected static function defaultCreateResponse( |
||
321 | |||
322 | /** @noinspection PhpTooManyParametersInspection |
||
323 | * @param string $index |
||
324 | * @param UriInterface $requestUri |
||
325 | * @param string $requestBody |
||
326 | * @param string $schemaClass |
||
327 | * @param ModelSchemaInfoInterface $schemaInfo |
||
328 | * @param JsonApiDataParserInterface $parser |
||
329 | * @param CrudInterface $crud |
||
330 | * @param EncoderInterface $encoder |
||
331 | * @param FactoryInterface $errorFactory |
||
332 | * @param FormatterFactoryInterface $formatterFactory |
||
333 | 6 | * |
|
334 | * @return ResponseInterface |
||
335 | * |
||
336 | * @SuppressWarnings(PHPMD.ExcessiveParameterList) |
||
337 | */ |
||
338 | 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 JsonApiDataParserInterface $parser |
||
372 | * @param CrudInterface $crud |
||
373 | * @param FactoryInterface $errorFactory |
||
374 | * @param FormatterFactoryInterface $formatterFactory |
||
375 | * |
||
376 | 6 | * @return int |
|
377 | * |
||
378 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
379 | * @SuppressWarnings(PHPMD.ExcessiveParameterList) |
||
380 | */ |
||
381 | protected static function defaultUpdate( |
||
432 | |||
433 | /** |
||
434 | * @param int $updated |
||
435 | * @param string $index |
||
436 | * @param UriInterface $requestUri |
||
437 | * @param CrudInterface $crud |
||
438 | * @param EncoderInterface $encoder |
||
439 | 3 | * |
|
440 | * @return ResponseInterface |
||
441 | * |
||
442 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
443 | */ |
||
444 | protected static function defaultUpdateResponse( |
||
461 | |||
462 | /** |
||
463 | * @param string $index |
||
464 | * @param UriInterface $requestUri |
||
465 | * @param JsonApiQueryParserInterface $queryParser |
||
466 | 1 | * @param CrudInterface $crud |
|
467 | * @param EncoderInterface $encoder |
||
468 | * |
||
469 | * @return ResponseInterface |
||
470 | */ |
||
471 | protected static function defaultDeleteHandler( |
||
486 | |||
487 | /** @noinspection PhpTooManyParametersInspection |
||
488 | * @param string $index |
||
489 | * @param string $jsonRelName |
||
490 | * @param string $modelRelName |
||
491 | * @param UriInterface $requestUri |
||
492 | * @param string $requestBody |
||
493 | * @param string $schemaClass |
||
494 | * @param ModelSchemaInfoInterface $schemaInfo |
||
495 | * @param JsonApiQueryParserInterface $queryParser |
||
496 | * @param JsonApiDataParserInterface $dataValidator |
||
497 | * @param CrudInterface $parentCrud |
||
498 | * @param EncoderInterface $encoder |
||
499 | * @param FactoryInterface $errorFactory |
||
500 | * @param FormatterFactoryInterface $formatterFactory |
||
501 | 2 | * |
|
502 | * @return ResponseInterface |
||
503 | * |
||
504 | * @SuppressWarnings(PHPMD.ExcessiveParameterList) |
||
505 | */ |
||
506 | protected static function defaultAddInRelationshipHandler( |
||
539 | |||
540 | /** @noinspection PhpTooManyParametersInspection |
||
541 | * @param string $index |
||
542 | * @param string $jsonRelName |
||
543 | * @param string $modelRelName |
||
544 | * @param UriInterface $requestUri |
||
545 | * @param string $requestBody |
||
546 | * @param string $schemaClass |
||
547 | * @param ModelSchemaInfoInterface $schemaInfo |
||
548 | * @param JsonApiQueryParserInterface $queryParser |
||
549 | * @param JsonApiDataParserInterface $dataValidator |
||
550 | * @param CrudInterface $parentCrud |
||
551 | * @param EncoderInterface $encoder |
||
552 | * @param FactoryInterface $errorFactory |
||
553 | * @param FormatterFactoryInterface $formatterFactory |
||
554 | 1 | * |
|
555 | * @return ResponseInterface |
||
556 | * |
||
557 | * @SuppressWarnings(PHPMD.ExcessiveParameterList) |
||
558 | */ |
||
559 | protected static function defaultDeleteInRelationshipHandler( |
||
592 | |||
593 | /** @noinspection PhpTooManyParametersInspection |
||
594 | * @param string $index |
||
595 | * @param string $jsonRelName |
||
596 | * @param string $modelRelName |
||
597 | * @param UriInterface $requestUri |
||
598 | * @param string $requestBody |
||
599 | * @param string $schemaClass |
||
600 | * @param ModelSchemaInfoInterface $schemaInfo |
||
601 | * @param JsonApiQueryParserInterface $queryParser |
||
602 | * @param JsonApiDataParserInterface $dataValidator |
||
603 | * @param CrudInterface $crud |
||
604 | * @param EncoderInterface $encoder |
||
605 | * @param FactoryInterface $errorFactory |
||
606 | * @param FormatterFactoryInterface $formatterFactory |
||
607 | 2 | * |
|
608 | * @return ResponseInterface |
||
609 | * |
||
610 | * @SuppressWarnings(PHPMD.ExcessiveParameterList) |
||
611 | */ |
||
612 | protected static function defaultReplaceInRelationship( |
||
648 | |||
649 | /** |
||
650 | * @param ContainerInterface $container |
||
651 | * @param string $rulesClass |
||
652 | * |
||
653 | 22 | * @return JsonApiQueryParserInterface |
|
654 | * |
||
655 | * @throws ContainerExceptionInterface |
||
656 | * @throws NotFoundExceptionInterface |
||
657 | 22 | */ |
|
658 | protected static function defaultCreateQueryParser( |
||
670 | |||
671 | /** |
||
672 | * @param ContainerInterface $container |
||
673 | * @param string $rulesClass |
||
674 | * |
||
675 | 13 | * @return JsonApiDataParserInterface |
|
676 | * |
||
677 | * @throws ContainerExceptionInterface |
||
678 | * @throws NotFoundExceptionInterface |
||
679 | 13 | */ |
|
680 | protected static function defaultCreateDataParser( |
||
692 | |||
693 | /** |
||
694 | * @param ContainerInterface $container |
||
695 | * @param string $rulesClass |
||
696 | * |
||
697 | 1 | * @return FormValidatorInterface |
|
698 | * |
||
699 | * @throws ContainerExceptionInterface |
||
700 | * @throws NotFoundExceptionInterface |
||
701 | 1 | */ |
|
702 | protected static function defaultCreateFormValidator( |
||
714 | |||
715 | /** |
||
716 | * @param ContainerInterface $container |
||
717 | * @param string $schemaClass |
||
718 | * |
||
719 | 16 | * @return ParametersMapperInterface |
|
720 | * |
||
721 | * @throws ContainerExceptionInterface |
||
722 | * @throws NotFoundExceptionInterface |
||
723 | 16 | */ |
|
724 | protected static function defaultCreateParameterMapper( |
||
739 | |||
740 | /** |
||
741 | 14 | * @param JsonApiQueryParserInterface $queryParser |
|
742 | * @param EncoderInterface $encoder |
||
743 | * |
||
744 | * @return void |
||
745 | 14 | */ |
|
746 | 3 | protected static function defaultApplyIncludesAndFieldSetsToEncoder( |
|
758 | |||
759 | /** |
||
760 | * @param ContainerInterface $container |
||
761 | * @param string|null $class |
||
762 | * |
||
763 | 30 | * @return CrudInterface |
|
764 | * |
||
765 | 30 | * @throws ContainerExceptionInterface |
|
766 | * @throws NotFoundExceptionInterface |
||
767 | */ |
||
768 | 30 | protected static function defaultCreateApi(ContainerInterface $container, string $class): CrudInterface |
|
778 | |||
779 | /** |
||
780 | 21 | * @param UriInterface $requestUri |
|
781 | * @param EncoderInterface $encoder |
||
782 | * |
||
783 | * @return ResponsesInterface |
||
784 | 21 | */ |
|
785 | 21 | protected static function defaultCreateResponses( |
|
797 | |||
798 | /** |
||
799 | * Developers can override the method in order to add/remove some data for `create`/`update` inputs. |
||
800 | * |
||
801 | * @param string $requestBody |
||
802 | 13 | * @param FactoryInterface $errorFactory |
|
803 | * @param FormatterFactoryInterface $formatterFactory |
||
804 | * |
||
805 | * @return array |
||
806 | */ |
||
807 | 13 | protected static function readJsonFromRequest( |
|
822 | |||
823 | /** |
||
824 | * Developers can override the method in order to use custom data mapping from a Schema to Model. |
||
825 | * |
||
826 | * @param iterable $captures |
||
827 | 6 | * @param string $schemaClass |
|
828 | * @param ModelSchemaInfoInterface $schemaInfo |
||
829 | * |
||
830 | * @return array |
||
831 | */ |
||
832 | 6 | protected static function mapSchemaDataToModelData( |
|
868 | |||
869 | /** |
||
870 | * @param mixed $model |
||
871 | 1 | * @param UriInterface $requestUri |
|
872 | * @param JsonSchemasInterface $jsonSchemas |
||
873 | * |
||
874 | * @return string |
||
875 | */ |
||
876 | 1 | protected static function defaultGetResourceUrl( |
|
888 | |||
889 | 30 | /** |
|
890 | * @param null|string $value |
||
891 | 30 | * |
|
892 | * @return void |
||
893 | */ |
||
894 | private static function assertClassValueDefined(?string $value): void |
||
898 | |||
899 | /** |
||
900 | 31 | * @param string $class |
|
901 | * @param string $interface |
||
902 | 31 | * |
|
903 | 31 | * @return void |
|
904 | 31 | */ |
|
905 | private static function assertClassImplements(string $class, string $interface): void |
||
912 | } |
||
913 |
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.