Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Validator 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 Validator, and based on these observations, apply Extract Interface, too.
| 1 | <?php namespace Limoncello\Flute\Validation; |
||
| 43 | class Validator extends BaseValidator implements JsonApiValidatorInterface |
||
| 44 | { |
||
| 45 | use RelationshipsTrait, HasContainerTrait; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Namespace for string resources. |
||
| 49 | */ |
||
| 50 | const RESOURCES_NAMESPACE = 'Limoncello.Flute.Validation'; |
||
| 51 | |||
| 52 | /** Rule description index */ |
||
| 53 | const RULE_INDEX = 0; |
||
| 54 | |||
| 55 | /** Rule description index */ |
||
| 56 | const RULE_ATTRIBUTES = self::RULE_INDEX + 1; |
||
| 57 | |||
| 58 | /** Rule description index */ |
||
| 59 | const RULE_TO_ONE = self::RULE_ATTRIBUTES + 1; |
||
| 60 | |||
| 61 | /** Rule description index */ |
||
| 62 | const RULE_TO_MANY = self::RULE_TO_ONE + 1; |
||
| 63 | |||
| 64 | /** Rule description index */ |
||
| 65 | const RULE_UNLISTED_ATTRIBUTE = self::RULE_TO_MANY + 1; |
||
| 66 | |||
| 67 | /** Rule description index */ |
||
| 68 | const RULE_UNLISTED_RELATIONSHIP = self::RULE_UNLISTED_ATTRIBUTE + 1; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var int |
||
| 72 | */ |
||
| 73 | private $errorStatus; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var ContextStorageInterface |
||
| 77 | */ |
||
| 78 | private $contextStorage; |
||
| 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 $messageFormatter; |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @param string $name |
||
| 127 | * @param array $data |
||
| 128 | * @param ContainerInterface $container |
||
| 129 | * @param int $errorStatus |
||
| 130 | * |
||
| 131 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 132 | */ |
||
| 133 | public function __construct( |
||
| 155 | |||
| 156 | 18 | /** |
|
| 157 | * @inheritdoc |
||
| 158 | */ |
||
| 159 | public function assert($jsonData): JsonApiValidatorInterface |
||
| 167 | |||
| 168 | 7 | /** |
|
| 169 | * @inheritdoc |
||
| 170 | */ |
||
| 171 | public function validate($input): bool |
||
| 191 | |||
| 192 | 4 | /** |
|
| 193 | * @inheritdoc |
||
| 194 | 4 | */ |
|
| 195 | public function getJsonApiErrors(): array |
||
| 199 | |||
| 200 | 10 | /** |
|
| 201 | * @inheritdoc |
||
| 202 | 10 | */ |
|
| 203 | public function getJsonApiCaptures(): array |
||
| 207 | |||
| 208 | 18 | /** |
|
| 209 | * @return BaseValidator |
||
|
|
|||
| 210 | 18 | */ |
|
| 211 | protected function resetAggregators(): BaseValidator |
||
| 220 | |||
| 221 | /** |
||
| 222 | * @param array $jsonData |
||
| 223 | * |
||
| 224 | * @return self |
||
| 225 | * |
||
| 226 | 16 | * @SuppressWarnings(PHPMD.StaticAccess) |
|
| 227 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
| 228 | */ |
||
| 229 | 16 | private function validateType(array $jsonData): self |
|
| 262 | |||
| 263 | /** |
||
| 264 | * @param array $jsonData |
||
| 265 | * |
||
| 266 | * @return self |
||
| 267 | 16 | * |
|
| 268 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 269 | */ |
||
| 270 | 16 | private function validateId(array $jsonData): self |
|
| 299 | |||
| 300 | /** |
||
| 301 | * @param array $jsonData |
||
| 302 | * |
||
| 303 | * @return self |
||
| 304 | * |
||
| 305 | 16 | * @SuppressWarnings(PHPMD.StaticAccess) |
|
| 306 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
| 307 | */ |
||
| 308 | 16 | private function validateAttributes(array $jsonData): self |
|
| 349 | |||
| 350 | /** |
||
| 351 | * @param array $jsonData |
||
| 352 | * |
||
| 353 | * @return self |
||
| 354 | * |
||
| 355 | 16 | * @SuppressWarnings(PHPMD.StaticAccess) |
|
| 356 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
| 357 | */ |
||
| 358 | 16 | private function validateRelationships(array $jsonData): self |
|
| 413 | |||
| 414 | /** |
||
| 415 | * @param int $index |
||
| 416 | * @param string $name |
||
| 417 | * @param mixed $mightBeRelationship |
||
| 418 | * |
||
| 419 | * @return void |
||
| 420 | 8 | * |
|
| 421 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
| 422 | 8 | */ |
|
| 423 | 8 | private function validateAsToOneRelationship(int $index, string $name, $mightBeRelationship): void |
|
| 437 | |||
| 438 | /** |
||
| 439 | * @param int $index |
||
| 440 | * @param string $name |
||
| 441 | * @param mixed $mightBeRelationship |
||
| 442 | * |
||
| 443 | * @return void |
||
| 444 | 8 | * |
|
| 445 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
| 446 | 8 | */ |
|
| 447 | 8 | private function validateAsToManyRelationship(int $index, string $name, $mightBeRelationship): void |
|
| 477 | |||
| 478 | /** |
||
| 479 | * @param mixed $data |
||
| 480 | * |
||
| 481 | * @return array|null|false Either `array` ($type => $id), or `null`, or `false` on error. |
||
| 482 | 7 | * |
|
| 483 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
| 484 | 7 | */ |
|
| 485 | 2 | private function parseSingleRelationship($data) |
|
| 502 | |||
| 503 | 16 | /** |
|
| 504 | * Re-initializes internal aggregators for captures, errors, etc. |
||
| 505 | 16 | */ |
|
| 506 | private function reInitAggregatorsIfNeeded(): void |
||
| 510 | |||
| 511 | /** |
||
| 512 | * @param mixed $input |
||
| 513 | * @param int $index |
||
| 514 | * |
||
| 515 | * @return void |
||
| 516 | 15 | * |
|
| 517 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 518 | 15 | */ |
|
| 519 | 15 | private function executeBlock($input, int $index): void |
|
| 530 | |||
| 531 | /** |
||
| 532 | * @param array $indexes |
||
| 533 | * |
||
| 534 | * @return void |
||
| 535 | 16 | * |
|
| 536 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 537 | 16 | */ |
|
| 538 | private function executeStarts(array $indexes): void |
||
| 542 | |||
| 543 | /** |
||
| 544 | * @param array $indexes |
||
| 545 | * |
||
| 546 | * @return void |
||
| 547 | 16 | * |
|
| 548 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 549 | 16 | */ |
|
| 550 | private function executeEnds(array $indexes): void |
||
| 554 | |||
| 555 | /** |
||
| 556 | * @param ErrorInterface $error |
||
| 557 | 3 | * |
|
| 558 | * @return string |
||
| 559 | 3 | */ |
|
| 560 | 3 | View Code Duplication | private function getMessage(ErrorInterface $error): string |
| 568 | |||
| 569 | 16 | /** |
|
| 570 | * @return array |
||
| 571 | 16 | */ |
|
| 572 | protected function getIdRule(): array |
||
| 576 | |||
| 577 | 16 | /** |
|
| 578 | * @return array |
||
| 579 | 16 | */ |
|
| 580 | protected function getTypeRule(): array |
||
| 584 | |||
| 585 | 16 | /** |
|
| 586 | * @return ContextStorageInterface |
||
| 587 | 16 | */ |
|
| 588 | protected function getContextStorage(): ContextStorageInterface |
||
| 592 | |||
| 593 | 18 | /** |
|
| 594 | * @return ContextStorageInterface |
||
| 595 | 18 | */ |
|
| 596 | protected function createContextStorage(): ContextStorageInterface |
||
| 600 | |||
| 601 | 16 | /** |
|
| 602 | * @return JsonApiErrorCollection |
||
| 603 | 16 | */ |
|
| 604 | protected function getJsonApiErrorCollection(): JsonApiErrorCollection |
||
| 608 | |||
| 609 | 18 | /** |
|
| 610 | * @return JsonApiErrorCollection |
||
| 611 | 18 | */ |
|
| 612 | protected function createJsonApiErrors(): JsonApiErrorCollection |
||
| 616 | |||
| 617 | 18 | /** |
|
| 618 | * @return int |
||
| 619 | 18 | */ |
|
| 620 | protected function getErrorStatus(): int |
||
| 624 | |||
| 625 | 18 | /** |
|
| 626 | * @return bool |
||
| 627 | 18 | */ |
|
| 628 | protected function isIgnoreUnknowns(): bool |
||
| 632 | |||
| 633 | 1 | /** |
|
| 634 | * @return self |
||
| 635 | 1 | */ |
|
| 636 | protected function enableIgnoreUnknowns(): self |
||
| 642 | |||
| 643 | 1 | /** |
|
| 644 | * @return self |
||
| 645 | 1 | */ |
|
| 646 | protected function disableIgnoreUnknowns(): self |
||
| 652 | |||
| 653 | 18 | /** |
|
| 654 | * @param array $rules |
||
| 655 | 18 | * |
|
| 656 | * @return self |
||
| 657 | */ |
||
| 658 | private function setAttributeRules(array $rules): self |
||
| 666 | |||
| 667 | 18 | /** |
|
| 668 | * @param array $rules |
||
| 669 | 18 | * |
|
| 670 | * @return self |
||
| 671 | */ |
||
| 672 | private function setToOneIndexes(array $rules): self |
||
| 680 | |||
| 681 | 18 | /** |
|
| 682 | * @param array $rules |
||
| 683 | 18 | * |
|
| 684 | * @return self |
||
| 685 | */ |
||
| 686 | private function setToManyIndexes(array $rules): self |
||
| 694 | |||
| 695 | 18 | /** |
|
| 696 | * @return int[] |
||
| 697 | 18 | */ |
|
| 698 | protected function getAttributeRules(): array |
||
| 702 | |||
| 703 | 16 | /** |
|
| 704 | * @return int[] |
||
| 705 | 16 | */ |
|
| 706 | protected function getToOneRules(): array |
||
| 710 | |||
| 711 | 16 | /** |
|
| 712 | * @return int[] |
||
| 713 | 16 | */ |
|
| 714 | protected function getToManyRules(): array |
||
| 718 | |||
| 719 | 16 | /** |
|
| 720 | * @return array |
||
| 721 | 16 | */ |
|
| 722 | private function getBlocks(): array |
||
| 726 | |||
| 727 | 18 | /** |
|
| 728 | * @return FormatterInterface |
||
| 729 | 18 | */ |
|
| 730 | protected function getMessageFormatter(): FormatterInterface |
||
| 740 | 8 | ||
| 741 | /** |
||
| 742 | * @param string $name |
||
| 743 | 8 | * |
|
| 744 | * @return int |
||
| 745 | * |
||
| 746 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 747 | */ |
||
| 748 | private function getAttributeIndex(string $name): int |
||
| 755 | 9 | ||
| 756 | 9 | /** |
|
| 757 | * @param string $name |
||
| 758 | 9 | * |
|
| 759 | * @return bool |
||
| 760 | * |
||
| 761 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 762 | */ |
||
| 763 | private function hasAttributeIndex(string $name): bool |
||
| 770 | 10 | ||
| 771 | 10 | /** |
|
| 772 | * @param int $messageId |
||
| 773 | 10 | * @param array $args |
|
| 774 | * |
||
| 775 | * @return string |
||
| 776 | */ |
||
| 777 | private function formatMessage(int $messageId, array $args = []): string |
||
| 783 | |||
| 784 | 8 | /** |
|
| 785 | * @param array $rules |
||
| 786 | 8 | * |
|
| 787 | * @return bool |
||
| 788 | * |
||
| 789 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 790 | */ |
||
| 791 | private function debugCheckIndexesExist(array $rules): bool |
||
| 807 | } |
||
| 808 |
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.