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:
| 1 | <?php namespace Limoncello\Flute\Validation\Execution; |
||
| 31 | class JsonApiErrorCollection extends ErrorCollection |
||
| 32 | { |
||
| 33 | /** |
||
| 34 | * @var ContainerInterface |
||
| 35 | */ |
||
| 36 | private $container; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var int |
||
| 40 | */ |
||
| 41 | private $errorStatus; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var FormatterInterface|null |
||
| 45 | */ |
||
| 46 | private $messageFormatter; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @param ContainerInterface $container |
||
| 50 | * @param int $errorStatus |
||
| 51 | */ |
||
| 52 | public function __construct( |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @inheritdoc |
||
| 62 | */ |
||
| 63 | public function addValidationAttributeError(ErrorInterface $error) |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @inheritdoc |
||
| 72 | */ |
||
| 73 | public function addValidationRelationshipError(ErrorInterface $error) |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @return int |
||
| 82 | */ |
||
| 83 | protected function getErrorStatus(): int |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @return string |
||
| 90 | */ |
||
| 91 | private function getInvalidValueMessage(): string |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @param ErrorInterface $error |
||
| 100 | * |
||
| 101 | * @return string |
||
| 102 | */ |
||
| 103 | View Code Duplication | private function getValidationMessage(ErrorInterface $error): string |
|
| 111 | |||
| 112 | /** |
||
| 113 | * @return FormatterInterface |
||
| 114 | */ |
||
| 115 | View Code Duplication | private function getMessageFormatter(): FormatterInterface |
|
| 125 | |||
| 126 | /** |
||
| 127 | * @return ContainerInterface |
||
| 128 | */ |
||
| 129 | private function getContainer(): ContainerInterface |
||
| 133 | } |
||
| 134 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.