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 Event 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 Event, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
62 | class Event extends Offer implements UpdateableWithCdbXmlInterface |
||
63 | { |
||
64 | /** |
||
65 | * @var string |
||
66 | */ |
||
67 | protected $eventId; |
||
68 | |||
69 | /** |
||
70 | * @var Audience |
||
71 | */ |
||
72 | private $audience; |
||
73 | |||
74 | /** |
||
75 | * @var boolean |
||
76 | */ |
||
77 | private $concluded = false; |
||
78 | |||
79 | const MAIN_LANGUAGE_CODE = 'nl'; |
||
80 | |||
81 | public function __construct() |
||
82 | { |
||
83 | parent::__construct(); |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * Factory method to create a new event. |
||
88 | * |
||
89 | * @param $eventId |
||
90 | * @param Title $title |
||
91 | * @param EventType $eventType |
||
92 | * @param Location $location |
||
93 | * @param CalendarInterface $calendar |
||
94 | * @param Theme|null $theme |
||
95 | * @param \DateTimeImmutable|null $publicationDate |
||
96 | * @return Event |
||
97 | */ |
||
98 | View Code Duplication | public static function create( |
|
|
|||
99 | $eventId, |
||
100 | Title $title, |
||
101 | EventType $eventType, |
||
102 | Location $location, |
||
103 | CalendarInterface $calendar, |
||
104 | Theme $theme = null, |
||
105 | \DateTimeImmutable $publicationDate = null |
||
106 | ) { |
||
107 | $event = new self(); |
||
108 | |||
109 | $event->apply( |
||
110 | new EventCreated( |
||
111 | $eventId, |
||
112 | $title, |
||
113 | $eventType, |
||
114 | $location, |
||
115 | $calendar, |
||
116 | $theme, |
||
117 | $publicationDate |
||
118 | ) |
||
119 | ); |
||
120 | |||
121 | return $event; |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * @param string $newEventId |
||
126 | * @param CalendarInterface $calendar |
||
127 | * |
||
128 | * @return Event |
||
129 | */ |
||
130 | public function copy($newEventId, CalendarInterface $calendar) |
||
131 | { |
||
132 | if ($this->hasUncommittedEvents()) { |
||
133 | throw new \RuntimeException('I refuse to copy, there are uncommitted events present.'); |
||
134 | } |
||
135 | |||
136 | // The copied event will have a playhead of the original event + 1 |
||
137 | $copy = clone $this; |
||
138 | |||
139 | $copy->apply( |
||
140 | new EventCopied( |
||
141 | $newEventId, |
||
142 | $this->eventId, |
||
143 | $calendar |
||
144 | ) |
||
145 | ); |
||
146 | |||
147 | return $copy; |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * @param string $eventId |
||
152 | * @param string $cdbXml |
||
153 | * @param string $cdbXmlNamespaceUri |
||
154 | * @return Event |
||
155 | */ |
||
156 | public static function importFromUDB2( |
||
157 | $eventId, |
||
158 | $cdbXml, |
||
159 | $cdbXmlNamespaceUri |
||
160 | ) { |
||
161 | $event = new self(); |
||
162 | $event->apply( |
||
163 | new EventImportedFromUDB2( |
||
164 | $eventId, |
||
165 | $cdbXml, |
||
166 | $cdbXmlNamespaceUri |
||
167 | ) |
||
168 | ); |
||
169 | |||
170 | return $event; |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * @param ImageCollection $images |
||
175 | */ |
||
176 | public function updateImagesFromUDB2(ImageCollection $images) |
||
177 | { |
||
178 | $this->apply(new ImagesUpdatedFromUDB2($this->eventId, $images)); |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * @param ImageCollection $images |
||
183 | */ |
||
184 | public function importImagesFromUDB2(ImageCollection $images) |
||
185 | { |
||
186 | $this->apply(new ImagesImportedFromUDB2($this->eventId, $images)); |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * {@inheritdoc} |
||
191 | */ |
||
192 | public function getAggregateRootId() |
||
193 | { |
||
194 | return $this->eventId; |
||
195 | } |
||
196 | |||
197 | /** |
||
198 | * @return UUID[] |
||
199 | */ |
||
200 | public function getMediaObjects() |
||
201 | { |
||
202 | return $this->mediaObjects; |
||
203 | } |
||
204 | |||
205 | protected function applyEventCreated(EventCreated $eventCreated) |
||
206 | { |
||
207 | $this->eventId = $eventCreated->getEventId(); |
||
208 | $this->workflowStatus = WorkflowStatus::DRAFT(); |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * @param EventCopied $eventCopied |
||
213 | */ |
||
214 | protected function applyEventCopied(EventCopied $eventCopied) |
||
215 | { |
||
216 | $this->eventId = $eventCopied->getItemId(); |
||
217 | $this->workflowStatus = WorkflowStatus::DRAFT(); |
||
218 | $this->labels = new LabelCollection(); |
||
219 | } |
||
220 | |||
221 | protected function applyEventImportedFromUDB2( |
||
222 | EventImportedFromUDB2 $eventImported |
||
223 | ) { |
||
224 | $this->eventId = $eventImported->getEventId(); |
||
225 | $this->setUDB2Data($eventImported); |
||
226 | } |
||
227 | |||
228 | /** |
||
229 | * @param EventUpdatedFromUDB2 $eventUpdated |
||
230 | */ |
||
231 | protected function applyEventUpdatedFromUDB2( |
||
232 | EventUpdatedFromUDB2 $eventUpdated |
||
233 | ) { |
||
234 | $this->setUDB2Data($eventUpdated); |
||
235 | } |
||
236 | |||
237 | /** |
||
238 | * @param EventCdbXMLInterface $eventCdbXML |
||
239 | */ |
||
240 | protected function setUDB2Data( |
||
241 | EventCdbXMLInterface $eventCdbXML |
||
242 | ) { |
||
243 | $udb2Event = EventItemFactory::createEventFromCdbXml( |
||
244 | $eventCdbXML->getCdbXmlNamespaceUri(), |
||
245 | $eventCdbXML->getCdbXml() |
||
246 | ); |
||
247 | |||
248 | $this->importWorkflowStatus($udb2Event); |
||
249 | $this->labels = LabelCollection::fromKeywords($udb2Event->getKeywords(true)); |
||
250 | } |
||
251 | |||
252 | /** |
||
253 | * Update the major info. |
||
254 | * |
||
255 | * @param Title $title |
||
256 | * @param EventType $eventType |
||
257 | * @param Location $location |
||
258 | * @param CalendarInterface $calendar |
||
259 | * @param Theme|null $theme |
||
260 | */ |
||
261 | public function updateMajorInfo( |
||
262 | Title $title, |
||
263 | EventType $eventType, |
||
264 | Location $location, |
||
265 | CalendarInterface $calendar, |
||
266 | Theme $theme = null |
||
267 | ) { |
||
268 | $this->apply(new MajorInfoUpdated($this->eventId, $title, $eventType, $location, $calendar, $theme)); |
||
269 | } |
||
270 | |||
271 | /** |
||
272 | * @param Audience $audience |
||
273 | */ |
||
274 | public function updateAudience( |
||
275 | Audience $audience |
||
276 | ) { |
||
277 | if (is_null($this->audience) || !$this->audience->equals($audience)) { |
||
278 | $this->apply(new AudienceUpdated( |
||
279 | $this->eventId, |
||
280 | $audience |
||
281 | )); |
||
282 | } |
||
283 | } |
||
284 | |||
285 | /** |
||
286 | * @param AudienceUpdated $audienceUpdated |
||
287 | */ |
||
288 | public function applyAudienceUpdated(AudienceUpdated $audienceUpdated) |
||
289 | { |
||
290 | $this->audience= $audienceUpdated->getAudience(); |
||
291 | } |
||
292 | |||
293 | /** |
||
294 | * @inheritDoc |
||
295 | * @return ImagesImportedFromUDB2 |
||
296 | */ |
||
297 | protected function createImagesImportedFromUDB2(ImageCollection $images) |
||
298 | { |
||
299 | return new ImagesImportedFromUDB2($this->eventId, $images); |
||
300 | } |
||
301 | |||
302 | /** |
||
303 | * @inheritDoc |
||
304 | * @return ImagesUpdatedFromUDB2 |
||
305 | */ |
||
306 | protected function createImagesUpdatedFromUDB2(ImageCollection $images) |
||
307 | { |
||
308 | return new ImagesUpdatedFromUDB2($this->eventId, $images); |
||
309 | } |
||
310 | |||
311 | /** |
||
312 | * @inheritdoc |
||
313 | */ |
||
314 | public function updateWithCdbXml($cdbXml, $cdbXmlNamespaceUri) |
||
315 | { |
||
316 | $this->apply( |
||
317 | new EventUpdatedFromUDB2( |
||
318 | $this->eventId, |
||
319 | $cdbXml, |
||
320 | $cdbXmlNamespaceUri |
||
321 | ) |
||
322 | ); |
||
323 | } |
||
324 | |||
325 | /** |
||
326 | * @param Label $label |
||
327 | * @return LabelAdded |
||
328 | */ |
||
329 | protected function createLabelAddedEvent(Label $label) |
||
330 | { |
||
331 | return new LabelAdded($this->eventId, $label); |
||
332 | } |
||
333 | |||
334 | /** |
||
335 | * @param Label $label |
||
336 | * @return LabelRemoved |
||
337 | */ |
||
338 | protected function createLabelRemovedEvent(Label $label) |
||
339 | { |
||
340 | return new LabelRemoved($this->eventId, $label); |
||
341 | } |
||
342 | |||
343 | /** |
||
344 | * @param Image $image |
||
345 | * @return ImageAdded |
||
346 | */ |
||
347 | protected function createImageAddedEvent(Image $image) |
||
348 | { |
||
349 | return new ImageAdded($this->eventId, $image); |
||
350 | } |
||
351 | |||
352 | /** |
||
353 | * @param Image $image |
||
354 | * @return ImageRemoved |
||
355 | */ |
||
356 | protected function createImageRemovedEvent(Image $image) |
||
357 | { |
||
358 | return new ImageRemoved($this->eventId, $image); |
||
359 | } |
||
360 | |||
361 | /** |
||
362 | * @param AbstractUpdateImage $updateImageCommand |
||
363 | * @return ImageUpdated |
||
364 | */ |
||
365 | protected function createImageUpdatedEvent( |
||
366 | AbstractUpdateImage $updateImageCommand |
||
367 | ) { |
||
368 | return new ImageUpdated( |
||
369 | $this->eventId, |
||
370 | $updateImageCommand->getMediaObjectId(), |
||
371 | $updateImageCommand->getDescription(), |
||
372 | $updateImageCommand->getCopyrightHolder() |
||
373 | ); |
||
374 | } |
||
375 | |||
376 | /** |
||
377 | * @param Image $image |
||
378 | * @return MainImageSelected |
||
379 | */ |
||
380 | protected function createMainImageSelectedEvent(Image $image) |
||
381 | { |
||
382 | return new MainImageSelected($this->eventId, $image); |
||
383 | } |
||
384 | |||
385 | /** |
||
386 | * @param Language $language |
||
387 | * @param StringLiteral $title |
||
388 | * @return TitleTranslated |
||
389 | */ |
||
390 | protected function createTitleTranslatedEvent(Language $language, StringLiteral $title) |
||
391 | { |
||
392 | return new TitleTranslated($this->eventId, $language, $title); |
||
393 | } |
||
394 | |||
395 | /** |
||
396 | * @param Language $language |
||
397 | * @param StringLiteral $description |
||
398 | * @return DescriptionTranslated |
||
399 | */ |
||
400 | protected function createDescriptionTranslatedEvent(Language $language, StringLiteral $description) |
||
401 | { |
||
402 | return new DescriptionTranslated($this->eventId, $language, $description); |
||
403 | } |
||
404 | |||
405 | /** |
||
406 | * @param string $description |
||
407 | * @return DescriptionUpdated |
||
408 | */ |
||
409 | protected function createDescriptionUpdatedEvent($description) |
||
410 | { |
||
411 | return new DescriptionUpdated($this->eventId, $description); |
||
412 | } |
||
413 | |||
414 | /** |
||
415 | * @inheritdoc |
||
416 | */ |
||
417 | protected function createCalendarUpdatedEvent(Calendar $calendar) |
||
418 | { |
||
419 | return new CalendarUpdated($this->eventId, $calendar); |
||
420 | } |
||
421 | |||
422 | /** |
||
423 | * @param string $typicalAgeRange |
||
424 | * @return TypicalAgeRangeUpdated |
||
425 | */ |
||
426 | protected function createTypicalAgeRangeUpdatedEvent($typicalAgeRange) |
||
430 | |||
431 | /** |
||
432 | * @return TypicalAgeRangeDeleted |
||
433 | */ |
||
434 | protected function createTypicalAgeRangeDeletedEvent() |
||
438 | |||
439 | /** |
||
440 | * @param string $organizerId |
||
441 | * @return OrganizerUpdated |
||
442 | */ |
||
443 | protected function createOrganizerUpdatedEvent($organizerId) |
||
447 | |||
448 | /** |
||
449 | * @param string $organizerId |
||
450 | * @return OrganizerDeleted |
||
451 | */ |
||
452 | protected function createOrganizerDeletedEvent($organizerId) |
||
456 | |||
457 | /** |
||
458 | * @param ContactPoint $contactPoint |
||
459 | * @return ContactPointUpdated |
||
460 | */ |
||
461 | protected function createContactPointUpdatedEvent(ContactPoint $contactPoint) |
||
465 | |||
466 | /** |
||
467 | * @param BookingInfo $bookingInfo |
||
468 | * @return BookingInfoUpdated |
||
469 | */ |
||
470 | protected function createBookingInfoUpdatedEvent(BookingInfo $bookingInfo) |
||
474 | |||
475 | /** |
||
476 | * @param PriceInfo $priceInfo |
||
477 | * @return PriceInfoUpdated |
||
478 | */ |
||
479 | protected function createPriceInfoUpdatedEvent(PriceInfo $priceInfo) |
||
483 | |||
484 | /** |
||
485 | * @return EventDeleted |
||
486 | */ |
||
487 | protected function createOfferDeletedEvent() |
||
491 | |||
492 | /** |
||
493 | * @inheritdoc |
||
494 | */ |
||
495 | protected function createPublishedEvent(\DateTimeInterface $publicationDate) |
||
499 | |||
500 | /** |
||
501 | * @inheritdoc |
||
502 | */ |
||
503 | protected function createApprovedEvent() |
||
507 | |||
508 | /** |
||
509 | * @inheritdoc |
||
510 | */ |
||
511 | protected function createRejectedEvent(StringLiteral $reason) |
||
515 | |||
516 | /** |
||
517 | * @inheritDoc |
||
518 | */ |
||
519 | protected function createFlaggedAsDuplicate() |
||
523 | |||
524 | /** |
||
525 | * @inheritDoc |
||
526 | */ |
||
527 | protected function createFlaggedAsInappropriate() |
||
531 | |||
532 | /** |
||
533 | * Use reflection to get check if the aggregate has uncommitted events. |
||
534 | * @return bool |
||
535 | */ |
||
536 | private function hasUncommittedEvents() |
||
546 | |||
547 | public function conclude() |
||
553 | |||
554 | /** |
||
555 | * @param Concluded $concluded |
||
556 | */ |
||
557 | protected function applyConcluded(Concluded $concluded) |
||
561 | } |
||
562 |
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.