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 |
||
39 | abstract class OfferCommandHandler extends Udb3CommandHandler |
||
40 | { |
||
41 | /** |
||
42 | * @var RepositoryInterface |
||
43 | */ |
||
44 | protected $offerRepository; |
||
45 | |||
46 | /** |
||
47 | * @var RepositoryInterface |
||
48 | */ |
||
49 | protected $organizerRepository; |
||
50 | |||
51 | /** |
||
52 | * @var RepositoryInterface |
||
53 | */ |
||
54 | protected $labelRepository; |
||
55 | |||
56 | /** |
||
57 | * @param RepositoryInterface $offerRepository |
||
58 | * @param RepositoryInterface $organizerRepository |
||
59 | * @param ReadRepositoryInterface $labelRepository |
||
60 | */ |
||
61 | public function __construct( |
||
62 | RepositoryInterface $offerRepository, |
||
63 | RepositoryInterface $organizerRepository, |
||
64 | ReadRepositoryInterface $labelRepository |
||
65 | ) { |
||
66 | $this->offerRepository = $offerRepository; |
||
67 | $this->organizerRepository = $organizerRepository; |
||
68 | $this->labelRepository = $labelRepository; |
||
|
|||
69 | } |
||
70 | |||
71 | /** |
||
72 | * {@inheritdoc} |
||
73 | */ |
||
74 | public function handle($command) |
||
75 | { |
||
76 | $commandName = get_class($command); |
||
77 | $commandHandlers = $this->getCommandHandlers(); |
||
78 | |||
79 | if (isset($commandHandlers[$commandName])) { |
||
80 | $handler = $commandHandlers[$commandName]; |
||
81 | call_user_func(array($this, $handler), $command); |
||
82 | } else { |
||
83 | parent::handle($command); |
||
84 | } |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * @return string[] |
||
89 | * An associative array of commands and their handler methods. |
||
90 | */ |
||
91 | View Code Duplication | private function getCommandHandlers() |
|
92 | { |
||
93 | $commands = []; |
||
94 | |||
95 | foreach (get_class_methods($this) as $method) { |
||
96 | $matches = []; |
||
97 | if (preg_match('/^handle(.+)$/', $method, $matches)) { |
||
98 | $command = $matches[1]; |
||
99 | $classNameMethod = 'get' . $command . 'ClassName'; |
||
100 | |||
101 | if (method_exists($this, $classNameMethod)) { |
||
102 | $commandFullClassName = call_user_func(array($this, $classNameMethod)); |
||
103 | $commands[$commandFullClassName] = $method; |
||
104 | } |
||
105 | } |
||
106 | } |
||
107 | |||
108 | return $commands; |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * @return string |
||
113 | */ |
||
114 | abstract protected function getAddLabelClassName(); |
||
115 | |||
116 | /** |
||
117 | * @return string |
||
118 | */ |
||
119 | abstract protected function getRemoveLabelClassName(); |
||
120 | |||
121 | /** |
||
122 | * @return string |
||
123 | */ |
||
124 | abstract protected function getUpdateTitleClassName(); |
||
125 | |||
126 | /** |
||
127 | * @return string |
||
128 | */ |
||
129 | abstract protected function getAddImageClassName(); |
||
130 | |||
131 | /** |
||
132 | * @return string |
||
133 | */ |
||
134 | abstract protected function getUpdateImageClassName(); |
||
135 | |||
136 | /** |
||
137 | * @return string |
||
138 | */ |
||
139 | abstract protected function getRemoveImageClassName(); |
||
140 | |||
141 | /** |
||
142 | * @return string |
||
143 | */ |
||
144 | abstract protected function getSelectMainImageClassName(); |
||
145 | |||
146 | /** |
||
147 | * @return string |
||
148 | */ |
||
149 | abstract protected function getUpdateDescriptionClassName(); |
||
150 | |||
151 | /** |
||
152 | * @return string |
||
153 | */ |
||
154 | abstract protected function getUpdateCalendarClassName(); |
||
155 | |||
156 | /** |
||
157 | * @return string |
||
158 | */ |
||
159 | abstract protected function getUpdateTypicalAgeRangeClassName(); |
||
160 | |||
161 | /** |
||
162 | * @return string |
||
163 | */ |
||
164 | abstract protected function getDeleteTypicalAgeRangeClassName(); |
||
165 | |||
166 | /** |
||
167 | * @return string |
||
168 | */ |
||
169 | abstract protected function getUpdateOrganizerClassName(); |
||
170 | |||
171 | /** |
||
172 | * @return string |
||
173 | */ |
||
174 | abstract protected function getDeleteOrganizerClassName(); |
||
175 | |||
176 | /** |
||
177 | * @return string |
||
178 | */ |
||
179 | abstract protected function getUpdateContactPointClassName(); |
||
180 | |||
181 | /** |
||
182 | * @return string |
||
183 | */ |
||
184 | abstract protected function getUpdateBookingInfoClassName(); |
||
185 | |||
186 | /** |
||
187 | * @return string |
||
188 | */ |
||
189 | abstract protected function getUpdatePriceInfoClassName(); |
||
190 | |||
191 | /** |
||
192 | * @return string |
||
193 | */ |
||
194 | abstract protected function getDeleteOfferClassName(); |
||
195 | |||
196 | /** |
||
197 | * @return string |
||
198 | */ |
||
199 | abstract protected function getPublishClassName(); |
||
200 | |||
201 | /** |
||
202 | * @return string |
||
203 | */ |
||
204 | abstract protected function getApproveClassName(); |
||
205 | |||
206 | /** |
||
207 | * @return string |
||
208 | */ |
||
209 | abstract protected function getRejectClassName(); |
||
210 | |||
211 | /** |
||
212 | * @return string |
||
213 | */ |
||
214 | abstract protected function getFlagAsDuplicateClassName(); |
||
215 | |||
216 | /** |
||
217 | * @return string |
||
218 | */ |
||
219 | abstract protected function getFlagAsInappropriateClassName(); |
||
220 | |||
221 | /** |
||
222 | * @return string |
||
223 | */ |
||
224 | abstract protected function getUpdateTypeClassName(); |
||
225 | |||
226 | /** |
||
227 | * @return string |
||
228 | */ |
||
229 | abstract protected function getUpdateThemeClassName(); |
||
230 | |||
231 | /** |
||
232 | * @return string |
||
233 | */ |
||
234 | abstract protected function getUpdateFacilitiesClassName(); |
||
235 | |||
236 | /** |
||
237 | * @param AbstractUpdateType $updateType |
||
238 | */ |
||
239 | public function handleUpdateType(AbstractUpdateType $updateType) |
||
247 | |||
248 | /** |
||
249 | * @param AbstractUpdateTheme $updateTheme |
||
250 | */ |
||
251 | public function handleUpdateTheme(AbstractUpdateTheme $updateTheme) |
||
259 | |||
260 | /** |
||
261 | * @param AbstractUpdateFacilities $updateFacilities |
||
262 | */ |
||
263 | public function handleUpdateFacilities(AbstractUpdateFacilities $updateFacilities) |
||
271 | |||
272 | /** |
||
273 | * @param AbstractAddLabel $addLabel |
||
274 | */ |
||
275 | private function handleAddLabel(AbstractAddLabel $addLabel) |
||
283 | |||
284 | /** |
||
285 | * @param AbstractRemoveLabel $removeLabel |
||
286 | */ |
||
287 | private function handleRemoveLabel(AbstractRemoveLabel $removeLabel) |
||
295 | |||
296 | /** |
||
297 | * @param AbstractLabelCommand $labelCommand |
||
298 | * @return Label |
||
299 | */ |
||
300 | View Code Duplication | private function createLabel(AbstractLabelCommand $labelCommand) |
|
310 | |||
311 | /** |
||
312 | * @param AbstractUpdateTitle $translateTitle |
||
313 | */ |
||
314 | private function handleUpdateTitle(AbstractUpdateTitle $translateTitle) |
||
320 | |||
321 | /** |
||
322 | * Handle an add image command. |
||
323 | * @param AbstractAddImage $addImage |
||
324 | */ |
||
325 | public function handleAddImage(AbstractAddImage $addImage) |
||
331 | |||
332 | /** |
||
333 | * @param AbstractRemoveImage $removeImage |
||
334 | */ |
||
335 | public function handleRemoveImage(AbstractRemoveImage $removeImage) |
||
341 | |||
342 | /** |
||
343 | * @param AbstractUpdateImage $updateImage |
||
344 | */ |
||
345 | public function handleUpdateImage(AbstractUpdateImage $updateImage) |
||
351 | |||
352 | /** |
||
353 | * @param AbstractSelectMainImage $selectMainImage |
||
354 | */ |
||
355 | public function handleSelectMainImage(AbstractSelectMainImage $selectMainImage) |
||
361 | |||
362 | /** |
||
363 | * Handle the update of description on a place. |
||
364 | * @param AbstractUpdateDescription $updateDescription |
||
365 | */ |
||
366 | public function handleUpdateDescription(AbstractUpdateDescription $updateDescription) |
||
378 | |||
379 | /** |
||
380 | * @param AbstractUpdateCalendar $updateCalendar |
||
381 | */ |
||
382 | public function handleUpdateCalendar(AbstractUpdateCalendar $updateCalendar) |
||
390 | |||
391 | /** |
||
392 | * Handle the update of typical age range on a place. |
||
393 | * @param AbstractUpdateTypicalAgeRange $updateTypicalAgeRange |
||
394 | */ |
||
395 | public function handleUpdateTypicalAgeRange(AbstractUpdateTypicalAgeRange $updateTypicalAgeRange) |
||
406 | |||
407 | /** |
||
408 | * Handle the deletion of typical age range on a place. |
||
409 | * @param AbstractDeleteTypicalAgeRange $deleteTypicalAgeRange |
||
410 | */ |
||
411 | public function handleDeleteTypicalAgeRange(AbstractDeleteTypicalAgeRange $deleteTypicalAgeRange) |
||
420 | |||
421 | /** |
||
422 | * Handle an update command to update organizer of a place. |
||
423 | * @param AbstractUpdateOrganizer $updateOrganizer |
||
424 | */ |
||
425 | public function handleUpdateOrganizer(AbstractUpdateOrganizer $updateOrganizer) |
||
436 | |||
437 | /** |
||
438 | * Handle an update command to delete the organizer. |
||
439 | * @param AbstractDeleteOrganizer $deleteOrganizer |
||
440 | */ |
||
441 | public function handleDeleteOrganizer(AbstractDeleteOrganizer $deleteOrganizer) |
||
451 | |||
452 | /** |
||
453 | * Handle an update command to updated the contact point. |
||
454 | * @param AbstractUpdateContactPoint $updateContactPoint |
||
455 | */ |
||
456 | public function handleUpdateContactPoint(AbstractUpdateContactPoint $updateContactPoint) |
||
467 | |||
468 | /** |
||
469 | * Handle an update command to updated the booking info. |
||
470 | * @param AbstractUpdateBookingInfo $updateBookingInfo |
||
471 | */ |
||
472 | public function handleUpdateBookingInfo(AbstractUpdateBookingInfo $updateBookingInfo) |
||
482 | |||
483 | /** |
||
484 | * @param AbstractUpdatePriceInfo $updatePriceInfo |
||
485 | */ |
||
486 | private function handleUpdatePriceInfo(AbstractUpdatePriceInfo $updatePriceInfo) |
||
496 | |||
497 | /** |
||
498 | * @param AbstractDeleteOffer $deleteOffer |
||
499 | */ |
||
500 | private function handleDeleteOffer(AbstractDeleteOffer $deleteOffer) |
||
506 | |||
507 | /** |
||
508 | * @param AbstractPublish $publish |
||
509 | */ |
||
510 | private function handlePublish(AbstractPublish $publish) |
||
516 | |||
517 | /** |
||
518 | * @param AbstractApprove $approve |
||
519 | */ |
||
520 | private function handleApprove(AbstractApprove $approve) |
||
526 | |||
527 | /** |
||
528 | * @param AbstractReject $reject |
||
529 | */ |
||
530 | private function handleReject(AbstractReject $reject) |
||
536 | |||
537 | /** |
||
538 | * @param AbstractFlagAsDuplicate $flagAsDuplicate |
||
539 | */ |
||
540 | private function handleFlagAsDuplicate(AbstractFlagAsDuplicate $flagAsDuplicate) |
||
546 | |||
547 | /** |
||
548 | * @param AbstractFlagAsInappropriate $flagAsInappropriate |
||
549 | */ |
||
550 | private function handleFlagAsInappropriate(AbstractFlagAsInappropriate $flagAsInappropriate) |
||
556 | |||
557 | /** |
||
558 | * Makes it easier to type-hint to Offer. |
||
559 | * |
||
560 | * @param string $id |
||
561 | * @return Offer |
||
562 | */ |
||
563 | private function load($id) |
||
567 | |||
568 | /** |
||
569 | * Makes it easier to type-hint to Organizer. |
||
570 | * |
||
571 | * @param string $id |
||
572 | * @return Organizer |
||
573 | */ |
||
574 | private function loadOrganizer($id) |
||
579 | } |
||
580 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..