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 | ||
| 51 | class Event extends EventSourcedAggregateRoot | ||
| 52 | { | ||
| 53 | protected $eventId; | ||
| 54 | |||
| 55 | /** | ||
| 56 | * @var LabelCollection | ||
| 57 | */ | ||
| 58 | protected $labels; | ||
| 59 | |||
| 60 | /** | ||
| 61 | * @var Translation[] | ||
| 62 | */ | ||
| 63 | protected $translations = []; | ||
| 64 | |||
| 65 | /** | ||
| 66 | * @var CollaborationDataCollection[] | ||
| 67 | * Array of different collections, keyed by language. | ||
| 68 | */ | ||
| 69 | protected $collaborationData; | ||
| 70 | |||
| 71 | /** | ||
| 72 | * @var UUID[] | ||
| 73 | */ | ||
| 74 | protected $mediaObjects = []; | ||
| 75 | |||
| 76 | const MAIN_LANGUAGE_CODE = 'nl'; | ||
| 77 | |||
| 78 | public function __construct() | ||
| 82 | |||
| 83 | /** | ||
| 84 | * Factory method to create a new event. | ||
| 85 | * | ||
| 86 | * @param Title $title | ||
| 87 | * @param EventType $eventType | ||
| 88 | * @param Location $location | ||
| 89 | * @param CalendarBase $calendar | ||
| 90 | * @param Theme/null $theme | ||
|  | |||
| 91 | * | ||
| 92 | * @return Event | ||
| 93 | */ | ||
| 94 | public static function create($eventId, Title $title, EventType $eventType, Location $location, CalendarInterface $calendar, $theme = null) | ||
| 106 | |||
| 107 | /** | ||
| 108 | * @param string $eventId | ||
| 109 | * @param string $cdbXml | ||
| 110 | * @param string $cdbXmlNamespaceUri | ||
| 111 | * @return Event | ||
| 112 | */ | ||
| 113 | public static function importFromUDB2( | ||
| 129 | |||
| 130 | /** | ||
| 131 | * @param EventXmlString $xmlString | ||
| 132 | * @param String $eventId | ||
| 133 | * @param String $cdbXmlNamespaceUri | ||
| 134 | * @return Event | ||
| 135 | */ | ||
| 136 | public static function createFromCdbXml( | ||
| 152 | |||
| 153 | /** | ||
| 154 | * @param String $eventId | ||
| 155 | * @param EventXmlString $xmlString | ||
| 156 | * @param String $cdbXmlNamespaceUri | ||
| 157 | * @return Event | ||
| 158 | */ | ||
| 159 | public function updateFromCdbXml( | ||
| 172 | |||
| 173 | /** | ||
| 174 | * @param LabelCollection $labels | ||
| 175 | */ | ||
| 176 | public function mergeLabels(LabelCollection $labels) | ||
| 191 | |||
| 192 | /** | ||
| 193 | * @param Language $language | ||
| 194 | * @param String|null $title | ||
| 195 | * @param String|null $shortDescription | ||
| 196 | * @param String|null $longDescription | ||
| 197 | */ | ||
| 198 | public function applyTranslation( | ||
| 214 | |||
| 215 | /** | ||
| 216 | * @param Language $language | ||
| 217 | */ | ||
| 218 | public function deleteTranslation( | ||
| 219 | Language $language | ||
| 220 |     ) { | ||
| 221 |         if (!array_key_exists($language->getCode(), $this->translations)) { | ||
| 222 | return; | ||
| 223 | } | ||
| 224 | |||
| 225 | $this->apply( | ||
| 226 | new TranslationDeleted( | ||
| 227 | new String($this->eventId), | ||
| 228 | $language | ||
| 229 | ) | ||
| 230 | ); | ||
| 231 | } | ||
| 232 | |||
| 233 | /** | ||
| 234 | * @param Language $language | ||
| 235 | * @param CollaborationData $collaborationData | ||
| 236 | * @return bool | ||
| 237 | */ | ||
| 238 | protected function isSameCollaborationDataAlreadyPresent( | ||
| 250 | |||
| 251 | /** | ||
| 252 | * @param Language $language | ||
| 253 | * @param \CultuurNet\UDB3\CollaborationData $collaborationData | ||
| 254 | */ | ||
| 255 | public function addCollaborationData( | ||
| 271 | |||
| 272 | /** | ||
| 273 |      * {@inheritdoc} | ||
| 274 | */ | ||
| 275 | public function getAggregateRootId() | ||
| 279 | |||
| 280 | /** | ||
| 281 | * @return Translation[] | ||
| 282 | */ | ||
| 283 | public function getTranslations() | ||
| 287 | |||
| 288 | /** | ||
| 289 | * @return LabelCollection | ||
| 290 | */ | ||
| 291 | public function getLabels() | ||
| 295 | |||
| 296 | /** | ||
| 297 | * @param Label $label | ||
| 298 | */ | ||
| 299 | public function label(Label $label) | ||
| 300 |     { | ||
| 301 |         if (!$this->labels->contains($label)) { | ||
| 302 | $this->apply(new EventWasLabelled($this->eventId, $label)); | ||
| 303 | } | ||
| 304 | } | ||
| 305 | |||
| 306 | /** | ||
| 307 | * @param Label $label | ||
| 308 | */ | ||
| 309 | public function unlabel(Label $label) | ||
| 310 |     { | ||
| 311 |         if ($this->labels->contains($label)) { | ||
| 312 | $this->apply(new Unlabelled($this->eventId, $label)); | ||
| 313 | } | ||
| 314 | } | ||
| 315 | |||
| 316 | protected function applyEventCreated(EventCreated $eventCreated) | ||
| 317 |     { | ||
| 318 | $this->eventId = $eventCreated->getEventId(); | ||
| 319 | } | ||
| 320 | |||
| 321 | protected function applyEventWasLabelled(EventWasLabelled $eventLabelled) | ||
| 322 |     { | ||
| 323 | $newLabel = $eventLabelled->getLabel(); | ||
| 324 | |||
| 325 |         if (!$this->labels->contains($newLabel)) { | ||
| 326 | $this->labels = $this->labels->with($newLabel); | ||
| 327 | } | ||
| 328 | } | ||
| 329 | |||
| 330 | protected function applyUnlabelled(Unlabelled $unlabelled) | ||
| 331 |     { | ||
| 332 | $removedLabel = $unlabelled->getLabel(); | ||
| 333 | |||
| 334 | $this->labels = $this->labels->without($removedLabel); | ||
| 335 | } | ||
| 336 | |||
| 337 | protected function applyEventImportedFromUDB2( | ||
| 338 | EventImportedFromUDB2 $eventImported | ||
| 339 |     ) { | ||
| 340 | $this->eventId = $eventImported->getEventId(); | ||
| 341 | $this->setUDB2Data($eventImported); | ||
| 342 | } | ||
| 343 | |||
| 344 | /** | ||
| 345 | * @param EventUpdatedFromUDB2 $eventUpdated | ||
| 346 | */ | ||
| 347 | protected function applyEventUpdatedFromUDB2( | ||
| 348 | EventUpdatedFromUDB2 $eventUpdated | ||
| 349 |     ) { | ||
| 350 | $this->setUDB2Data($eventUpdated); | ||
| 351 | } | ||
| 352 | |||
| 353 | /** | ||
| 354 | * @param EventCdbXMLInterface $eventCdbXML | ||
| 355 | */ | ||
| 356 | protected function setUDB2Data( | ||
| 357 | EventCdbXMLInterface $eventCdbXML | ||
| 358 |     ) { | ||
| 359 | $udb2Event = EventItemFactory::createEventFromCdbXml( | ||
| 360 | $eventCdbXML->getCdbXmlNamespaceUri(), | ||
| 361 | $eventCdbXML->getCdbXml() | ||
| 362 | ); | ||
| 363 | |||
| 364 | $this->setLabelsFromUDB2Event($udb2Event); | ||
| 365 | } | ||
| 366 | |||
| 367 | /** | ||
| 368 | * @param Language $language | ||
| 369 | * @param string $title | ||
| 370 | */ | ||
| 371 | public function translateTitle(Language $language, $title) | ||
| 372 |     { | ||
| 373 | $this->apply(new TitleTranslated($this->eventId, $language, $title)); | ||
| 374 | } | ||
| 375 | |||
| 376 | /** | ||
| 377 | * @param Language $language | ||
| 378 | * @param string $description | ||
| 379 | */ | ||
| 380 | public function translateDescription(Language $language, $description) | ||
| 381 |     { | ||
| 382 | $this->apply( | ||
| 383 | new DescriptionTranslated($this->eventId, $language, $description) | ||
| 384 | ); | ||
| 385 | } | ||
| 386 | |||
| 387 | /** | ||
| 388 | * @param string $description | ||
| 389 | */ | ||
| 390 | public function updateDescription($description) | ||
| 391 |     { | ||
| 392 | $this->apply(new DescriptionUpdated($this->eventId, $description)); | ||
| 393 | } | ||
| 394 | |||
| 395 | /** | ||
| 396 | * @param string $typicalAgeRange | ||
| 397 | */ | ||
| 398 | public function updateTypicalAgeRange($typicalAgeRange) | ||
| 399 |     { | ||
| 400 | $this->apply(new TypicalAgeRangeUpdated($this->eventId, $typicalAgeRange)); | ||
| 401 | } | ||
| 402 | |||
| 403 | public function deleteTypicalAgeRange() | ||
| 404 |     { | ||
| 405 | $this->apply(new TypicalAgeRangeDeleted($this->eventId)); | ||
| 406 | } | ||
| 407 | |||
| 408 | /** | ||
| 409 | * @param string $organizerId | ||
| 410 | */ | ||
| 411 | public function updateOrganizer($organizerId) | ||
| 412 |     { | ||
| 413 | $this->apply(new OrganizerUpdated($this->eventId, $organizerId)); | ||
| 414 | } | ||
| 415 | |||
| 416 | /** | ||
| 417 | * Delete the given organizer. | ||
| 418 | * | ||
| 419 | * @param string $organizerId | ||
| 420 | */ | ||
| 421 | public function deleteOrganizer($organizerId) | ||
| 422 |     { | ||
| 423 | $this->apply(new OrganizerDeleted($this->eventId, $organizerId)); | ||
| 424 | } | ||
| 425 | |||
| 426 | /** | ||
| 427 | * Updated the contact info. | ||
| 428 | * | ||
| 429 | * @param array $phones | ||
| 430 | * @param array $emails | ||
| 431 | * @param array $urls | ||
| 432 | */ | ||
| 433 | public function updateContactPoint(ContactPoint $contactPoint) | ||
| 434 |     { | ||
| 435 | $this->apply(new ContactPointUpdated($this->eventId, $contactPoint)); | ||
| 436 | } | ||
| 437 | |||
| 438 | /** | ||
| 439 | * Updated the booking info. | ||
| 440 | * | ||
| 441 | * @param BookingInfo $bookingInfo | ||
| 442 | */ | ||
| 443 | public function updateBookingInfo(BookingInfo $bookingInfo) | ||
| 444 |     { | ||
| 445 | $this->apply(new BookingInfoUpdated($this->eventId, $bookingInfo)); | ||
| 446 | } | ||
| 447 | |||
| 448 | /** | ||
| 449 | * @return boolean | ||
| 450 | */ | ||
| 451 | private function containsImage(Image $image) | ||
| 452 |     { | ||
| 453 | $equalImages = array_filter( | ||
| 454 | $this->mediaObjects, | ||
| 455 |             function ($existingMediaObjectId) use ($image) { | ||
| 456 | return $image | ||
| 457 | ->getMediaObjectId() | ||
| 458 | ->sameValueAs($existingMediaObjectId); | ||
| 459 | } | ||
| 460 | ); | ||
| 461 | |||
| 462 | return !empty($equalImages); | ||
| 463 | } | ||
| 464 | |||
| 465 | /** | ||
| 466 | * Add a new image. | ||
| 467 | * | ||
| 468 | * @param Image $image | ||
| 469 | */ | ||
| 470 | public function addImage(Image $image) | ||
| 471 |     { | ||
| 472 |         if (!$this->containsImage($image)) { | ||
| 473 | $this->apply(new ImageAdded($this->eventId, $image)); | ||
| 474 | } | ||
| 475 | } | ||
| 476 | |||
| 477 | /** | ||
| 478 | * @param UpdateImage $updateImageCommand | ||
| 479 | */ | ||
| 480 | public function updateImage(UpdateImage $updateImageCommand) | ||
| 481 |     { | ||
| 482 | $this->apply(new ImageUpdated( | ||
| 483 | $updateImageCommand->getItemId(), | ||
| 484 | $updateImageCommand->getMediaObjectId(), | ||
| 485 | $updateImageCommand->getDescription(), | ||
| 486 | $updateImageCommand->getCopyrightHolder() | ||
| 487 | )); | ||
| 488 | } | ||
| 489 | |||
| 490 | /** | ||
| 491 | * Remove an image. | ||
| 492 | * | ||
| 493 | * @param Image $image | ||
| 494 | */ | ||
| 495 | public function removeImage(Image $image) | ||
| 496 |     { | ||
| 497 |         if ($this->containsImage($image)) { | ||
| 498 | $this->apply(new ImageRemoved($this->eventId, $image)); | ||
| 499 | } | ||
| 500 | } | ||
| 501 | |||
| 502 | /** | ||
| 503 | * Update the major info. | ||
| 504 | * | ||
| 505 | * @param Title $title | ||
| 506 | * @param EventType $eventType | ||
| 507 | * @param Location $location | ||
| 508 | * @param CalendarInterface $calendar | ||
| 509 | * @param type $theme | ||
| 510 | */ | ||
| 511 | public function updateMajorInfo(Title $title, EventType $eventType, Location $location, CalendarInterface $calendar, $theme = null) | ||
| 515 | |||
| 516 | /** | ||
| 517 | * Delete this item. | ||
| 518 | */ | ||
| 519 | public function deleteEvent() | ||
| 523 | |||
| 524 | /** | ||
| 525 | * @param \CultureFeed_Cdb_Item_Event $udb2Event | ||
| 526 | */ | ||
| 527 | protected function setLabelsFromUDB2Event(\CultureFeed_Cdb_Item_Event $udb2Event) | ||
| 541 | |||
| 542 | protected function resetLabels() | ||
| 546 | |||
| 547 | protected function applyEventCreatedFromCdbXml( | ||
| 559 | |||
| 560 | View Code Duplication | protected function applyEventUpdatedFromCdbXml( | |
| 572 | |||
| 573 | protected function applyLabelsMerged( | ||
| 578 | |||
| 579 | protected function applyTranslationApplied( | ||
| 599 | |||
| 600 | protected function applyTranslationDeleted( | ||
| 609 | |||
| 610 | protected function applyCollaborationDataAdded( | ||
| 630 | |||
| 631 | public function updateWithCdbXml($cdbXml, $cdbXmlNamespaceUri) | ||
| 641 | |||
| 642 | protected function applyImageAdded(ImageAdded $imageAdded) | ||
| 646 | |||
| 647 | protected function applyImageRemoved(ImageRemoved $imageRemoved) | ||
| 654 | } | ||
| 655 | 
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.