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 |
||
22 | class OrganizerCommandHandler implements CommandHandlerInterface |
||
23 | { |
||
24 | /** |
||
25 | * @var RepositoryInterface |
||
26 | */ |
||
27 | private $organizerRepository; |
||
28 | |||
29 | /** |
||
30 | * @var ReadRepositoryInterface |
||
31 | */ |
||
32 | private $labelRepository; |
||
33 | |||
34 | /** |
||
35 | * @var OrganizerRelationServiceInterface[] |
||
36 | */ |
||
37 | private $organizerRelationServices; |
||
38 | |||
39 | /** |
||
40 | * @param RepositoryInterface $organizerRepository |
||
41 | * @param ReadRepositoryInterface $labelRepository |
||
42 | */ |
||
43 | public function __construct( |
||
51 | |||
52 | /** |
||
53 | * @param OrganizerRelationServiceInterface $relationService |
||
54 | * @return OrganizerCommandHandler |
||
55 | */ |
||
56 | public function withOrganizerRelationService(OrganizerRelationServiceInterface $relationService) |
||
62 | |||
63 | /** |
||
64 | * @return array |
||
65 | */ |
||
66 | protected function getCommandHandlerMethods() |
||
80 | |||
81 | /** |
||
82 | * @param mixed $command |
||
83 | */ |
||
84 | public function handle($command) |
||
94 | |||
95 | protected function createOrganizer(CreateOrganizer $createOrganizer) |
||
106 | |||
107 | /** |
||
108 | * @param UpdateWebsite $updateWebsite |
||
109 | */ |
||
110 | protected function updateWebsite(UpdateWebsite $updateWebsite) |
||
118 | |||
119 | /** |
||
120 | * @param UpdateTitle $updateTitle |
||
121 | */ |
||
122 | protected function updateTitle(UpdateTitle $updateTitle) |
||
133 | |||
134 | /** |
||
135 | * @param UpdateAddress $updateAddress |
||
136 | */ |
||
137 | protected function updateAddress(UpdateAddress $updateAddress) |
||
145 | |||
146 | /** |
||
147 | * @param UpdateContactPoint $updateContactPoint |
||
148 | */ |
||
149 | protected function updateContactPoint(UpdateContactPoint $updateContactPoint) |
||
157 | |||
158 | /** |
||
159 | * @param AddLabel $addLabel |
||
160 | */ |
||
161 | protected function addLabel(AddLabel $addLabel) |
||
169 | |||
170 | /** |
||
171 | * @param RemoveLabel $removeLabel |
||
172 | */ |
||
173 | protected function removeLabel(RemoveLabel $removeLabel) |
||
181 | |||
182 | /** |
||
183 | * @param ImportLabels $importLabels |
||
184 | */ |
||
185 | protected function importLabels(ImportLabels $importLabels) |
||
193 | |||
194 | /** |
||
195 | * @param AbstractLabelCommand $labelCommand |
||
196 | * @return Label |
||
197 | */ |
||
198 | View Code Duplication | private function createLabel(AbstractLabelCommand $labelCommand) |
|
208 | |||
209 | /** |
||
210 | * @param DeleteOrganizer $deleteOrganizer |
||
211 | */ |
||
212 | public function deleteOrganizer(DeleteOrganizer $deleteOrganizer) |
||
228 | |||
229 | /** |
||
230 | * Makes it easier to type hint to Organizer. |
||
231 | * |
||
232 | * @param string $id |
||
233 | * @return Organizer |
||
234 | */ |
||
235 | protected function loadOrganizer($id) |
||
239 | } |
||
240 |
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.