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 | * @return UUID[] |
||
| 298 | */ |
||
| 299 | public function getMediaObjects() |
||
| 303 | |||
| 304 | /** |
||
| 305 | * @param Label $label |
||
| 306 | */ |
||
| 307 | public function label(Label $label) |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @param Label $label |
||
| 316 | */ |
||
| 317 | public function unlabel(Label $label) |
||
| 323 | |||
| 324 | protected function applyEventCreated(EventCreated $eventCreated) |
||
| 328 | |||
| 329 | protected function applyEventWasLabelled(EventWasLabelled $eventLabelled) |
||
| 337 | |||
| 338 | protected function applyUnlabelled(Unlabelled $unlabelled) |
||
| 344 | |||
| 345 | protected function applyEventImportedFromUDB2( |
||
| 351 | |||
| 352 | /** |
||
| 353 | * @param EventUpdatedFromUDB2 $eventUpdated |
||
| 354 | */ |
||
| 355 | protected function applyEventUpdatedFromUDB2( |
||
| 356 | EventUpdatedFromUDB2 $eventUpdated |
||
| 357 | ) { |
||
| 358 | $this->setUDB2Data($eventUpdated); |
||
| 359 | } |
||
| 360 | |||
| 361 | /** |
||
| 362 | * @param EventCdbXMLInterface $eventCdbXML |
||
| 363 | */ |
||
| 364 | protected function setUDB2Data( |
||
| 374 | |||
| 375 | /** |
||
| 376 | * @param Language $language |
||
| 377 | * @param string $title |
||
| 378 | */ |
||
| 379 | public function translateTitle(Language $language, $title) |
||
| 383 | |||
| 384 | /** |
||
| 385 | * @param Language $language |
||
| 386 | * @param string $description |
||
| 387 | */ |
||
| 388 | public function translateDescription(Language $language, $description) |
||
| 394 | |||
| 395 | /** |
||
| 396 | * @param string $description |
||
| 397 | */ |
||
| 398 | public function updateDescription($description) |
||
| 402 | |||
| 403 | /** |
||
| 404 | * @param string $typicalAgeRange |
||
| 405 | */ |
||
| 406 | public function updateTypicalAgeRange($typicalAgeRange) |
||
| 410 | |||
| 411 | public function deleteTypicalAgeRange() |
||
| 415 | |||
| 416 | /** |
||
| 417 | * @param string $organizerId |
||
| 418 | */ |
||
| 419 | public function updateOrganizer($organizerId) |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Delete the given organizer. |
||
| 426 | * |
||
| 427 | * @param string $organizerId |
||
| 428 | */ |
||
| 429 | public function deleteOrganizer($organizerId) |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Updated the contact info. |
||
| 436 | * |
||
| 437 | * @param array $phones |
||
| 438 | * @param array $emails |
||
| 439 | * @param array $urls |
||
| 440 | */ |
||
| 441 | public function updateContactPoint(ContactPoint $contactPoint) |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Updated the booking info. |
||
| 448 | * |
||
| 449 | * @param BookingInfo $bookingInfo |
||
| 450 | */ |
||
| 451 | public function updateBookingInfo(BookingInfo $bookingInfo) |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Add a new image. |
||
| 458 | * |
||
| 459 | * @param Image $image |
||
| 460 | * @throws DuplicateMediaObjectException |
||
| 461 | */ |
||
| 462 | public function addImage(Image $image) |
||
| 477 | |||
| 478 | |||
| 479 | /** |
||
| 480 | * @param UpdateImage $updateImageCommand |
||
| 481 | */ |
||
| 482 | public function updateImage(UpdateImage $updateImageCommand) |
||
| 491 | |||
| 492 | /** |
||
| 493 | * Remove an image. |
||
| 494 | * |
||
| 495 | * @param Image $image |
||
| 496 | */ |
||
| 497 | public function removeImage(Image $image) |
||
| 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.