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 EventTrait 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 EventTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | trait EventTrait |
||
16 | { |
||
17 | private $_about; |
||
18 | |||
19 | /** |
||
20 | * @return Thing |
||
21 | */ |
||
22 | public function getAbout() |
||
26 | |||
27 | /** |
||
28 | * The subject matter of the content. |
||
29 | * Inverse property: subjectOf. |
||
30 | * |
||
31 | * @param Thing $about |
||
32 | * @return EventTrait |
||
|
|||
33 | */ |
||
34 | public function setAbout($about) |
||
39 | |||
40 | private $_actor; |
||
41 | |||
42 | /** |
||
43 | * @return Person |
||
44 | */ |
||
45 | public function getActor() |
||
49 | |||
50 | /** |
||
51 | * An actor, e.g. in tv, radio, movie, video games etc., or in an event. |
||
52 | * Actors can be associated with individual items or with a series, episode, clip. Supersedes actors. |
||
53 | * |
||
54 | * @param Person $actor |
||
55 | * @return EventTrait |
||
56 | */ |
||
57 | public function setActor($actor) |
||
62 | |||
63 | private $_attendee; |
||
64 | |||
65 | /** |
||
66 | * @return Organization|Person |
||
67 | */ |
||
68 | public function getAttendee() |
||
72 | |||
73 | /** |
||
74 | * A person or organization attending the event. Supersedes attendees. |
||
75 | * |
||
76 | * @param Organization|Person $attendee |
||
77 | * @return EventTrait |
||
78 | */ |
||
79 | View Code Duplication | public function setAttendee($attendee) |
|
86 | |||
87 | private $_composer; |
||
88 | |||
89 | /** |
||
90 | * @return Organization|Person |
||
91 | */ |
||
92 | public function getComposer() |
||
96 | |||
97 | /** |
||
98 | * The person or organization who wrote a composition, or who is the composer of a work performed at some event. |
||
99 | * |
||
100 | * @param Organization|Person $composer |
||
101 | * @return EventTrait |
||
102 | */ |
||
103 | View Code Duplication | public function setComposer($composer) |
|
110 | |||
111 | private $_contributor; |
||
112 | |||
113 | /** |
||
114 | * @return Organization|Person |
||
115 | */ |
||
116 | public function getContributor() |
||
120 | |||
121 | /** |
||
122 | * A secondary contributor to the CreativeWork or Event. |
||
123 | * |
||
124 | * @param Organization|Person $contributor |
||
125 | * @return EventTrait |
||
126 | */ |
||
127 | View Code Duplication | public function setContributor($contributor) |
|
134 | |||
135 | private $_director; |
||
136 | |||
137 | /** |
||
138 | * @return Person |
||
139 | */ |
||
140 | public function getDirector() |
||
144 | |||
145 | /** |
||
146 | * A director of e.g. tv, radio, movie, video gaming etc. content, or of an event. |
||
147 | * Directors can be associated with individual items or with a series, episode, clip. |
||
148 | * Supersedes directors. |
||
149 | * |
||
150 | * @param Person $director |
||
151 | * @return EventTrait |
||
152 | */ |
||
153 | public function setDirector($director) |
||
158 | |||
159 | private $_doorTime; |
||
160 | |||
161 | /** |
||
162 | * @return DateTime |
||
163 | */ |
||
164 | public function getDoorTime() |
||
168 | |||
169 | /** |
||
170 | * The time admission will commence. |
||
171 | * |
||
172 | * @param DateTime $doorTime |
||
173 | * @return EventTrait |
||
174 | */ |
||
175 | public function setDoorTime($doorTime) |
||
180 | |||
181 | private $_duration; |
||
182 | |||
183 | /** |
||
184 | * @return Duration |
||
185 | */ |
||
186 | public function getDuration() |
||
190 | |||
191 | /** |
||
192 | * The duration of the item (movie, audio recording, event, etc.) in ISO 8601 date format. |
||
193 | * |
||
194 | * @param Duration $duration |
||
195 | * @return EventTrait |
||
196 | */ |
||
197 | public function setDuration($duration) |
||
202 | |||
203 | private $_endDate; |
||
204 | |||
205 | /** |
||
206 | * @return Date|DateTime |
||
207 | */ |
||
208 | public function getEndDate() |
||
212 | |||
213 | /** |
||
214 | * The end date and time of the item (in ISO 8601 date format). |
||
215 | * |
||
216 | * @param Date|DateTime $endDate |
||
217 | * @return EventTrait |
||
218 | */ |
||
219 | public function setEndDate($endDate) |
||
224 | |||
225 | private $_funder; |
||
226 | |||
227 | /** |
||
228 | * @return Organization|Person |
||
229 | */ |
||
230 | public function getFunder() |
||
234 | |||
235 | /** |
||
236 | * A person or organization that supports (sponsors) something through some kind of financial contribution. |
||
237 | * |
||
238 | * @param Organization|Person $funder |
||
239 | * @return EventTrait |
||
240 | */ |
||
241 | View Code Duplication | public function setFunder($funder) |
|
248 | |||
249 | private $_inLanguage; |
||
250 | |||
251 | /** |
||
252 | * @return Language|Text |
||
253 | */ |
||
254 | public function getInLanguage() |
||
258 | |||
259 | /** |
||
260 | * The language of the content or performance or used in an action. |
||
261 | * Please use one of the language codes from the IETF BCP 47 standard. See also availableLanguage. |
||
262 | * Supersedes language. |
||
263 | * |
||
264 | * @param Language|Text $inLanguage |
||
265 | * @return EventTrait |
||
266 | */ |
||
267 | public function setInLanguage($inLanguage) |
||
272 | |||
273 | private $_isAccessibleForFree; |
||
274 | |||
275 | /** |
||
276 | * @return bool |
||
277 | */ |
||
278 | public function isAccessibleForFree() |
||
282 | |||
283 | /** |
||
284 | * A flag to signal that the item, event, or place is accessible for free. Supersedes free. |
||
285 | * |
||
286 | * @param bool $isAccessibleForFree |
||
287 | * @return EventTrait |
||
288 | */ |
||
289 | public function setIsAccessibleForFree($isAccessibleForFree) |
||
294 | |||
295 | private $_location; |
||
296 | |||
297 | /** |
||
298 | * @return Place|PostalAddress|string |
||
299 | */ |
||
300 | public function getLocation() |
||
304 | |||
305 | /** |
||
306 | * The location of for example where the event is happening, an organization is located, |
||
307 | * or where an action takes place. |
||
308 | * |
||
309 | * @param Place|PostalAddress|string $location |
||
310 | * @return EventTrait |
||
311 | */ |
||
312 | public function setLocation($location) |
||
317 | |||
318 | private $_maximumAttendeeCapacity; |
||
319 | |||
320 | /** |
||
321 | * @return int |
||
322 | */ |
||
323 | public function getMaximumAttendeeCapacity() |
||
327 | |||
328 | /** |
||
329 | * The total number of individuals that may attend an event or venue. |
||
330 | * |
||
331 | * @param int $maximumAttendeeCapacity |
||
332 | */ |
||
333 | public function setMaximumAttendeeCapacity($maximumAttendeeCapacity) |
||
337 | |||
338 | private $_organizer; |
||
339 | |||
340 | /** |
||
341 | * @return Organization|Person |
||
342 | */ |
||
343 | public function getOrganizer() |
||
347 | |||
348 | /** |
||
349 | * An organizer of an Event. |
||
350 | * |
||
351 | * @param Organization|Person $organizer |
||
352 | * @return EventTrait |
||
353 | */ |
||
354 | View Code Duplication | public function setOrganizer($organizer) |
|
361 | |||
362 | private $_performer; |
||
363 | |||
364 | /** |
||
365 | * @return Organization|Person |
||
366 | */ |
||
367 | public function getPerformer() |
||
371 | |||
372 | /** |
||
373 | * A performer at the event—for example, a presenter, musician, musical group or actor. |
||
374 | * Supersedes performers. |
||
375 | * |
||
376 | * @param Organization|Person $performer |
||
377 | * @return EventTrait |
||
378 | */ |
||
379 | View Code Duplication | public function setPerformer($performer) |
|
386 | |||
387 | private $_previousStartDate; |
||
388 | |||
389 | /** |
||
390 | * @return string |
||
391 | */ |
||
392 | public function getPreviousStartDate() |
||
396 | |||
397 | /** |
||
398 | * Used in conjunction with eventStatus for rescheduled or cancelled events. |
||
399 | * This property contains the previously scheduled start date. |
||
400 | * For rescheduled events, the startDate property should be used for the newly scheduled start date. |
||
401 | * In the (rare) case of an event that has been postponed and rescheduled multiple times, |
||
402 | * this field may be repeated. |
||
403 | * |
||
404 | * @param DateValue $previousStartDate |
||
405 | * @return EventTrait |
||
406 | */ |
||
407 | public function setPreviousStartDate(DateValue $previousStartDate) |
||
412 | |||
413 | private $_recordedIn; |
||
414 | |||
415 | /** |
||
416 | * @return CreativeWork |
||
417 | */ |
||
418 | public function getRecordedIn() |
||
422 | |||
423 | /** |
||
424 | * The CreativeWork that captured all or part of this Event. |
||
425 | * Inverse property: recordedAt |
||
426 | * |
||
427 | * @param CreativeWork $recordedIn |
||
428 | * @return EventTrait |
||
429 | */ |
||
430 | public function setRecordedIn(CreativeWork $recordedIn) |
||
435 | |||
436 | private $_remainingAttendeeCapacity; |
||
437 | |||
438 | /** |
||
439 | * @return int |
||
440 | */ |
||
441 | public function getRemainingAttendeeCapacity() |
||
445 | |||
446 | /** |
||
447 | * The number of attendee places for an event that remain unallocated. |
||
448 | * |
||
449 | * @param int $remainingAttendeeCapacity |
||
450 | * @return EventTrait |
||
451 | */ |
||
452 | public function setRemainingAttendeeCapacity($remainingAttendeeCapacity) |
||
457 | |||
458 | private $_sponsor; |
||
459 | |||
460 | /** |
||
461 | * @return Organization|Person |
||
462 | */ |
||
463 | public function getSponsor() |
||
467 | |||
468 | /** |
||
469 | * A person or organization that supports a thing through a pledge, promise, or financial contribution. |
||
470 | * e.g. a sponsor of a Medical Study or a corporate sponsor of an event. |
||
471 | * |
||
472 | * @param Organization|Person $sponsor |
||
473 | * @return EventTrait |
||
474 | */ |
||
475 | View Code Duplication | public function setSponsor($sponsor) |
|
482 | |||
483 | private $_startDate; |
||
484 | |||
485 | /** |
||
486 | * @return Date|DateTime |
||
487 | */ |
||
488 | public function getStartDate() |
||
492 | |||
493 | /** |
||
494 | * The start date and time of the item (in ISO 8601 date format). |
||
495 | * |
||
496 | * @param DateTimeValue $startDate |
||
497 | * @return EventTrait |
||
498 | */ |
||
499 | public function setStartDate(DateTimeValue $startDate) |
||
504 | |||
505 | private $_subEvent; |
||
506 | |||
507 | /** |
||
508 | * @return Event |
||
509 | */ |
||
510 | public function getSubEvent() |
||
514 | |||
515 | /** |
||
516 | * An Event that is part of this event. |
||
517 | * For example, a conference event includes many presentations, each of which is a subEvent of the conference. |
||
518 | * Supersedes subEvents. |
||
519 | * Inverse property: superEvent. |
||
520 | * |
||
521 | * @param Event $subEvent |
||
522 | */ |
||
523 | public function setSubEvent(Event $subEvent) |
||
527 | |||
528 | private $_superEvent; |
||
529 | |||
530 | /** |
||
531 | * @return Event |
||
532 | */ |
||
533 | public function getSuperEvent() |
||
537 | |||
538 | /** |
||
539 | * An event that this event is a part of. |
||
540 | * For example, a collection of individual music performances might each have a music festival as their superEvent. |
||
541 | * Inverse property: subEvent. |
||
542 | * |
||
543 | * @param Event $superEvent |
||
544 | * @return EventTrait |
||
545 | */ |
||
546 | public function setSuperEvent(Event $superEvent) |
||
551 | |||
552 | private $_translator; |
||
553 | |||
554 | /** |
||
555 | * @return Organization|Person |
||
556 | */ |
||
557 | public function getTranslator() |
||
561 | |||
562 | /** |
||
563 | * Organization or person who adapts a creative work to different languages, regional differences |
||
564 | * and technical requirements of a target market, or that translates during some event. |
||
565 | * |
||
566 | * @param Organization|Person $translator |
||
567 | * @return EventTrait |
||
568 | */ |
||
569 | View Code Duplication | public function setTranslator($translator) |
|
576 | |||
577 | private $_typicalAgeRange; |
||
578 | |||
579 | /** |
||
580 | * @return string |
||
581 | */ |
||
582 | public function getTypicalAgeRange() |
||
586 | |||
587 | /** |
||
588 | * The typical expected age range, e.g. '7-9', '11-'. |
||
589 | * |
||
590 | * @param string $typicalAgeRange |
||
591 | * @return EventTrait |
||
592 | */ |
||
593 | public function setTypicalAgeRange($typicalAgeRange) |
||
598 | |||
599 | private $_workFeatured; |
||
600 | |||
601 | /** |
||
602 | * @return CreativeWork |
||
603 | */ |
||
604 | public function getWorkFeatured() |
||
608 | |||
609 | /** |
||
610 | * A work featured in some event, e.g. exhibited in an ExhibitionEvent. |
||
611 | * Specific subproperties are available for workPerformed (e.g. a play), |
||
612 | * or a workPresented (a Movie at a ScreeningEvent). |
||
613 | * |
||
614 | * @param CreativeWork $workFeatured |
||
615 | * @return EventTrait |
||
616 | */ |
||
617 | public function setWorkFeatured(CreativeWork $workFeatured) |
||
622 | |||
623 | private $_workPerformed; |
||
624 | |||
625 | /** |
||
626 | * @return CreativeWork |
||
627 | */ |
||
628 | public function getWorkPerformed() |
||
632 | |||
633 | /** |
||
634 | * A work performed in some event, for example a play performed in a TheaterEvent. |
||
635 | * |
||
636 | * @param CreativeWork $workPerformed |
||
637 | * @return EventTrait |
||
638 | */ |
||
639 | public function setWorkPerformed(CreativeWork $workPerformed) |
||
644 | } |
||
645 |
In PHP traits cannot be used for type-hinting as they do not define a well-defined structure. This is because any class that uses a trait can rename that trait’s methods.
If you would like to return an object that has a guaranteed set of methods, you could create a companion interface that lists these methods explicitly.