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 RelationshipRulesTrait; |
||
| 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 | 26 | public function __construct( |
|
| 170 | |||
| 171 | /** |
||
| 172 | * @inheritdoc |
||
| 173 | */ |
||
| 174 | 11 | public function assert($jsonData): JsonApiDataValidatingParserInterface |
|
| 182 | |||
| 183 | /** |
||
| 184 | * @inheritdoc |
||
| 185 | */ |
||
| 186 | 11 | public function validate($input): bool |
|
| 198 | |||
| 199 | /** |
||
| 200 | * @inheritdoc |
||
| 201 | * |
||
| 202 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
| 203 | */ |
||
| 204 | 14 | public function parse(array $input): bool |
|
| 218 | |||
| 219 | /** |
||
| 220 | * @inheritdoc |
||
| 221 | * |
||
| 222 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
| 223 | */ |
||
| 224 | 7 | public function parseRelationship(string $name, array $jsonData): bool |
|
| 264 | |||
| 265 | /** |
||
| 266 | * @inheritdoc |
||
| 267 | */ |
||
| 268 | 7 | public function assertRelationship(string $name, array $jsonData): JsonApiDataValidatingParserInterface |
|
| 276 | |||
| 277 | /** |
||
| 278 | * @inheritdoc |
||
| 279 | */ |
||
| 280 | 6 | public function getJsonApiErrors(): array |
|
| 284 | |||
| 285 | /** |
||
| 286 | * @inheritdoc |
||
| 287 | */ |
||
| 288 | 13 | public function getJsonApiCaptures(): array |
|
| 292 | |||
| 293 | /** |
||
| 294 | * @return BaseValidator |
||
|
|
|||
| 295 | */ |
||
| 296 | 26 | protected function resetAggregators(): BaseValidator |
|
| 304 | |||
| 305 | /** |
||
| 306 | * @param string $serializerClass |
||
| 307 | * |
||
| 308 | * @return self |
||
| 309 | */ |
||
| 310 | 26 | protected function setSerializerClass(string $serializerClass): self |
|
| 321 | |||
| 322 | /** |
||
| 323 | * @return JsonApiDataRulesSerializerInterface|string |
||
| 324 | */ |
||
| 325 | 26 | protected function getSerializer() |
|
| 329 | |||
| 330 | /** |
||
| 331 | * @param array $jsonData |
||
| 332 | * |
||
| 333 | * @return self |
||
| 334 | * |
||
| 335 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 336 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
| 337 | */ |
||
| 338 | 14 | private function validateType(array $jsonData): self |
|
| 371 | |||
| 372 | /** |
||
| 373 | * @param array $jsonData |
||
| 374 | * |
||
| 375 | * @return self |
||
| 376 | * |
||
| 377 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 378 | */ |
||
| 379 | 14 | private function validateId(array $jsonData): self |
|
| 408 | |||
| 409 | /** |
||
| 410 | * @param array $jsonData |
||
| 411 | * |
||
| 412 | * @return self |
||
| 413 | * |
||
| 414 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 415 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
| 416 | */ |
||
| 417 | 14 | private function validateAttributes(array $jsonData): self |
|
| 458 | |||
| 459 | /** |
||
| 460 | * @param array $jsonData |
||
| 461 | * |
||
| 462 | * @return self |
||
| 463 | * |
||
| 464 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 465 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
| 466 | */ |
||
| 467 | 14 | private function validateRelationships(array $jsonData): self |
|
| 522 | |||
| 523 | /** |
||
| 524 | * @param int $index |
||
| 525 | * @param string $name |
||
| 526 | * @param mixed $mightBeRelationship |
||
| 527 | * |
||
| 528 | * @return void |
||
| 529 | * |
||
| 530 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
| 531 | */ |
||
| 532 | 10 | private function validateAsToOneRelationship(int $index, string $name, $mightBeRelationship): void |
|
| 546 | |||
| 547 | /** |
||
| 548 | * @param int $index |
||
| 549 | * @param string $name |
||
| 550 | * @param mixed $mightBeRelationship |
||
| 551 | * |
||
| 552 | * @return void |
||
| 553 | * |
||
| 554 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
| 555 | */ |
||
| 556 | 12 | private function validateAsToManyRelationship(int $index, string $name, $mightBeRelationship): void |
|
| 586 | |||
| 587 | /** |
||
| 588 | * @param mixed $data |
||
| 589 | * |
||
| 590 | * @return array|null|false Either `array` ($type => $id), or `null`, or `false` on error. |
||
| 591 | * |
||
| 592 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
| 593 | */ |
||
| 594 | 13 | private function parseSingleRelationship($data) |
|
| 611 | |||
| 612 | /** |
||
| 613 | * Re-initializes internal aggregators for captures, errors, etc. |
||
| 614 | */ |
||
| 615 | 21 | private function reInitAggregatorsIfNeeded(): void |
|
| 619 | |||
| 620 | /** |
||
| 621 | * @param mixed $input |
||
| 622 | * @param int $index |
||
| 623 | * |
||
| 624 | * @return void |
||
| 625 | * |
||
| 626 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 627 | */ |
||
| 628 | 19 | private function executeBlock($input, int $index): void |
|
| 639 | |||
| 640 | /** |
||
| 641 | * @param array $indexes |
||
| 642 | * |
||
| 643 | * @return void |
||
| 644 | * |
||
| 645 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 646 | */ |
||
| 647 | 20 | private function executeStarts(array $indexes): void |
|
| 656 | |||
| 657 | /** |
||
| 658 | * @param array $indexes |
||
| 659 | * |
||
| 660 | * @return void |
||
| 661 | * |
||
| 662 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 663 | */ |
||
| 664 | 20 | private function executeEnds(array $indexes): void |
|
| 673 | |||
| 674 | /** |
||
| 675 | * @param ErrorInterface $error |
||
| 676 | * |
||
| 677 | * @return string |
||
| 678 | */ |
||
| 679 | 3 | private function getMessage(ErrorInterface $error): string |
|
| 687 | |||
| 688 | /** |
||
| 689 | * @return array |
||
| 690 | */ |
||
| 691 | 14 | protected function getIdRule(): array |
|
| 695 | |||
| 696 | /** |
||
| 697 | * @return array |
||
| 698 | */ |
||
| 699 | 14 | protected function getTypeRule(): array |
|
| 703 | |||
| 704 | /** |
||
| 705 | * @return ContextStorageInterface |
||
| 706 | */ |
||
| 707 | 26 | protected function getContext(): ContextStorageInterface |
|
| 711 | |||
| 712 | /** |
||
| 713 | * @param ContextStorageInterface $context |
||
| 714 | * |
||
| 715 | * @return self |
||
| 716 | */ |
||
| 717 | 26 | protected function setContext(ContextStorageInterface $context): self |
|
| 723 | |||
| 724 | /** |
||
| 725 | * @return JsonApiErrorCollection |
||
| 726 | */ |
||
| 727 | 22 | protected function getJsonApiErrorCollection(): JsonApiErrorCollection |
|
| 731 | |||
| 732 | /** |
||
| 733 | * @param JsonApiErrorCollection $errors |
||
| 734 | * |
||
| 735 | * @return self |
||
| 736 | */ |
||
| 737 | 26 | protected function setJsonApiErrors(JsonApiErrorCollection $errors): self |
|
| 743 | |||
| 744 | /** |
||
| 745 | * @return int |
||
| 746 | */ |
||
| 747 | 12 | protected function getErrorStatus(): int |
|
| 751 | |||
| 752 | /** |
||
| 753 | * @return bool |
||
| 754 | */ |
||
| 755 | 1 | protected function isIgnoreUnknowns(): bool |
|
| 759 | |||
| 760 | /** |
||
| 761 | * @return self |
||
| 762 | */ |
||
| 763 | 1 | protected function enableIgnoreUnknowns(): self |
|
| 769 | |||
| 770 | /** |
||
| 771 | * @return self |
||
| 772 | */ |
||
| 773 | 26 | protected function disableIgnoreUnknowns(): self |
|
| 779 | |||
| 780 | /** |
||
| 781 | * @param array $rules |
||
| 782 | * |
||
| 783 | * @return self |
||
| 784 | */ |
||
| 785 | 26 | private function setAttributeRules(array $rules): self |
|
| 793 | |||
| 794 | /** |
||
| 795 | * @param array $rules |
||
| 796 | * |
||
| 797 | * @return self |
||
| 798 | */ |
||
| 799 | 26 | private function setToOneIndexes(array $rules): self |
|
| 807 | |||
| 808 | /** |
||
| 809 | * @param array $rules |
||
| 810 | * |
||
| 811 | * @return self |
||
| 812 | */ |
||
| 813 | 26 | private function setToManyIndexes(array $rules): self |
|
| 821 | |||
| 822 | /** |
||
| 823 | * @return int[] |
||
| 824 | */ |
||
| 825 | 14 | protected function getAttributeRules(): array |
|
| 829 | |||
| 830 | /** |
||
| 831 | * @return int[] |
||
| 832 | */ |
||
| 833 | 21 | protected function getToOneRules(): array |
|
| 837 | |||
| 838 | /** |
||
| 839 | * @return int[] |
||
| 840 | */ |
||
| 841 | 19 | protected function getToManyRules(): array |
|
| 845 | |||
| 846 | /** |
||
| 847 | * @return array |
||
| 848 | */ |
||
| 849 | 23 | private function getBlocks(): array |
|
| 853 | |||
| 854 | /** |
||
| 855 | * @return FormatterInterface |
||
| 856 | */ |
||
| 857 | 10 | protected function getFormatter(): FormatterInterface |
|
| 865 | |||
| 866 | /** |
||
| 867 | * @param FormatterFactoryInterface $formatterFactory |
||
| 868 | * |
||
| 869 | * @return self |
||
| 870 | */ |
||
| 871 | 26 | protected function setFormatterFactory(FormatterFactoryInterface $formatterFactory): self |
|
| 877 | |||
| 878 | /** |
||
| 879 | * @param string $name |
||
| 880 | * |
||
| 881 | * @return int|null |
||
| 882 | * |
||
| 883 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 884 | */ |
||
| 885 | 8 | private function getAttributeIndex(string $name): ?int |
|
| 892 | |||
| 893 | /** |
||
| 894 | * @param int $messageId |
||
| 895 | * @param array $args |
||
| 896 | * |
||
| 897 | * @return string |
||
| 898 | */ |
||
| 899 | 10 | private function formatMessage(int $messageId, array $args = []): string |
|
| 905 | |||
| 906 | /** |
||
| 907 | * @param array $rules |
||
| 908 | * |
||
| 909 | * @return bool |
||
| 910 | * |
||
| 911 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 912 | */ |
||
| 913 | 26 | private function debugCheckIndexesExist(array $rules): bool |
|
| 929 | } |
||
| 930 |
This check looks for the generic type
arrayas a return type and suggests a more specific type. This type is inferred from the actual code.