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 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) |
||
301 | |||
302 | public function delete() |
||
308 | |||
309 | /** |
||
310 | * Apply the organizer created event. |
||
311 | * @param OrganizerCreated $organizerCreated |
||
312 | */ |
||
313 | protected function applyOrganizerCreated(OrganizerCreated $organizerCreated) |
||
321 | |||
322 | /** |
||
323 | * Apply the organizer created event. |
||
324 | * @param OrganizerCreatedWithUniqueWebsite $organizerCreated |
||
325 | */ |
||
326 | protected function applyOrganizerCreatedWithUniqueWebsite(OrganizerCreatedWithUniqueWebsite $organizerCreated) |
||
336 | |||
337 | /** |
||
338 | * @param OrganizerImportedFromUDB2 $organizerImported |
||
339 | */ |
||
340 | protected function applyOrganizerImportedFromUDB2( |
||
357 | |||
358 | /** |
||
359 | * @param OrganizerUpdatedFromUDB2 $organizerUpdatedFromUDB2 |
||
360 | */ |
||
361 | protected function applyOrganizerUpdatedFromUDB2( |
||
375 | |||
376 | /** |
||
377 | * @param WebsiteUpdated $websiteUpdated |
||
378 | */ |
||
379 | protected function applyWebsiteUpdated(WebsiteUpdated $websiteUpdated) |
||
383 | |||
384 | /** |
||
385 | * @param TitleUpdated $titleUpdated |
||
386 | */ |
||
387 | protected function applyTitleUpdated(TitleUpdated $titleUpdated) |
||
391 | |||
392 | /** |
||
393 | * @param TitleTranslated $titleTranslated |
||
394 | */ |
||
395 | protected function applyTitleTranslated(TitleTranslated $titleTranslated) |
||
399 | |||
400 | /** |
||
401 | * @param AddressUpdated $addressUpdated |
||
402 | */ |
||
403 | protected function applyAddressUpdated(AddressUpdated $addressUpdated) |
||
407 | |||
408 | /** |
||
409 | * @param ContactPointUpdated $contactPointUpdated |
||
410 | */ |
||
411 | protected function applyContactPointUpdated(ContactPointUpdated $contactPointUpdated) |
||
415 | |||
416 | /** |
||
417 | * @param LabelAdded $labelAdded |
||
418 | */ |
||
419 | protected function applyLabelAdded(LabelAdded $labelAdded) |
||
423 | |||
424 | /** |
||
425 | * @param LabelRemoved $labelRemoved |
||
426 | */ |
||
427 | protected function applyLabelRemoved(LabelRemoved $labelRemoved) |
||
431 | |||
432 | /** |
||
433 | * @param \CultureFeed_Cdb_Item_Actor $actor |
||
434 | * @return null|Title |
||
435 | */ |
||
436 | private function getTitle(\CultureFeed_Cdb_Item_Actor $actor) |
||
450 | |||
451 | /** |
||
452 | * @param Title $title |
||
453 | * @param Language $language |
||
454 | */ |
||
455 | private function setTitle(Title $title, Language $language) |
||
459 | |||
460 | /** |
||
461 | * @param Title $title |
||
462 | * @param Language $language |
||
463 | * @return bool |
||
464 | */ |
||
465 | private function isTitleChanged(Title $title, Language $language) |
||
470 | } |
||
471 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.