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\JsonApi\Execution; |
||
28 | class JsonApiErrorCollection extends ErrorCollection |
||
29 | { |
||
30 | /** |
||
31 | * @var FormatterInterface |
||
32 | */ |
||
33 | private $messageFormatter; |
||
34 | |||
35 | /** |
||
36 | * @param FormatterInterface $formatter |
||
37 | */ |
||
38 | 60 | public function __construct(FormatterInterface $formatter) |
|
42 | |||
43 | /** |
||
44 | * @inheritdoc |
||
45 | */ |
||
46 | 1 | View Code Duplication | public function addValidationIdError( |
54 | |||
55 | /** |
||
56 | * @inheritdoc |
||
57 | */ |
||
58 | 1 | View Code Duplication | public function addValidationTypeError( |
66 | |||
67 | /** |
||
68 | * @inheritdoc |
||
69 | */ |
||
70 | 2 | View Code Duplication | public function addValidationAttributeError( |
78 | |||
79 | /** |
||
80 | * @inheritdoc |
||
81 | */ |
||
82 | 4 | View Code Duplication | public function addValidationRelationshipError( |
90 | |||
91 | /** |
||
92 | * @inheritdoc |
||
93 | */ |
||
94 | 6 | View Code Duplication | public function addValidationQueryError( |
103 | |||
104 | /** |
||
105 | * |
||
106 | * @return string |
||
107 | */ |
||
108 | 11 | private function getInvalidValueMessage(): string |
|
114 | |||
115 | /** |
||
116 | * @param ErrorInterface $error |
||
117 | * |
||
118 | * @return string |
||
119 | */ |
||
120 | 11 | private function getValidationMessage(ErrorInterface $error): string |
|
128 | |||
129 | /** |
||
130 | * @return FormatterInterface |
||
131 | */ |
||
132 | 11 | private function getMessageFormatter(): FormatterInterface |
|
136 | } |
||
137 |
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.