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 | 21 | public function __construct( |
|
| 155 | |||
| 156 | /** |
||
| 157 | * @inheritdoc |
||
| 158 | */ |
||
| 159 | 12 | public function assert($jsonData): JsonApiValidatorInterface |
|
| 167 | |||
| 168 | /** |
||
| 169 | * @inheritdoc |
||
| 170 | * |
||
| 171 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
| 172 | */ |
||
| 173 | 18 | public function validate($input): bool |
|
| 193 | |||
| 194 | /** |
||
| 195 | * @inheritdoc |
||
| 196 | */ |
||
| 197 | 6 | public function getJsonApiErrors(): array |
|
| 201 | |||
| 202 | /** |
||
| 203 | * @inheritdoc |
||
| 204 | */ |
||
| 205 | 11 | public function getJsonApiCaptures(): array |
|
| 209 | |||
| 210 | /** |
||
| 211 | * @return BaseValidator |
||
|
|
|||
| 212 | */ |
||
| 213 | 21 | protected function resetAggregators(): BaseValidator |
|
| 222 | |||
| 223 | /** |
||
| 224 | * @param array $jsonData |
||
| 225 | * |
||
| 226 | * @return self |
||
| 227 | * |
||
| 228 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 229 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
| 230 | */ |
||
| 231 | 17 | private function validateType(array $jsonData): self |
|
| 264 | |||
| 265 | /** |
||
| 266 | * @param array $jsonData |
||
| 267 | * |
||
| 268 | * @return self |
||
| 269 | * |
||
| 270 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 271 | */ |
||
| 272 | 17 | private function validateId(array $jsonData): self |
|
| 301 | |||
| 302 | /** |
||
| 303 | * @param array $jsonData |
||
| 304 | * |
||
| 305 | * @return self |
||
| 306 | * |
||
| 307 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 308 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
| 309 | */ |
||
| 310 | 17 | private function validateAttributes(array $jsonData): self |
|
| 351 | |||
| 352 | /** |
||
| 353 | * @param array $jsonData |
||
| 354 | * |
||
| 355 | * @return self |
||
| 356 | * |
||
| 357 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 358 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
| 359 | */ |
||
| 360 | 17 | private function validateRelationships(array $jsonData): self |
|
| 415 | |||
| 416 | /** |
||
| 417 | * @param int $index |
||
| 418 | * @param string $name |
||
| 419 | * @param mixed $mightBeRelationship |
||
| 420 | * |
||
| 421 | * @return void |
||
| 422 | * |
||
| 423 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
| 424 | */ |
||
| 425 | 8 | private function validateAsToOneRelationship(int $index, string $name, $mightBeRelationship): void |
|
| 439 | |||
| 440 | /** |
||
| 441 | * @param int $index |
||
| 442 | * @param string $name |
||
| 443 | * @param mixed $mightBeRelationship |
||
| 444 | * |
||
| 445 | * @return void |
||
| 446 | * |
||
| 447 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
| 448 | */ |
||
| 449 | 8 | private function validateAsToManyRelationship(int $index, string $name, $mightBeRelationship): void |
|
| 479 | |||
| 480 | /** |
||
| 481 | * @param mixed $data |
||
| 482 | * |
||
| 483 | * @return array|null|false Either `array` ($type => $id), or `null`, or `false` on error. |
||
| 484 | * |
||
| 485 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
| 486 | */ |
||
| 487 | 7 | private function parseSingleRelationship($data) |
|
| 504 | |||
| 505 | /** |
||
| 506 | * Re-initializes internal aggregators for captures, errors, etc. |
||
| 507 | */ |
||
| 508 | 18 | private function reInitAggregatorsIfNeeded(): void |
|
| 512 | |||
| 513 | /** |
||
| 514 | * @param mixed $input |
||
| 515 | * @param int $index |
||
| 516 | * |
||
| 517 | * @return void |
||
| 518 | * |
||
| 519 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 520 | */ |
||
| 521 | 16 | private function executeBlock($input, int $index): void |
|
| 532 | |||
| 533 | /** |
||
| 534 | * @param array $indexes |
||
| 535 | * |
||
| 536 | * @return void |
||
| 537 | * |
||
| 538 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 539 | */ |
||
| 540 | 17 | private function executeStarts(array $indexes): void |
|
| 544 | |||
| 545 | /** |
||
| 546 | * @param array $indexes |
||
| 547 | * |
||
| 548 | * @return void |
||
| 549 | * |
||
| 550 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 551 | */ |
||
| 552 | 17 | private function executeEnds(array $indexes): void |
|
| 556 | |||
| 557 | /** |
||
| 558 | * @param ErrorInterface $error |
||
| 559 | * |
||
| 560 | * @return string |
||
| 561 | */ |
||
| 562 | 3 | View Code Duplication | private function getMessage(ErrorInterface $error): string |
| 570 | |||
| 571 | /** |
||
| 572 | * @return array |
||
| 573 | */ |
||
| 574 | 17 | protected function getIdRule(): array |
|
| 578 | |||
| 579 | /** |
||
| 580 | * @return array |
||
| 581 | */ |
||
| 582 | 17 | protected function getTypeRule(): array |
|
| 586 | |||
| 587 | /** |
||
| 588 | * @return ContextStorageInterface |
||
| 589 | */ |
||
| 590 | 17 | protected function getContextStorage(): ContextStorageInterface |
|
| 594 | |||
| 595 | /** |
||
| 596 | * @return ContextStorageInterface |
||
| 597 | */ |
||
| 598 | 21 | protected function createContextStorage(): ContextStorageInterface |
|
| 602 | |||
| 603 | /** |
||
| 604 | * @return JsonApiErrorCollection |
||
| 605 | */ |
||
| 606 | 18 | protected function getJsonApiErrorCollection(): JsonApiErrorCollection |
|
| 610 | |||
| 611 | /** |
||
| 612 | * @return JsonApiErrorCollection |
||
| 613 | */ |
||
| 614 | 21 | protected function createJsonApiErrors(): JsonApiErrorCollection |
|
| 618 | |||
| 619 | /** |
||
| 620 | * @return int |
||
| 621 | */ |
||
| 622 | 21 | protected function getErrorStatus(): int |
|
| 626 | |||
| 627 | /** |
||
| 628 | * @return bool |
||
| 629 | */ |
||
| 630 | 1 | protected function isIgnoreUnknowns(): bool |
|
| 634 | |||
| 635 | /** |
||
| 636 | * @return self |
||
| 637 | */ |
||
| 638 | 1 | protected function enableIgnoreUnknowns(): self |
|
| 644 | |||
| 645 | /** |
||
| 646 | * @return self |
||
| 647 | */ |
||
| 648 | 21 | protected function disableIgnoreUnknowns(): self |
|
| 654 | |||
| 655 | /** |
||
| 656 | * @param array $rules |
||
| 657 | * |
||
| 658 | * @return self |
||
| 659 | */ |
||
| 660 | 21 | private function setAttributeRules(array $rules): self |
|
| 668 | |||
| 669 | /** |
||
| 670 | * @param array $rules |
||
| 671 | * |
||
| 672 | * @return self |
||
| 673 | */ |
||
| 674 | 21 | private function setToOneIndexes(array $rules): self |
|
| 682 | |||
| 683 | /** |
||
| 684 | * @param array $rules |
||
| 685 | * |
||
| 686 | * @return self |
||
| 687 | */ |
||
| 688 | 21 | private function setToManyIndexes(array $rules): self |
|
| 696 | |||
| 697 | /** |
||
| 698 | * @return int[] |
||
| 699 | */ |
||
| 700 | 17 | protected function getAttributeRules(): array |
|
| 704 | |||
| 705 | /** |
||
| 706 | * @return int[] |
||
| 707 | */ |
||
| 708 | 17 | protected function getToOneRules(): array |
|
| 712 | |||
| 713 | /** |
||
| 714 | * @return int[] |
||
| 715 | */ |
||
| 716 | 17 | protected function getToManyRules(): array |
|
| 720 | |||
| 721 | /** |
||
| 722 | * @return array |
||
| 723 | */ |
||
| 724 | 21 | private function getBlocks(): array |
|
| 728 | |||
| 729 | /** |
||
| 730 | * @return FormatterInterface |
||
| 731 | */ |
||
| 732 | 9 | protected function getMessageFormatter(): FormatterInterface |
|
| 742 | |||
| 743 | /** |
||
| 744 | * @param string $name |
||
| 745 | * |
||
| 746 | * @return int |
||
| 747 | * |
||
| 748 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 749 | */ |
||
| 750 | 10 | private function getAttributeIndex(string $name): int |
|
| 757 | |||
| 758 | /** |
||
| 759 | * @param string $name |
||
| 760 | * |
||
| 761 | * @return bool |
||
| 762 | * |
||
| 763 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 764 | */ |
||
| 765 | 11 | private function hasAttributeIndex(string $name): bool |
|
| 772 | |||
| 773 | /** |
||
| 774 | * @param int $messageId |
||
| 775 | * @param array $args |
||
| 776 | * |
||
| 777 | * @return string |
||
| 778 | */ |
||
| 779 | 9 | private function formatMessage(int $messageId, array $args = []): string |
|
| 785 | |||
| 786 | /** |
||
| 787 | * @param array $rules |
||
| 788 | * |
||
| 789 | * @return bool |
||
| 790 | * |
||
| 791 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 792 | */ |
||
| 793 | 21 | private function debugCheckIndexesExist(array $rules): bool |
|
| 809 | } |
||
| 810 |
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.