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 |
||
58 | class Event extends Offer implements UpdateableWithCdbXmlInterface |
||
59 | { |
||
60 | protected $eventId; |
||
61 | |||
62 | /** |
||
63 | * @var Translation[] |
||
64 | */ |
||
65 | protected $translations = []; |
||
66 | |||
67 | const MAIN_LANGUAGE_CODE = 'nl'; |
||
68 | |||
69 | public function __construct() |
||
73 | |||
74 | /** |
||
75 | * Factory method to create a new event. |
||
76 | * |
||
77 | * @param Title $title |
||
78 | * @param EventType $eventType |
||
79 | * @param Location $location |
||
80 | * @param CalendarInterface $calendar |
||
81 | * @param Theme|null $theme |
||
82 | * @param \DateTimeImmutable|null $publicationDate |
||
83 | * |
||
84 | * @return Event |
||
85 | */ |
||
86 | public static function create( |
||
117 | |||
118 | /** |
||
119 | * @param string $eventId |
||
120 | * @param string $cdbXml |
||
121 | * @param string $cdbXmlNamespaceUri |
||
122 | * @return Event |
||
123 | */ |
||
124 | public static function importFromUDB2( |
||
140 | |||
141 | /** |
||
142 | * @param EventXmlString $xmlString |
||
143 | * @param StringLiteral $eventId |
||
144 | * @param StringLiteral $cdbXmlNamespaceUri |
||
145 | * @return Event |
||
146 | */ |
||
147 | public static function createFromCdbXml( |
||
163 | |||
164 | /** |
||
165 | * @param StringLiteral $eventId |
||
166 | * @param EventXmlString $xmlString |
||
167 | * @param StringLiteral $cdbXmlNamespaceUri |
||
168 | * @return Event |
||
169 | */ |
||
170 | public function updateFromCdbXml( |
||
183 | |||
184 | /** |
||
185 | * @param LabelCollection $labels |
||
186 | */ |
||
187 | public function mergeLabels(LabelCollection $labels) |
||
202 | |||
203 | /** |
||
204 | * @param Language $language |
||
205 | * @param StringLiteral|null $title |
||
206 | * @param StringLiteral|null $shortDescription |
||
207 | * @param StringLiteral|null $longDescription |
||
208 | */ |
||
209 | public function applyTranslation( |
||
225 | |||
226 | /** |
||
227 | * @param Language $language |
||
228 | */ |
||
229 | public function deleteTranslation( |
||
243 | |||
244 | /** |
||
245 | * {@inheritdoc} |
||
246 | */ |
||
247 | public function getAggregateRootId() |
||
251 | |||
252 | /** |
||
253 | * @return Translation[] |
||
254 | */ |
||
255 | public function getTranslations() |
||
259 | |||
260 | /** |
||
261 | * @return UUID[] |
||
262 | */ |
||
263 | public function getMediaObjects() |
||
267 | |||
268 | protected function applyEventCreated(EventCreated $eventCreated) |
||
273 | |||
274 | protected function applyEventImportedFromUDB2( |
||
280 | |||
281 | /** |
||
282 | * @param EventUpdatedFromUDB2 $eventUpdated |
||
283 | */ |
||
284 | protected function applyEventUpdatedFromUDB2( |
||
289 | |||
290 | /** |
||
291 | * @param EventCdbXMLInterface $eventCdbXML |
||
292 | */ |
||
293 | protected function setUDB2Data( |
||
304 | |||
305 | /** |
||
306 | * Update the major info. |
||
307 | * |
||
308 | * @param Title $title |
||
309 | * @param EventType $eventType |
||
310 | * @param Location $location |
||
311 | * @param CalendarInterface $calendar |
||
312 | * @param type $theme |
||
313 | */ |
||
314 | public function updateMajorInfo( |
||
323 | |||
324 | protected function applyEventCreatedFromCdbXml( |
||
336 | |||
337 | protected function applyEventUpdatedFromCdbXml( |
||
349 | |||
350 | protected function applyLabelsMerged( |
||
355 | |||
356 | protected function applyTranslationApplied( |
||
376 | |||
377 | protected function applyTranslationDeleted( |
||
386 | |||
387 | /** |
||
388 | * @inheritdoc |
||
389 | */ |
||
390 | public function updateWithCdbXml($cdbXml, $cdbXmlNamespaceUri) |
||
400 | |||
401 | /** |
||
402 | * @param Label $label |
||
403 | * @return LabelAdded |
||
404 | */ |
||
405 | protected function createLabelAddedEvent(Label $label) |
||
409 | |||
410 | /** |
||
411 | * @param Label $label |
||
412 | * @return LabelRemoved |
||
413 | */ |
||
414 | protected function createLabelRemovedEvent(Label $label) |
||
418 | |||
419 | /** |
||
420 | * @param Image $image |
||
421 | * @return ImageAdded |
||
422 | */ |
||
423 | protected function createImageAddedEvent(Image $image) |
||
427 | |||
428 | /** |
||
429 | * @param Image $image |
||
430 | * @return ImageRemoved |
||
431 | */ |
||
432 | protected function createImageRemovedEvent(Image $image) |
||
436 | |||
437 | /** |
||
438 | * @param AbstractUpdateImage $updateImageCommand |
||
439 | * @return ImageUpdated |
||
440 | */ |
||
441 | protected function createImageUpdatedEvent( |
||
451 | |||
452 | /** |
||
453 | * @param Image $image |
||
454 | * @return MainImageSelected |
||
455 | */ |
||
456 | protected function createMainImageSelectedEvent(Image $image) |
||
460 | |||
461 | /** |
||
462 | * @param Language $language |
||
463 | * @param StringLiteral $title |
||
464 | * @return TitleTranslated |
||
465 | */ |
||
466 | protected function createTitleTranslatedEvent(Language $language, StringLiteral $title) |
||
470 | |||
471 | /** |
||
472 | * @param Language $language |
||
473 | * @param StringLiteral $description |
||
474 | * @return DescriptionTranslated |
||
475 | */ |
||
476 | protected function createDescriptionTranslatedEvent(Language $language, StringLiteral $description) |
||
480 | |||
481 | /** |
||
482 | * @param string $description |
||
483 | * @return DescriptionUpdated |
||
484 | */ |
||
485 | protected function createDescriptionUpdatedEvent($description) |
||
489 | |||
490 | /** |
||
491 | * @param string $typicalAgeRange |
||
492 | * @return TypicalAgeRangeUpdated |
||
493 | */ |
||
494 | protected function createTypicalAgeRangeUpdatedEvent($typicalAgeRange) |
||
498 | |||
499 | /** |
||
500 | * @return TypicalAgeRangeDeleted |
||
501 | */ |
||
502 | protected function createTypicalAgeRangeDeletedEvent() |
||
506 | |||
507 | /** |
||
508 | * @param string $organizerId |
||
509 | * @return OrganizerUpdated |
||
510 | */ |
||
511 | protected function createOrganizerUpdatedEvent($organizerId) |
||
515 | |||
516 | /** |
||
517 | * @param string $organizerId |
||
518 | * @return OrganizerDeleted |
||
519 | */ |
||
520 | protected function createOrganizerDeletedEvent($organizerId) |
||
524 | |||
525 | /** |
||
526 | * @param ContactPoint $contactPoint |
||
527 | * @return ContactPointUpdated |
||
528 | */ |
||
529 | protected function createContactPointUpdatedEvent(ContactPoint $contactPoint) |
||
533 | |||
534 | /** |
||
535 | * @param BookingInfo $bookingInfo |
||
536 | * @return BookingInfoUpdated |
||
537 | */ |
||
538 | protected function createBookingInfoUpdatedEvent(BookingInfo $bookingInfo) |
||
542 | |||
543 | /** |
||
544 | * @param PriceInfo $priceInfo |
||
545 | * @return PriceInfoUpdated |
||
546 | */ |
||
547 | protected function createPriceInfoUpdatedEvent(PriceInfo $priceInfo) |
||
551 | |||
552 | /** |
||
553 | * @return EventDeleted |
||
554 | */ |
||
555 | protected function createOfferDeletedEvent() |
||
559 | |||
560 | /** |
||
561 | * @inheritdoc |
||
562 | */ |
||
563 | protected function createPublishedEvent(\DateTimeInterface $publicationDate) |
||
567 | |||
568 | /** |
||
569 | * @inheritdoc |
||
570 | */ |
||
571 | protected function createApprovedEvent() |
||
575 | |||
576 | /** |
||
577 | * @inheritdoc |
||
578 | */ |
||
579 | protected function createRejectedEvent(StringLiteral $reason) |
||
583 | |||
584 | /** |
||
585 | * @inheritDoc |
||
586 | */ |
||
587 | protected function createFlaggedAsDuplicate() |
||
591 | |||
592 | /** |
||
593 | * @inheritDoc |
||
594 | */ |
||
595 | protected function createFlaggedAsInappropriate() |
||
599 | } |
||
600 |
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.