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 |
||
| 46 | class Event extends EventSourcedAggregateRoot |
||
| 47 | { |
||
| 48 | protected $eventId; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var LabelCollection |
||
| 52 | */ |
||
| 53 | protected $labels; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var Translation[] |
||
| 57 | */ |
||
| 58 | protected $translations = []; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var CollaborationDataCollection[] |
||
| 62 | * Array of different collections, keyed by language. |
||
| 63 | */ |
||
| 64 | protected $collaborationData; |
||
| 65 | |||
| 66 | const MAIN_LANGUAGE_CODE = 'nl'; |
||
| 67 | |||
| 68 | public function __construct() |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Factory method to create a new event. |
||
| 75 | * |
||
| 76 | * @param Title $title |
||
| 77 | * @param EventType $eventType |
||
| 78 | * @param Location $location |
||
| 79 | * @param CalendarBase $calendar |
||
| 80 | * @param Theme/null $theme |
||
|
|
|||
| 81 | * |
||
| 82 | * @return Event |
||
| 83 | */ |
||
| 84 | public static function create($eventId, Title $title, EventType $eventType, Location $location, CalendarInterface $calendar, $theme = null) |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @param string $eventId |
||
| 99 | * @param string $cdbXml |
||
| 100 | * @param string $cdbXmlNamespaceUri |
||
| 101 | * @return Event |
||
| 102 | */ |
||
| 103 | public static function importFromUDB2( |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @param EventXmlString $xmlString |
||
| 122 | * @param String $eventId |
||
| 123 | * @param String $cdbXmlNamespaceUri |
||
| 124 | * @return Event |
||
| 125 | */ |
||
| 126 | public static function createFromCdbXml( |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @param String $eventId |
||
| 145 | * @param EventXmlString $xmlString |
||
| 146 | * @param String $cdbXmlNamespaceUri |
||
| 147 | * @return Event |
||
| 148 | */ |
||
| 149 | public function updateFromCdbXml( |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @param LabelCollection $labels |
||
| 165 | */ |
||
| 166 | public function mergeLabels(LabelCollection $labels) |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @param Language $language |
||
| 184 | * @param String|null $title |
||
| 185 | * @param String|null $shortDescription |
||
| 186 | * @param String|null $longDescription |
||
| 187 | */ |
||
| 188 | public function applyTranslation( |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @param Language $language |
||
| 207 | */ |
||
| 208 | public function deleteTranslation( |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @param Language $language |
||
| 225 | * @param CollaborationData $collaborationData |
||
| 226 | * @return bool |
||
| 227 | */ |
||
| 228 | protected function isSameCollaborationDataAlreadyPresent( |
||
| 240 | |||
| 241 | /** |
||
| 242 | * @param Language $language |
||
| 243 | * @param \CultuurNet\UDB3\CollaborationData $collaborationData |
||
| 244 | */ |
||
| 245 | public function addCollaborationData( |
||
| 261 | |||
| 262 | /** |
||
| 263 | * {@inheritdoc} |
||
| 264 | */ |
||
| 265 | public function getAggregateRootId() |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @return Translation[] |
||
| 272 | */ |
||
| 273 | public function getTranslations() |
||
| 277 | |||
| 278 | /** |
||
| 279 | * @return LabelCollection |
||
| 280 | */ |
||
| 281 | public function getLabels() |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @param Label $label |
||
| 288 | */ |
||
| 289 | public function label(Label $label) |
||
| 295 | |||
| 296 | /** |
||
| 297 | * @param Label $label |
||
| 298 | */ |
||
| 299 | public function unlabel(Label $label) |
||
| 305 | |||
| 306 | protected function applyEventCreated(EventCreated $eventCreated) |
||
| 310 | |||
| 311 | protected function applyEventWasLabelled(EventWasLabelled $eventLabelled) |
||
| 319 | |||
| 320 | protected function applyUnlabelled(Unlabelled $unlabelled) |
||
| 326 | |||
| 327 | protected function applyEventImportedFromUDB2( |
||
| 333 | |||
| 334 | /** |
||
| 335 | * @param EventUpdatedFromUDB2 $eventUpdated |
||
| 336 | */ |
||
| 337 | protected function applyEventUpdatedFromUDB2( |
||
| 342 | |||
| 343 | /** |
||
| 344 | * @param EventCdbXMLInterface $eventCdbXML |
||
| 345 | */ |
||
| 346 | protected function setUDB2Data( |
||
| 356 | |||
| 357 | /** |
||
| 358 | * @param Language $language |
||
| 359 | * @param string $title |
||
| 360 | */ |
||
| 361 | public function translateTitle(Language $language, $title) |
||
| 365 | |||
| 366 | /** |
||
| 367 | * @param Language $language |
||
| 368 | * @param string $description |
||
| 369 | */ |
||
| 370 | public function translateDescription(Language $language, $description) |
||
| 376 | |||
| 377 | /** |
||
| 378 | * @param string $description |
||
| 379 | */ |
||
| 380 | public function updateDescription($description) |
||
| 384 | |||
| 385 | /** |
||
| 386 | * @param string $typicalAgeRange |
||
| 387 | */ |
||
| 388 | public function updateTypicalAgeRange($typicalAgeRange) |
||
| 392 | |||
| 393 | public function deleteTypicalAgeRange() |
||
| 397 | |||
| 398 | /** |
||
| 399 | * @param string $organizerId |
||
| 400 | */ |
||
| 401 | public function updateOrganizer($organizerId) |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Delete the given organizer. |
||
| 408 | * |
||
| 409 | * @param string $organizerId |
||
| 410 | */ |
||
| 411 | public function deleteOrganizer($organizerId) |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Updated the contact info. |
||
| 418 | * |
||
| 419 | * @param array $phones |
||
| 420 | * @param array $emails |
||
| 421 | * @param array $urls |
||
| 422 | */ |
||
| 423 | public function updateContactPoint(ContactPoint $contactPoint) |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Updated the booking info. |
||
| 430 | * |
||
| 431 | * @param BookingInfo $bookingInfo |
||
| 432 | */ |
||
| 433 | public function updateBookingInfo(BookingInfo $bookingInfo) |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Add a new image. |
||
| 440 | * |
||
| 441 | * @param MediaObject $mediaObject |
||
| 442 | */ |
||
| 443 | public function addImage(MediaObject $mediaObject) |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Update an image. |
||
| 450 | * |
||
| 451 | * @param int $indexToUpdate |
||
| 452 | * @param MediaObject $mediaObject |
||
| 453 | */ |
||
| 454 | public function updateImage($indexToUpdate, MediaObject $mediaObject) |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Delete an image. |
||
| 461 | * |
||
| 462 | * @param int $indexToDelete |
||
| 463 | * @param mixed int|string $internalId |
||
| 464 | */ |
||
| 465 | public function deleteImage($indexToDelete, $internalId) |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Update the major info. |
||
| 472 | * |
||
| 473 | * @param Title $title |
||
| 474 | * @param EventType $eventType |
||
| 475 | * @param Location $location |
||
| 476 | * @param CalendarInterface $calendar |
||
| 477 | * @param type $theme |
||
| 478 | */ |
||
| 479 | public function updateMajorInfo(Title $title, EventType $eventType, Location $location, CalendarInterface $calendar, $theme = null) |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Delete this item. |
||
| 486 | */ |
||
| 487 | public function deleteEvent() |
||
| 491 | |||
| 492 | /** |
||
| 493 | * @param \CultureFeed_Cdb_Item_Event $udb2Event |
||
| 494 | */ |
||
| 495 | protected function setLabelsFromUDB2Event(\CultureFeed_Cdb_Item_Event $udb2Event) |
||
| 509 | |||
| 510 | protected function resetLabels() |
||
| 514 | |||
| 515 | protected function applyEventCreatedFromCdbXml( |
||
| 527 | |||
| 528 | View Code Duplication | protected function applyEventUpdatedFromCdbXml( |
|
| 540 | |||
| 541 | protected function applyLabelsMerged( |
||
| 546 | |||
| 547 | protected function applyTranslationApplied( |
||
| 567 | |||
| 568 | protected function applyTranslationDeleted( |
||
| 577 | |||
| 578 | protected function applyCollaborationDataAdded( |
||
| 595 | |||
| 596 | public function updateWithCdbXml($cdbXml, $cdbXmlNamespaceUri) |
||
| 606 | } |
||
| 607 |
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.