Complex classes like Organizer 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 Organizer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | class Organizer extends EventSourcedAggregateRoot implements UpdateableWithCdbXmlInterface, LabelAwareAggregateRoot |
||
| 34 | { |
||
| 35 | /** |
||
| 36 | * The actor id. |
||
| 37 | * |
||
| 38 | * @var string |
||
| 39 | */ |
||
| 40 | protected $actorId; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var Language |
||
| 44 | */ |
||
| 45 | private $mainLanguage; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var Url |
||
| 49 | */ |
||
| 50 | private $website; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var Title[] |
||
| 54 | */ |
||
| 55 | private $titles; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var Address|null |
||
| 59 | */ |
||
| 60 | private $address; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var ContactPoint |
||
| 64 | */ |
||
| 65 | private $contactPoint; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var LabelCollection|Label[] |
||
| 69 | */ |
||
| 70 | private $labels; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * {@inheritdoc} |
||
| 74 | */ |
||
| 75 | public function getAggregateRootId() |
||
| 76 | { |
||
| 77 | return $this->actorId; |
||
| 78 | } |
||
| 79 | |||
| 80 | public function __construct() |
||
| 81 | { |
||
| 82 | // Contact points can be empty, but we only want to start recording |
||
| 83 | // ContactPointUpdated events as soon as the organizer is updated |
||
| 84 | // with a non-empty contact point. To enforce this we initialize the |
||
| 85 | // aggregate state with an empty contact point. |
||
| 86 | $this->contactPoint = new ContactPoint(); |
||
| 87 | $this->labels = new LabelCollection(); |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Import from UDB2. |
||
| 92 | * |
||
| 93 | * @param string $actorId |
||
| 94 | * The actor id. |
||
| 95 | * @param string $cdbXml |
||
| 96 | * The cdb xml. |
||
| 97 | * @param string $cdbXmlNamespaceUri |
||
| 98 | * The cdb xml namespace uri. |
||
| 99 | * |
||
| 100 | * @return Organizer |
||
| 101 | * The actor. |
||
| 102 | */ |
||
| 103 | public static function importFromUDB2( |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Factory method to create a new Organizer. |
||
| 122 | * |
||
| 123 | * @param string $id |
||
| 124 | * @param Language $mainLanguage |
||
| 125 | * @param Url $website |
||
| 126 | * @param Title $title |
||
| 127 | * @return Organizer |
||
| 128 | */ |
||
| 129 | public static function create( |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @inheritdoc |
||
| 146 | */ |
||
| 147 | public function updateWithCdbXml($cdbXml, $cdbXmlNamespaceUri) |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @param Url $website |
||
| 160 | */ |
||
| 161 | public function updateWebsite(Url $website) |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @param Title $title |
||
| 175 | * @param Language $language |
||
| 176 | */ |
||
| 177 | public function updateTitle( |
||
| 198 | |||
| 199 | /** |
||
| 200 | * @param Address $address |
||
| 201 | */ |
||
| 202 | public function updateAddress(Address $address) |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @param ContactPoint $contactPoint |
||
| 213 | */ |
||
| 214 | public function updateContactPoint(ContactPoint $contactPoint) |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @inheritdoc |
||
| 225 | */ |
||
| 226 | public function addLabel(Label $label) |
||
| 232 | |||
| 233 | /** |
||
| 234 | * @inheritdoc |
||
| 235 | */ |
||
| 236 | public function removeLabel(Label $label) |
||
| 242 | |||
| 243 | /** |
||
| 244 | * @param Labels $labels |
||
| 245 | */ |
||
| 246 | public function importLabels(Labels $labels) |
||
| 299 | |||
| 300 | public function delete() |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Apply the organizer created event. |
||
| 309 | * @param OrganizerCreated $organizerCreated |
||
| 310 | */ |
||
| 311 | protected function applyOrganizerCreated(OrganizerCreated $organizerCreated) |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Apply the organizer created event. |
||
| 322 | * @param OrganizerCreatedWithUniqueWebsite $organizerCreated |
||
| 323 | */ |
||
| 324 | protected function applyOrganizerCreatedWithUniqueWebsite(OrganizerCreatedWithUniqueWebsite $organizerCreated) |
||
| 334 | |||
| 335 | /** |
||
| 336 | * @param OrganizerImportedFromUDB2 $organizerImported |
||
| 337 | */ |
||
| 338 | protected function applyOrganizerImportedFromUDB2( |
||
| 355 | |||
| 356 | /** |
||
| 357 | * @param OrganizerUpdatedFromUDB2 $organizerUpdatedFromUDB2 |
||
| 358 | */ |
||
| 359 | protected function applyOrganizerUpdatedFromUDB2( |
||
| 373 | |||
| 374 | /** |
||
| 375 | * @param WebsiteUpdated $websiteUpdated |
||
| 376 | */ |
||
| 377 | protected function applyWebsiteUpdated(WebsiteUpdated $websiteUpdated) |
||
| 381 | |||
| 382 | /** |
||
| 383 | * @param TitleUpdated $titleUpdated |
||
| 384 | */ |
||
| 385 | protected function applyTitleUpdated(TitleUpdated $titleUpdated) |
||
| 389 | |||
| 390 | /** |
||
| 391 | * @param TitleTranslated $titleTranslated |
||
| 392 | */ |
||
| 393 | protected function applyTitleTranslated(TitleTranslated $titleTranslated) |
||
| 397 | |||
| 398 | /** |
||
| 399 | * @param AddressUpdated $addressUpdated |
||
| 400 | */ |
||
| 401 | protected function applyAddressUpdated(AddressUpdated $addressUpdated) |
||
| 405 | |||
| 406 | /** |
||
| 407 | * @param ContactPointUpdated $contactPointUpdated |
||
| 408 | */ |
||
| 409 | protected function applyContactPointUpdated(ContactPointUpdated $contactPointUpdated) |
||
| 413 | |||
| 414 | /** |
||
| 415 | * @param LabelAdded $labelAdded |
||
| 416 | */ |
||
| 417 | protected function applyLabelAdded(LabelAdded $labelAdded) |
||
| 421 | |||
| 422 | /** |
||
| 423 | * @param LabelRemoved $labelRemoved |
||
| 424 | */ |
||
| 425 | protected function applyLabelRemoved(LabelRemoved $labelRemoved) |
||
| 429 | |||
| 430 | /** |
||
| 431 | * @param \CultureFeed_Cdb_Item_Actor $actor |
||
| 432 | * @return null|Title |
||
| 433 | */ |
||
| 434 | private function getTitle(\CultureFeed_Cdb_Item_Actor $actor) |
||
| 448 | |||
| 449 | /** |
||
| 450 | * @param Title $title |
||
| 451 | * @param Language $language |
||
| 452 | */ |
||
| 453 | private function setTitle(Title $title, Language $language) |
||
| 457 | |||
| 458 | /** |
||
| 459 | * @param Title $title |
||
| 460 | * @param Language $language |
||
| 461 | * @return bool |
||
| 462 | */ |
||
| 463 | private function isTitleChanged(Title $title, Language $language) |
||
| 468 | } |
||
| 469 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: