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 |
||
73 | class Event extends Offer implements UpdateableWithCdbXmlInterface |
||
74 | { |
||
75 | /** |
||
76 | * @var string |
||
77 | */ |
||
78 | protected $eventId; |
||
79 | |||
80 | /** |
||
81 | * @var Audience |
||
82 | */ |
||
83 | private $audience; |
||
84 | |||
85 | /** |
||
86 | * @var LocationId |
||
87 | */ |
||
88 | private $locationId; |
||
89 | |||
90 | /** |
||
91 | * @var boolean |
||
92 | */ |
||
93 | private $concluded = false; |
||
94 | |||
95 | public function __construct() |
||
99 | |||
100 | /** |
||
101 | * Factory method to create a new event. |
||
102 | * |
||
103 | * @param $eventId |
||
104 | * @param Language $mainLanguage |
||
105 | * @param Title $title |
||
106 | * @param EventType $eventType |
||
107 | * @param LocationId $location |
||
108 | * @param CalendarInterface $calendar |
||
109 | * @param Theme|null $theme |
||
110 | * @param \DateTimeImmutable|null $publicationDate |
||
111 | * @return Event |
||
112 | */ |
||
113 | public static function create( |
||
148 | |||
149 | /** |
||
150 | * @param string $newEventId |
||
151 | * @param CalendarInterface $calendar |
||
152 | * |
||
153 | * @return Event |
||
154 | */ |
||
155 | public function copy($newEventId, CalendarInterface $calendar) |
||
174 | |||
175 | /** |
||
176 | * @param string $eventId |
||
177 | * @param string $cdbXml |
||
178 | * @param string $cdbXmlNamespaceUri |
||
179 | * @return Event |
||
180 | */ |
||
181 | public static function importFromUDB2( |
||
197 | |||
198 | /** |
||
199 | * {@inheritdoc} |
||
200 | */ |
||
201 | public function getAggregateRootId() |
||
205 | |||
206 | protected function applyEventCreated(EventCreated $eventCreated) |
||
207 | { |
||
208 | $this->eventId = $eventCreated->getEventId(); |
||
209 | $this->titles[$eventCreated->getMainLanguage()->getCode()] = $eventCreated->getTitle(); |
||
210 | $this->calendar = $eventCreated->getCalendar(); |
||
211 | $this->audience = new Audience(AudienceType::EVERYONE()); |
||
212 | $this->contactPoint = new ContactPoint(); |
||
213 | $this->bookingInfo = new BookingInfo(); |
||
214 | $this->typeId = $eventCreated->getEventType()->getId(); |
||
215 | $this->themeId = $eventCreated->getTheme() ? $eventCreated->getTheme()->getId() : null; |
||
216 | $this->locationId = $eventCreated->getLocation(); |
||
217 | $this->mainLanguage = $eventCreated->getMainLanguage(); |
||
218 | $this->workflowStatus = WorkflowStatus::DRAFT(); |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * @param EventCopied $eventCopied |
||
223 | */ |
||
224 | protected function applyEventCopied(EventCopied $eventCopied) |
||
230 | |||
231 | protected function applyEventImportedFromUDB2( |
||
239 | |||
240 | /** |
||
241 | * @param EventUpdatedFromUDB2 $eventUpdated |
||
242 | */ |
||
243 | protected function applyEventUpdatedFromUDB2( |
||
249 | |||
250 | protected function applyEventDeleted(EventDeleted $event): void |
||
254 | |||
255 | /** |
||
256 | * @param EventCdbXMLInterface $eventCdbXML |
||
257 | */ |
||
258 | protected function setUDB2Data( |
||
294 | |||
295 | /** |
||
296 | * Update the major info. |
||
297 | * |
||
298 | * @param Title $title |
||
299 | * @param EventType $eventType |
||
300 | * @param LocationId $location |
||
301 | * @param CalendarInterface $calendar |
||
302 | * @param Theme|null $theme |
||
303 | */ |
||
304 | public function updateMajorInfo( |
||
313 | |||
314 | protected function applyMajorInfoUpdated(MajorInfoUpdated $majorInfoUpdated) |
||
318 | |||
319 | /** |
||
320 | * @param LocationId $locationId |
||
321 | */ |
||
322 | public function updateLocation(LocationId $locationId) |
||
338 | |||
339 | /** |
||
340 | * @param LocationUpdated $locationUpdated |
||
341 | */ |
||
342 | public function applyLocationUpdated(LocationUpdated $locationUpdated) |
||
346 | |||
347 | public function updateAudience( |
||
365 | |||
366 | /** |
||
367 | * @param AudienceUpdated $audienceUpdated |
||
368 | */ |
||
369 | public function applyAudienceUpdated(AudienceUpdated $audienceUpdated) |
||
373 | |||
374 | /** |
||
375 | * @inheritDoc |
||
376 | * @return ImagesImportedFromUDB2 |
||
377 | */ |
||
378 | protected function createImagesImportedFromUDB2(ImageCollection $images) |
||
382 | |||
383 | /** |
||
384 | * @inheritDoc |
||
385 | * @return ImagesUpdatedFromUDB2 |
||
386 | */ |
||
387 | protected function createImagesUpdatedFromUDB2(ImageCollection $images) |
||
391 | |||
392 | /** |
||
393 | * @inheritdoc |
||
394 | */ |
||
395 | public function updateWithCdbXml($cdbXml, $cdbXmlNamespaceUri) |
||
405 | |||
406 | /** |
||
407 | * @param Label $label |
||
408 | * @return LabelAdded |
||
409 | */ |
||
410 | protected function createLabelAddedEvent(Label $label) |
||
414 | |||
415 | /** |
||
416 | * @param Label $label |
||
417 | * @return LabelRemoved |
||
418 | */ |
||
419 | protected function createLabelRemovedEvent(Label $label) |
||
423 | |||
424 | /** |
||
425 | * @inheritdoc |
||
426 | */ |
||
427 | protected function createLabelsImportedEvent(Labels $labels) |
||
431 | |||
432 | /** |
||
433 | * @param Image $image |
||
434 | * @return ImageAdded |
||
435 | */ |
||
436 | protected function createImageAddedEvent(Image $image) |
||
440 | |||
441 | /** |
||
442 | * @param Image $image |
||
443 | * @return ImageRemoved |
||
444 | */ |
||
445 | protected function createImageRemovedEvent(Image $image) |
||
449 | |||
450 | /** |
||
451 | * @param UUID $mediaObjectId |
||
452 | * @param StringLiteral $description |
||
453 | * @param StringLiteral $copyrightHolder |
||
454 | * @return ImageUpdated |
||
455 | */ |
||
456 | protected function createImageUpdatedEvent( |
||
468 | |||
469 | /** |
||
470 | * @param Image $image |
||
471 | * @return MainImageSelected |
||
472 | */ |
||
473 | protected function createMainImageSelectedEvent(Image $image) |
||
477 | |||
478 | /** |
||
479 | * @inheritdoc |
||
480 | */ |
||
481 | protected function createTitleTranslatedEvent(Language $language, Title $title) |
||
485 | |||
486 | /** |
||
487 | * @param Title $title |
||
488 | * @return TitleUpdated |
||
489 | */ |
||
490 | protected function createTitleUpdatedEvent(Title $title) |
||
494 | |||
495 | /** |
||
496 | * @inheritdoc |
||
497 | */ |
||
498 | protected function createDescriptionTranslatedEvent(Language $language, Description $description) |
||
502 | |||
503 | /** |
||
504 | * @inheritdoc |
||
505 | */ |
||
506 | protected function createDescriptionUpdatedEvent(Description $description) |
||
510 | |||
511 | /** |
||
512 | * @inheritdoc |
||
513 | */ |
||
514 | protected function createCalendarUpdatedEvent(Calendar $calendar) |
||
518 | |||
519 | /** |
||
520 | * @param AgeRange $typicalAgeRange |
||
521 | * @return TypicalAgeRangeUpdated |
||
522 | */ |
||
523 | protected function createTypicalAgeRangeUpdatedEvent($typicalAgeRange) |
||
527 | |||
528 | /** |
||
529 | * @return TypicalAgeRangeDeleted |
||
530 | */ |
||
531 | protected function createTypicalAgeRangeDeletedEvent() |
||
535 | |||
536 | /** |
||
537 | * @param string $organizerId |
||
538 | * @return OrganizerUpdated |
||
539 | */ |
||
540 | protected function createOrganizerUpdatedEvent($organizerId) |
||
544 | |||
545 | /** |
||
546 | * @param string $organizerId |
||
547 | * @return OrganizerDeleted |
||
548 | */ |
||
549 | protected function createOrganizerDeletedEvent($organizerId) |
||
553 | |||
554 | /** |
||
555 | * @param ContactPoint $contactPoint |
||
556 | * @return ContactPointUpdated |
||
557 | */ |
||
558 | protected function createContactPointUpdatedEvent(ContactPoint $contactPoint) |
||
562 | |||
563 | /** |
||
564 | * @inheritdoc |
||
565 | */ |
||
566 | protected function createGeoCoordinatesUpdatedEvent(Coordinates $coordinates) |
||
570 | |||
571 | /** |
||
572 | * @param BookingInfo $bookingInfo |
||
573 | * @return BookingInfoUpdated |
||
574 | */ |
||
575 | protected function createBookingInfoUpdatedEvent(BookingInfo $bookingInfo) |
||
579 | |||
580 | /** |
||
581 | * @param PriceInfo $priceInfo |
||
582 | * @return PriceInfoUpdated |
||
583 | */ |
||
584 | protected function createPriceInfoUpdatedEvent(PriceInfo $priceInfo) |
||
588 | |||
589 | /** |
||
590 | * @return EventDeleted |
||
591 | */ |
||
592 | protected function createOfferDeletedEvent() |
||
596 | |||
597 | /** |
||
598 | * @inheritdoc |
||
599 | */ |
||
600 | protected function createPublishedEvent(\DateTimeInterface $publicationDate) |
||
604 | |||
605 | /** |
||
606 | * @inheritdoc |
||
607 | */ |
||
608 | protected function createApprovedEvent() |
||
612 | |||
613 | /** |
||
614 | * @inheritdoc |
||
615 | */ |
||
616 | protected function createRejectedEvent(StringLiteral $reason) |
||
620 | |||
621 | /** |
||
622 | * @inheritDoc |
||
623 | */ |
||
624 | protected function createFlaggedAsDuplicate() |
||
628 | |||
629 | /** |
||
630 | * @inheritDoc |
||
631 | */ |
||
632 | protected function createFlaggedAsInappropriate() |
||
636 | |||
637 | /** |
||
638 | * @inheritdoc |
||
639 | */ |
||
640 | protected function createTypeUpdatedEvent(EventType $type) |
||
644 | |||
645 | /** |
||
646 | * @inheritdoc |
||
647 | */ |
||
648 | protected function createThemeUpdatedEvent(Theme $theme) |
||
652 | |||
653 | /** |
||
654 | * @inheritdoc |
||
655 | */ |
||
656 | protected function createFacilitiesUpdatedEvent(array $facilities) |
||
660 | |||
661 | /** |
||
662 | * Use reflection to get check if the aggregate has uncommitted events. |
||
663 | * @return bool |
||
664 | */ |
||
665 | private function hasUncommittedEvents() |
||
675 | |||
676 | public function conclude() |
||
682 | |||
683 | /** |
||
684 | * @param Concluded $concluded |
||
685 | */ |
||
686 | protected function applyConcluded(Concluded $concluded) |
||
690 | } |
||
691 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.