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 |
||
| 61 | class Event extends Offer implements UpdateableWithCdbXmlInterface |
||
| 62 | { |
||
| 63 | protected $eventId; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var Translation[] |
||
| 67 | */ |
||
| 68 | protected $translations = []; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var CollaborationDataCollection[] |
||
| 72 | * Array of different collections, keyed by language. |
||
| 73 | */ |
||
| 74 | protected $collaborationData; |
||
| 75 | |||
| 76 | const MAIN_LANGUAGE_CODE = 'nl'; |
||
| 77 | |||
| 78 | public function __construct() |
||
| 79 | { |
||
| 80 | parent::__construct(); |
||
| 81 | } |
||
| 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 CalendarInterface $calendar |
||
| 90 | * @param Theme|null $theme |
||
| 91 | * @param \DateTimeImmutable|null $publicationDate |
||
| 92 | * |
||
| 93 | * @return Event |
||
| 94 | */ |
||
| 95 | public static function create( |
||
| 96 | $eventId, |
||
| 97 | Title $title, |
||
| 98 | EventType $eventType, |
||
| 99 | Location $location, |
||
| 100 | CalendarInterface $calendar, |
||
| 101 | Theme $theme = null, |
||
| 102 | \DateTimeImmutable $publicationDate = null |
||
| 103 | ) { |
||
| 104 | if (!is_string($eventId)) { |
||
| 105 | throw new \InvalidArgumentException( |
||
| 106 | 'Expected eventId to be a string, received ' . gettype($eventId) |
||
| 107 | ); |
||
| 108 | } |
||
| 109 | |||
| 110 | $event = new self(); |
||
| 111 | |||
| 112 | $event->apply( |
||
| 113 | new EventCreated( |
||
| 114 | $eventId, |
||
| 115 | $title, |
||
| 116 | $eventType, |
||
| 117 | $location, |
||
| 118 | $calendar, |
||
| 119 | $theme, |
||
| 120 | $publicationDate |
||
| 121 | ) |
||
| 122 | ); |
||
| 123 | |||
| 124 | return $event; |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @param string $eventId |
||
| 129 | * @param string $cdbXml |
||
| 130 | * @param string $cdbXmlNamespaceUri |
||
| 131 | * @return Event |
||
| 132 | */ |
||
| 133 | public static function importFromUDB2( |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @param EventXmlString $xmlString |
||
| 152 | * @param StringLiteral $eventId |
||
| 153 | * @param StringLiteral $cdbXmlNamespaceUri |
||
| 154 | * @return Event |
||
| 155 | */ |
||
| 156 | public static function createFromCdbXml( |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @param StringLiteral $eventId |
||
| 175 | * @param EventXmlString $xmlString |
||
| 176 | * @param StringLiteral $cdbXmlNamespaceUri |
||
| 177 | * @return Event |
||
| 178 | */ |
||
| 179 | public function updateFromCdbXml( |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @param LabelCollection $labels |
||
| 195 | */ |
||
| 196 | public function mergeLabels(LabelCollection $labels) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @param Language $language |
||
| 214 | * @param StringLiteral|null $title |
||
| 215 | * @param StringLiteral|null $shortDescription |
||
| 216 | * @param StringLiteral|null $longDescription |
||
| 217 | */ |
||
| 218 | public function applyTranslation( |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @param Language $language |
||
| 237 | */ |
||
| 238 | public function deleteTranslation( |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @param Language $language |
||
| 255 | * @param CollaborationData $collaborationData |
||
| 256 | * @return bool |
||
| 257 | */ |
||
| 258 | protected function isSameCollaborationDataAlreadyPresent( |
||
| 270 | |||
| 271 | /** |
||
| 272 | * @param Language $language |
||
| 273 | * @param \CultuurNet\UDB3\CollaborationData $collaborationData |
||
| 274 | */ |
||
| 275 | public function addCollaborationData( |
||
| 291 | |||
| 292 | /** |
||
| 293 | * {@inheritdoc} |
||
| 294 | */ |
||
| 295 | public function getAggregateRootId() |
||
| 299 | |||
| 300 | /** |
||
| 301 | * @return Translation[] |
||
| 302 | */ |
||
| 303 | public function getTranslations() |
||
| 307 | |||
| 308 | /** |
||
| 309 | * @return UUID[] |
||
| 310 | */ |
||
| 311 | public function getMediaObjects() |
||
| 315 | |||
| 316 | protected function applyEventCreated(EventCreated $eventCreated) |
||
| 321 | |||
| 322 | protected function applyEventImportedFromUDB2( |
||
| 328 | |||
| 329 | /** |
||
| 330 | * @param EventUpdatedFromUDB2 $eventUpdated |
||
| 331 | */ |
||
| 332 | protected function applyEventUpdatedFromUDB2( |
||
| 337 | |||
| 338 | /** |
||
| 339 | * @param EventCdbXMLInterface $eventCdbXML |
||
| 340 | */ |
||
| 341 | protected function setUDB2Data( |
||
| 342 | EventCdbXMLInterface $eventCdbXML |
||
| 343 | ) { |
||
| 344 | $udb2Event = EventItemFactory::createEventFromCdbXml( |
||
| 345 | $eventCdbXML->getCdbXmlNamespaceUri(), |
||
| 346 | $eventCdbXML->getCdbXml() |
||
| 347 | ); |
||
| 348 | |||
| 349 | $this->importWorkflowStatus($udb2Event); |
||
| 350 | $this->setLabelsFromUDB2Item($udb2Event); |
||
| 351 | } |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Update the major info. |
||
| 355 | * |
||
| 356 | * @param Title $title |
||
| 357 | * @param EventType $eventType |
||
| 358 | * @param Location $location |
||
| 359 | * @param CalendarInterface $calendar |
||
| 360 | * @param type $theme |
||
| 361 | */ |
||
| 362 | public function updateMajorInfo( |
||
| 363 | Title $title, |
||
| 364 | EventType $eventType, |
||
| 365 | Location $location, |
||
| 366 | CalendarInterface $calendar, |
||
| 367 | $theme = null |
||
| 368 | ) { |
||
| 369 | $this->apply(new MajorInfoUpdated($this->eventId, $title, $eventType, $location, $calendar, $theme)); |
||
| 370 | } |
||
| 371 | |||
| 372 | View Code Duplication | protected function applyEventCreatedFromCdbXml( |
|
| 373 | EventCreatedFromCdbXml $eventCreatedFromCdbXml |
||
| 374 | ) { |
||
| 375 | $this->eventId = $eventCreatedFromCdbXml->getEventId()->toNative(); |
||
| 376 | |||
| 377 | $udb2Event = EventItemFactory::createEventFromCdbXml( |
||
| 378 | $eventCreatedFromCdbXml->getCdbXmlNamespaceUri(), |
||
| 379 | $eventCreatedFromCdbXml->getEventXmlString()->toEventXmlString() |
||
| 380 | ); |
||
| 381 | |||
| 382 | $this->setLabelsFromUDB2Item($udb2Event); |
||
| 383 | } |
||
| 384 | |||
| 385 | View Code Duplication | protected function applyEventUpdatedFromCdbXml( |
|
| 386 | EventUpdatedFromCdbXml $eventUpdatedFromCdbXml |
||
| 387 | ) { |
||
| 388 | $this->eventId = $eventUpdatedFromCdbXml->getEventId()->toNative(); |
||
| 389 | |||
| 390 | $udb2Event = EventItemFactory::createEventFromCdbXml( |
||
| 391 | $eventUpdatedFromCdbXml->getCdbXmlNamespaceUri(), |
||
| 392 | $eventUpdatedFromCdbXml->getEventXmlString()->toEventXmlString() |
||
| 393 | ); |
||
| 394 | |||
| 395 | $this->setLabelsFromUDB2Item($udb2Event); |
||
| 396 | } |
||
| 397 | |||
| 398 | protected function applyLabelsMerged( |
||
| 399 | LabelsMerged $labelsMerged |
||
| 400 | ) { |
||
| 401 | $this->labels = $this->labels->merge($labelsMerged->getLabels()); |
||
| 402 | } |
||
| 403 | |||
| 404 | protected function applyTranslationApplied( |
||
| 405 | TranslationApplied $translationApplied |
||
| 406 | ) { |
||
| 407 | $this->eventId = $translationApplied->getEventId()->toNative(); |
||
| 408 | |||
| 409 | $language = $translationApplied->getLanguage()->getCode(); |
||
| 410 | $translation = new Translation( |
||
| 411 | $translationApplied->getLanguage(), |
||
| 412 | $translationApplied->getTitle(), |
||
| 413 | $translationApplied->getShortdescription(), |
||
| 414 | $translationApplied->getLongdescription() |
||
| 415 | ); |
||
| 416 | |||
| 417 | if (!array_key_exists($language, $this->translations)) { |
||
| 418 | $this->translations[$language] = $translation; |
||
| 419 | } else { |
||
| 420 | $newTranslation = $this->translations[$language]->mergeTranslation($translation); |
||
| 421 | $this->translations[$language] = $newTranslation; |
||
| 422 | } |
||
| 423 | } |
||
| 424 | |||
| 425 | protected function applyTranslationDeleted( |
||
| 426 | TranslationDeleted $translationDeleted |
||
| 427 | ) { |
||
| 428 | $language = $translationDeleted->getLanguage()->getCode(); |
||
| 429 | |||
| 430 | if (array_key_exists($language, $this->translations)) { |
||
| 431 | unset($this->translations[$language]); |
||
| 432 | } |
||
| 433 | } |
||
| 434 | |||
| 435 | protected function applyCollaborationDataAdded( |
||
| 436 | CollaborationDataAdded $collaborationDataAdded |
||
| 437 | ) { |
||
| 438 | $language = $collaborationDataAdded->getLanguage()->getCode(); |
||
| 439 | $collaborationData = $collaborationDataAdded->getCollaborationData(); |
||
| 440 | |||
| 441 | if (!isset($this->collaborationData[$language])) { |
||
| 442 | $this->collaborationData[$language] = new CollaborationDataCollection(); |
||
| 443 | } |
||
| 444 | |||
| 445 | if ($this->collaborationData[$language]->contains($collaborationData)) { |
||
| 446 | return; |
||
| 447 | } |
||
| 448 | |||
| 449 | $this->collaborationData[$language] = $this->collaborationData[$language] |
||
| 450 | ->withKey( |
||
| 451 | $collaborationData->getSubBrand()->toNative(), |
||
| 452 | $collaborationData |
||
| 453 | ); |
||
| 454 | } |
||
| 455 | |||
| 456 | /** |
||
| 457 | * @inheritdoc |
||
| 458 | */ |
||
| 459 | public function updateWithCdbXml($cdbXml, $cdbXmlNamespaceUri) |
||
| 460 | { |
||
| 461 | $this->apply( |
||
| 462 | new EventUpdatedFromUDB2( |
||
| 463 | $this->eventId, |
||
| 464 | $cdbXml, |
||
| 465 | $cdbXmlNamespaceUri |
||
| 466 | ) |
||
| 467 | ); |
||
| 468 | } |
||
| 469 | |||
| 470 | /** |
||
| 471 | * @param Label $label |
||
| 472 | * @return LabelAdded |
||
| 473 | */ |
||
| 474 | protected function createLabelAddedEvent(Label $label) |
||
| 475 | { |
||
| 476 | return new LabelAdded($this->eventId, $label); |
||
| 477 | } |
||
| 478 | |||
| 479 | /** |
||
| 480 | * @param Label $label |
||
| 481 | * @return LabelDeleted |
||
| 482 | */ |
||
| 483 | protected function createLabelDeletedEvent(Label $label) |
||
| 484 | { |
||
| 485 | return new LabelDeleted($this->eventId, $label); |
||
| 486 | } |
||
| 487 | |||
| 488 | /** |
||
| 489 | * @param Image $image |
||
| 490 | * @return ImageAdded |
||
| 491 | */ |
||
| 492 | protected function createImageAddedEvent(Image $image) |
||
| 493 | { |
||
| 494 | return new ImageAdded($this->eventId, $image); |
||
| 495 | } |
||
| 496 | |||
| 497 | /** |
||
| 498 | * @param Image $image |
||
| 499 | * @return ImageRemoved |
||
| 500 | */ |
||
| 501 | protected function createImageRemovedEvent(Image $image) |
||
| 502 | { |
||
| 503 | return new ImageRemoved($this->eventId, $image); |
||
| 504 | } |
||
| 505 | |||
| 506 | /** |
||
| 507 | * @param AbstractUpdateImage $updateImageCommand |
||
| 508 | * @return ImageUpdated |
||
| 509 | */ |
||
| 510 | protected function createImageUpdatedEvent( |
||
| 511 | AbstractUpdateImage $updateImageCommand |
||
| 512 | ) { |
||
| 513 | return new ImageUpdated( |
||
| 514 | $this->eventId, |
||
| 515 | $updateImageCommand->getMediaObjectId(), |
||
| 516 | $updateImageCommand->getDescription(), |
||
| 517 | $updateImageCommand->getCopyrightHolder() |
||
| 518 | ); |
||
| 519 | } |
||
| 520 | |||
| 521 | /** |
||
| 522 | * @param Image $image |
||
| 523 | * @return MainImageSelected |
||
| 524 | */ |
||
| 525 | protected function createMainImageSelectedEvent(Image $image) |
||
| 526 | { |
||
| 527 | return new MainImageSelected($this->eventId, $image); |
||
| 528 | } |
||
| 529 | |||
| 530 | /** |
||
| 531 | * @param Language $language |
||
| 532 | * @param StringLiteral $title |
||
| 533 | * @return TitleTranslated |
||
| 534 | */ |
||
| 535 | protected function createTitleTranslatedEvent(Language $language, StringLiteral $title) |
||
| 536 | { |
||
| 537 | return new TitleTranslated($this->eventId, $language, $title); |
||
| 538 | } |
||
| 539 | |||
| 540 | /** |
||
| 541 | * @param Language $language |
||
| 542 | * @param StringLiteral $description |
||
| 543 | * @return DescriptionTranslated |
||
| 544 | */ |
||
| 545 | protected function createDescriptionTranslatedEvent(Language $language, StringLiteral $description) |
||
| 546 | { |
||
| 547 | return new DescriptionTranslated($this->eventId, $language, $description); |
||
| 548 | } |
||
| 549 | |||
| 550 | /** |
||
| 551 | * @param string $description |
||
| 552 | * @return DescriptionUpdated |
||
| 553 | */ |
||
| 554 | protected function createDescriptionUpdatedEvent($description) |
||
| 555 | { |
||
| 556 | return new DescriptionUpdated($this->eventId, $description); |
||
| 557 | } |
||
| 558 | |||
| 559 | /** |
||
| 560 | * @param string $typicalAgeRange |
||
| 561 | * @return TypicalAgeRangeUpdated |
||
| 562 | */ |
||
| 563 | protected function createTypicalAgeRangeUpdatedEvent($typicalAgeRange) |
||
| 564 | { |
||
| 565 | return new TypicalAgeRangeUpdated($this->eventId, $typicalAgeRange); |
||
| 566 | } |
||
| 567 | |||
| 568 | /** |
||
| 569 | * @return TypicalAgeRangeDeleted |
||
| 570 | */ |
||
| 571 | protected function createTypicalAgeRangeDeletedEvent() |
||
| 572 | { |
||
| 573 | return new TypicalAgeRangeDeleted($this->eventId); |
||
| 574 | } |
||
| 575 | |||
| 576 | /** |
||
| 577 | * @param string $organizerId |
||
| 578 | * @return OrganizerUpdated |
||
| 579 | */ |
||
| 580 | protected function createOrganizerUpdatedEvent($organizerId) |
||
| 581 | { |
||
| 582 | return new OrganizerUpdated($this->eventId, $organizerId); |
||
| 583 | } |
||
| 584 | |||
| 585 | /** |
||
| 586 | * @param string $organizerId |
||
| 587 | * @return OrganizerDeleted |
||
| 588 | */ |
||
| 589 | protected function createOrganizerDeletedEvent($organizerId) |
||
| 590 | { |
||
| 591 | return new OrganizerDeleted($this->eventId, $organizerId); |
||
| 592 | } |
||
| 593 | |||
| 594 | /** |
||
| 595 | * @param ContactPoint $contactPoint |
||
| 596 | * @return ContactPointUpdated |
||
| 597 | */ |
||
| 598 | protected function createContactPointUpdatedEvent(ContactPoint $contactPoint) |
||
| 599 | { |
||
| 600 | return new ContactPointUpdated($this->eventId, $contactPoint); |
||
| 601 | } |
||
| 602 | |||
| 603 | /** |
||
| 604 | * @param BookingInfo $bookingInfo |
||
| 605 | * @return BookingInfoUpdated |
||
| 606 | */ |
||
| 607 | protected function createBookingInfoUpdatedEvent(BookingInfo $bookingInfo) |
||
| 608 | { |
||
| 609 | return new BookingInfoUpdated($this->eventId, $bookingInfo); |
||
| 610 | } |
||
| 611 | |||
| 612 | /** |
||
| 613 | * @param PriceInfo $priceInfo |
||
| 614 | * @return PriceInfoUpdated |
||
| 615 | */ |
||
| 616 | protected function createPriceInfoUpdatedEvent(PriceInfo $priceInfo) |
||
| 617 | { |
||
| 618 | return new PriceInfoUpdated($this->eventId, $priceInfo); |
||
| 619 | } |
||
| 620 | |||
| 621 | /** |
||
| 622 | * @return EventDeleted |
||
| 623 | */ |
||
| 624 | protected function createOfferDeletedEvent() |
||
| 625 | { |
||
| 626 | return new EventDeleted($this->eventId); |
||
| 627 | } |
||
| 628 | |||
| 629 | /** |
||
| 630 | * @inheritdoc |
||
| 631 | */ |
||
| 632 | protected function createPublishedEvent(\DateTimeInterface $publicationDate) |
||
| 633 | { |
||
| 634 | return new Published($this->eventId, $publicationDate); |
||
| 635 | } |
||
| 636 | |||
| 637 | /** |
||
| 638 | * @inheritdoc |
||
| 639 | */ |
||
| 640 | protected function createApprovedEvent() |
||
| 644 | |||
| 645 | /** |
||
| 646 | * @inheritdoc |
||
| 647 | */ |
||
| 648 | protected function createRejectedEvent(StringLiteral $reason) |
||
| 652 | |||
| 653 | /** |
||
| 654 | * @inheritDoc |
||
| 655 | */ |
||
| 656 | protected function createFlaggedAsDuplicate() |
||
| 660 | |||
| 661 | /** |
||
| 662 | * @inheritDoc |
||
| 663 | */ |
||
| 664 | protected function createFlaggedAsInappropriate() |
||
| 668 | } |
||
| 669 |
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.