Complex classes like DataParser 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 DataParser, and based on these observations, apply Extract Interface, too.
1 | <?php namespace Limoncello\Flute\Validation\JsonApi; |
||
41 | class DataParser extends BaseValidator implements JsonApiDataValidatingParserInterface |
||
42 | { |
||
43 | use RelationshipsTrait; |
||
44 | |||
45 | /** Rule description index */ |
||
46 | const RULE_INDEX = 0; |
||
47 | |||
48 | /** Rule description index */ |
||
49 | const RULE_ATTRIBUTES = self::RULE_INDEX + 1; |
||
50 | |||
51 | /** Rule description index */ |
||
52 | const RULE_TO_ONE = self::RULE_ATTRIBUTES + 1; |
||
53 | |||
54 | /** Rule description index */ |
||
55 | const RULE_TO_MANY = self::RULE_TO_ONE + 1; |
||
56 | |||
57 | /** Rule description index */ |
||
58 | const RULE_UNLISTED_ATTRIBUTE = self::RULE_TO_MANY + 1; |
||
59 | |||
60 | /** Rule description index */ |
||
61 | const RULE_UNLISTED_RELATIONSHIP = self::RULE_UNLISTED_ATTRIBUTE + 1; |
||
62 | |||
63 | /** |
||
64 | * NOTE: Despite the type it is just a string so only static methods can be called from the interface. |
||
65 | * |
||
66 | * @var JsonApiDataRulesSerializerInterface|string |
||
67 | */ |
||
68 | private $serializerClass; |
||
69 | |||
70 | /** |
||
71 | * @var int |
||
72 | */ |
||
73 | private $errorStatus; |
||
74 | |||
75 | /** |
||
76 | * @var ContextStorageInterface |
||
77 | */ |
||
78 | private $context; |
||
79 | |||
80 | /** |
||
81 | * @var JsonApiErrorCollection |
||
82 | */ |
||
83 | private $jsonApiErrors; |
||
84 | |||
85 | /** |
||
86 | * @var array |
||
87 | */ |
||
88 | private $blocks; |
||
89 | |||
90 | /** |
||
91 | * @var array |
||
92 | */ |
||
93 | private $idRule; |
||
94 | |||
95 | /** |
||
96 | * @var array |
||
97 | */ |
||
98 | private $typeRule; |
||
99 | |||
100 | /** |
||
101 | * @var int[] |
||
102 | */ |
||
103 | private $attributeRules; |
||
104 | |||
105 | /** |
||
106 | * @var int[] |
||
107 | */ |
||
108 | private $toOneRules; |
||
109 | |||
110 | /** |
||
111 | * @var int[] |
||
112 | */ |
||
113 | private $toManyRules; |
||
114 | |||
115 | /** |
||
116 | * @var bool |
||
117 | */ |
||
118 | private $isIgnoreUnknowns; |
||
119 | |||
120 | /** |
||
121 | * @var FormatterInterface|null |
||
122 | */ |
||
123 | private $formatter; |
||
124 | |||
125 | /** |
||
126 | * @var FormatterFactoryInterface |
||
127 | */ |
||
128 | private $formatterFactory; |
||
129 | |||
130 | /** |
||
131 | * @param string $rulesClass |
||
132 | * @param string $serializerClass |
||
133 | * @param array $serializedData |
||
134 | * @param ContextStorageInterface $context |
||
135 | * @param JsonApiErrorCollection $jsonErrors |
||
136 | * @param FormatterFactoryInterface $formatterFactory |
||
137 | * @param int $errorStatus |
||
138 | * |
||
139 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
140 | */ |
||
141 | 21 | public function __construct( |
|
170 | |||
171 | /** |
||
172 | * @inheritdoc |
||
173 | */ |
||
174 | 12 | public function assert($jsonData): JsonApiDataValidatingParserInterface |
|
182 | |||
183 | /** |
||
184 | * @inheritdoc |
||
185 | */ |
||
186 | 12 | public function validate($input): bool |
|
198 | |||
199 | /** |
||
200 | * @inheritdoc |
||
201 | * |
||
202 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
203 | */ |
||
204 | 15 | public function parse(array $input): bool |
|
218 | |||
219 | /** |
||
220 | * @inheritdoc |
||
221 | */ |
||
222 | 4 | public function getJsonApiErrors(): array |
|
226 | |||
227 | /** |
||
228 | * @inheritdoc |
||
229 | */ |
||
230 | 9 | public function getJsonApiCaptures(): array |
|
234 | |||
235 | /** |
||
236 | * @return BaseValidator |
||
|
|||
237 | */ |
||
238 | 21 | protected function resetAggregators(): BaseValidator |
|
246 | |||
247 | /** |
||
248 | * @param string $serializerClass |
||
249 | * |
||
250 | * @return self |
||
251 | */ |
||
252 | 21 | protected function setSerializerClass(string $serializerClass): self |
|
263 | |||
264 | /** |
||
265 | * @return JsonApiDataRulesSerializerInterface|string |
||
266 | */ |
||
267 | 21 | protected function getSerializer() |
|
271 | |||
272 | /** |
||
273 | * @param array $jsonData |
||
274 | * |
||
275 | * @return self |
||
276 | * |
||
277 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
278 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
279 | */ |
||
280 | 15 | private function validateType(array $jsonData): self |
|
313 | |||
314 | /** |
||
315 | * @param array $jsonData |
||
316 | * |
||
317 | * @return self |
||
318 | * |
||
319 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
320 | */ |
||
321 | 15 | private function validateId(array $jsonData): self |
|
350 | |||
351 | /** |
||
352 | * @param array $jsonData |
||
353 | * |
||
354 | * @return self |
||
355 | * |
||
356 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
357 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
358 | */ |
||
359 | 15 | private function validateAttributes(array $jsonData): self |
|
400 | |||
401 | /** |
||
402 | * @param array $jsonData |
||
403 | * |
||
404 | * @return self |
||
405 | * |
||
406 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
407 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
408 | */ |
||
409 | 15 | private function validateRelationships(array $jsonData): self |
|
464 | |||
465 | /** |
||
466 | * @param int $index |
||
467 | * @param string $name |
||
468 | * @param mixed $mightBeRelationship |
||
469 | * |
||
470 | * @return void |
||
471 | * |
||
472 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
473 | */ |
||
474 | 8 | private function validateAsToOneRelationship(int $index, string $name, $mightBeRelationship): void |
|
488 | |||
489 | /** |
||
490 | * @param int $index |
||
491 | * @param string $name |
||
492 | * @param mixed $mightBeRelationship |
||
493 | * |
||
494 | * @return void |
||
495 | * |
||
496 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
497 | */ |
||
498 | 8 | private function validateAsToManyRelationship(int $index, string $name, $mightBeRelationship): void |
|
528 | |||
529 | /** |
||
530 | * @param mixed $data |
||
531 | * |
||
532 | * @return array|null|false Either `array` ($type => $id), or `null`, or `false` on error. |
||
533 | * |
||
534 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
535 | */ |
||
536 | 7 | private function parseSingleRelationship($data) |
|
553 | |||
554 | /** |
||
555 | * Re-initializes internal aggregators for captures, errors, etc. |
||
556 | */ |
||
557 | 15 | private function reInitAggregatorsIfNeeded(): void |
|
561 | |||
562 | /** |
||
563 | * @param mixed $input |
||
564 | * @param int $index |
||
565 | * |
||
566 | * @return void |
||
567 | * |
||
568 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
569 | */ |
||
570 | 14 | private function executeBlock($input, int $index): void |
|
581 | |||
582 | /** |
||
583 | * @param array $indexes |
||
584 | * |
||
585 | * @return void |
||
586 | * |
||
587 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
588 | */ |
||
589 | 15 | private function executeStarts(array $indexes): void |
|
598 | |||
599 | /** |
||
600 | * @param array $indexes |
||
601 | * |
||
602 | * @return void |
||
603 | * |
||
604 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
605 | */ |
||
606 | 15 | private function executeEnds(array $indexes): void |
|
615 | |||
616 | /** |
||
617 | * @param ErrorInterface $error |
||
618 | * |
||
619 | * @return string |
||
620 | */ |
||
621 | 3 | private function getMessage(ErrorInterface $error): string |
|
629 | |||
630 | /** |
||
631 | * @return array |
||
632 | */ |
||
633 | 15 | protected function getIdRule(): array |
|
637 | |||
638 | /** |
||
639 | * @return array |
||
640 | */ |
||
641 | 15 | protected function getTypeRule(): array |
|
645 | |||
646 | /** |
||
647 | * @return ContextStorageInterface |
||
648 | */ |
||
649 | 21 | protected function getContext(): ContextStorageInterface |
|
653 | |||
654 | /** |
||
655 | * @param ContextStorageInterface $context |
||
656 | * |
||
657 | * @return self |
||
658 | */ |
||
659 | 21 | protected function setContext(ContextStorageInterface $context): self |
|
665 | |||
666 | /** |
||
667 | * @return JsonApiErrorCollection |
||
668 | */ |
||
669 | 16 | protected function getJsonApiErrorCollection(): JsonApiErrorCollection |
|
673 | |||
674 | /** |
||
675 | * @param JsonApiErrorCollection $errors |
||
676 | * |
||
677 | * @return self |
||
678 | */ |
||
679 | 21 | protected function setJsonApiErrors(JsonApiErrorCollection $errors): self |
|
685 | |||
686 | /** |
||
687 | * @return int |
||
688 | */ |
||
689 | 10 | protected function getErrorStatus(): int |
|
693 | |||
694 | /** |
||
695 | * @return bool |
||
696 | */ |
||
697 | 1 | protected function isIgnoreUnknowns(): bool |
|
701 | |||
702 | /** |
||
703 | * @return self |
||
704 | */ |
||
705 | 1 | protected function enableIgnoreUnknowns(): self |
|
711 | |||
712 | /** |
||
713 | * @return self |
||
714 | */ |
||
715 | 21 | protected function disableIgnoreUnknowns(): self |
|
721 | |||
722 | /** |
||
723 | * @param array $rules |
||
724 | * |
||
725 | * @return self |
||
726 | */ |
||
727 | 21 | private function setAttributeRules(array $rules): self |
|
735 | |||
736 | /** |
||
737 | * @param array $rules |
||
738 | * |
||
739 | * @return self |
||
740 | */ |
||
741 | 21 | private function setToOneIndexes(array $rules): self |
|
749 | |||
750 | /** |
||
751 | * @param array $rules |
||
752 | * |
||
753 | * @return self |
||
754 | */ |
||
755 | 21 | private function setToManyIndexes(array $rules): self |
|
763 | |||
764 | /** |
||
765 | * @return int[] |
||
766 | */ |
||
767 | 15 | protected function getAttributeRules(): array |
|
771 | |||
772 | /** |
||
773 | * @return int[] |
||
774 | */ |
||
775 | 15 | protected function getToOneRules(): array |
|
779 | |||
780 | /** |
||
781 | * @return int[] |
||
782 | */ |
||
783 | 15 | protected function getToManyRules(): array |
|
787 | |||
788 | /** |
||
789 | * @return array |
||
790 | */ |
||
791 | 19 | private function getBlocks(): array |
|
795 | |||
796 | /** |
||
797 | * @return FormatterInterface |
||
798 | */ |
||
799 | 9 | protected function getFormatter(): FormatterInterface |
|
807 | |||
808 | /** |
||
809 | * @param FormatterFactoryInterface $formatterFactory |
||
810 | * |
||
811 | * @return self |
||
812 | */ |
||
813 | 21 | protected function setFormatterFactory(FormatterFactoryInterface $formatterFactory): self |
|
819 | |||
820 | /** |
||
821 | * @param string $name |
||
822 | * |
||
823 | * @return int|null |
||
824 | * |
||
825 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
826 | */ |
||
827 | 9 | private function getAttributeIndex(string $name): ?int |
|
834 | |||
835 | /** |
||
836 | * @param int $messageId |
||
837 | * @param array $args |
||
838 | * |
||
839 | * @return string |
||
840 | */ |
||
841 | 9 | private function formatMessage(int $messageId, array $args = []): string |
|
847 | |||
848 | /** |
||
849 | * @param array $rules |
||
850 | * |
||
851 | * @return bool |
||
852 | * |
||
853 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
854 | */ |
||
855 | 21 | private function debugCheckIndexesExist(array $rules): bool |
|
871 | } |
||
872 |
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.