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 |
||
13 | trait EventTrait |
||
14 | { |
||
15 | private $_about; |
||
16 | |||
17 | /** |
||
18 | * @return Thing |
||
19 | */ |
||
20 | public function getAbout() |
||
24 | |||
25 | /** |
||
26 | * The subject matter of the content. |
||
27 | * Inverse property: subjectOf. |
||
28 | * |
||
29 | * @param Thing $about |
||
30 | * @return EventTrait |
||
|
|||
31 | */ |
||
32 | public function setAbout($about) |
||
37 | |||
38 | private $_actor; |
||
39 | |||
40 | /** |
||
41 | * @return Person |
||
42 | */ |
||
43 | public function getActor() |
||
47 | |||
48 | /** |
||
49 | * An actor, e.g. in tv, radio, movie, video games etc., or in an event. |
||
50 | * Actors can be associated with individual items or with a series, episode, clip. Supersedes actors. |
||
51 | * |
||
52 | * @param Person $actor |
||
53 | * @return EventTrait |
||
54 | */ |
||
55 | public function setActor($actor) |
||
60 | |||
61 | private $_attendee; |
||
62 | |||
63 | /** |
||
64 | * @return Organization|Person |
||
65 | */ |
||
66 | public function getAttendee() |
||
70 | |||
71 | /** |
||
72 | * A person or organization attending the event. Supersedes attendees. |
||
73 | * |
||
74 | * @param Organization|Person $attendee |
||
75 | * @return EventTrait |
||
76 | */ |
||
77 | public function setAttendee($attendee) |
||
82 | |||
83 | private $_composer; |
||
84 | |||
85 | /** |
||
86 | * @return Organization|Person |
||
87 | */ |
||
88 | public function getComposer() |
||
92 | |||
93 | /** |
||
94 | * The person or organization who wrote a composition, or who is the composer of a work performed at some event. |
||
95 | * |
||
96 | * @param Organization|Person $composer |
||
97 | * @return EventTrait |
||
98 | */ |
||
99 | public function setComposer($composer) |
||
104 | |||
105 | private $_contributor; |
||
106 | |||
107 | /** |
||
108 | * @return Organization|Person |
||
109 | */ |
||
110 | public function getContributor() |
||
114 | |||
115 | /** |
||
116 | * A secondary contributor to the CreativeWork or Event. |
||
117 | * |
||
118 | * @param Organization|Person $contributor |
||
119 | * @return EventTrait |
||
120 | */ |
||
121 | public function setContributor($contributor) |
||
126 | |||
127 | private $_director; |
||
128 | |||
129 | /** |
||
130 | * @return Person |
||
131 | */ |
||
132 | public function getDirector() |
||
136 | |||
137 | /** |
||
138 | * A director of e.g. tv, radio, movie, video gaming etc. content, or of an event. |
||
139 | * Directors can be associated with individual items or with a series, episode, clip. |
||
140 | * Supersedes directors. |
||
141 | * |
||
142 | * @param Person $director |
||
143 | * @return EventTrait |
||
144 | */ |
||
145 | public function setDirector($director) |
||
150 | |||
151 | private $_doorTime; |
||
152 | |||
153 | /** |
||
154 | * @return DateTime |
||
155 | */ |
||
156 | public function getDoorTime() |
||
160 | |||
161 | /** |
||
162 | * The time admission will commence. |
||
163 | * |
||
164 | * @param DateTime $doorTime |
||
165 | * @return EventTrait |
||
166 | */ |
||
167 | public function setDoorTime($doorTime) |
||
172 | |||
173 | private $_duration; |
||
174 | |||
175 | /** |
||
176 | * @return Duration |
||
177 | */ |
||
178 | public function getDuration() |
||
182 | |||
183 | /** |
||
184 | * The duration of the item (movie, audio recording, event, etc.) in ISO 8601 date format. |
||
185 | * |
||
186 | * @param Duration $duration |
||
187 | * @return EventTrait |
||
188 | */ |
||
189 | public function setDuration($duration) |
||
194 | |||
195 | private $_endDate; |
||
196 | |||
197 | /** |
||
198 | * @return Date|DateTime |
||
199 | */ |
||
200 | public function getEndDate() |
||
204 | |||
205 | /** |
||
206 | * The end date and time of the item (in ISO 8601 date format). |
||
207 | * |
||
208 | * @param Date|DateTime $endDate |
||
209 | * @return EventTrait |
||
210 | */ |
||
211 | public function setEndDate($endDate) |
||
216 | |||
217 | private $_funder; |
||
218 | |||
219 | /** |
||
220 | * @return Organization|Person |
||
221 | */ |
||
222 | public function getFunder() |
||
226 | |||
227 | /** |
||
228 | * A person or organization that supports (sponsors) something through some kind of financial contribution. |
||
229 | * |
||
230 | * @param Organization|Person $funder |
||
231 | * @return EventTrait |
||
232 | */ |
||
233 | public function setFunder($funder) |
||
238 | |||
239 | private $_inLanguage; |
||
240 | |||
241 | /** |
||
242 | * @return Language|Text |
||
243 | */ |
||
244 | public function getInLanguage() |
||
248 | |||
249 | /** |
||
250 | * The language of the content or performance or used in an action. |
||
251 | * Please use one of the language codes from the IETF BCP 47 standard. See also availableLanguage. |
||
252 | * Supersedes language. |
||
253 | * |
||
254 | * @param Language|Text $inLanguage |
||
255 | * @return EventTrait |
||
256 | */ |
||
257 | public function setInLanguage($inLanguage) |
||
262 | |||
263 | private $_isAccessibleForFree; |
||
264 | |||
265 | /** |
||
266 | * @return bool |
||
267 | */ |
||
268 | public function isAccessibleForFree() |
||
272 | |||
273 | /** |
||
274 | * A flag to signal that the item, event, or place is accessible for free. Supersedes free. |
||
275 | * |
||
276 | * @param bool $isAccessibleForFree |
||
277 | * @return EventTrait |
||
278 | */ |
||
279 | public function setIsAccessibleForFree($isAccessibleForFree) |
||
284 | |||
285 | private $_location; |
||
286 | |||
287 | /** |
||
288 | * @return Place|PostalAddress|string |
||
289 | */ |
||
290 | public function getLocation() |
||
294 | |||
295 | /** |
||
296 | * The location of for example where the event is happening, an organization is located, |
||
297 | * or where an action takes place. |
||
298 | * |
||
299 | * @param Place|PostalAddress|string $location |
||
300 | * @return EventTrait |
||
301 | */ |
||
302 | public function setLocation($location) |
||
307 | |||
308 | private $_maximumAttendeeCapacity; |
||
309 | |||
310 | /** |
||
311 | * @return int |
||
312 | */ |
||
313 | public function getMaximumAttendeeCapacity() |
||
317 | |||
318 | /** |
||
319 | * The total number of individuals that may attend an event or venue. |
||
320 | * |
||
321 | * @param int $maximumAttendeeCapacity |
||
322 | */ |
||
323 | public function setMaximumAttendeeCapacity($maximumAttendeeCapacity) |
||
327 | |||
328 | private $_organizer; |
||
329 | |||
330 | /** |
||
331 | * @return Organization|Person |
||
332 | */ |
||
333 | public function getOrganizer() |
||
337 | |||
338 | /** |
||
339 | * An organizer of an Event. |
||
340 | * |
||
341 | * @param Organization|Person $organizer |
||
342 | * @return EventTrait |
||
343 | */ |
||
344 | public function setOrganizer($organizer) |
||
349 | |||
350 | private $_performer; |
||
351 | |||
352 | /** |
||
353 | * @return Organization|Person |
||
354 | */ |
||
355 | public function getPerformer() |
||
359 | |||
360 | /** |
||
361 | * A performer at the event—for example, a presenter, musician, musical group or actor. |
||
362 | * Supersedes performers. |
||
363 | * |
||
364 | * @param Organization|Person $performer |
||
365 | * @return EventTrait |
||
366 | */ |
||
367 | public function setPerformer($performer) |
||
372 | |||
373 | private $_previousStartDate; |
||
374 | |||
375 | /** |
||
376 | * @return string |
||
377 | */ |
||
378 | public function getPreviousStartDate() |
||
382 | |||
383 | /** |
||
384 | * Used in conjunction with eventStatus for rescheduled or cancelled events. |
||
385 | * This property contains the previously scheduled start date. |
||
386 | * For rescheduled events, the startDate property should be used for the newly scheduled start date. |
||
387 | * In the (rare) case of an event that has been postponed and rescheduled multiple times, |
||
388 | * this field may be repeated. |
||
389 | * |
||
390 | * @param DateValue $previousStartDate |
||
391 | * @return EventTrait |
||
392 | */ |
||
393 | public function setPreviousStartDate(DateValue $previousStartDate) |
||
398 | |||
399 | private $_recordedIn; |
||
400 | |||
401 | /** |
||
402 | * @return CreativeWork |
||
403 | */ |
||
404 | public function getRecordedIn() |
||
408 | |||
409 | /** |
||
410 | * The CreativeWork that captured all or part of this Event. |
||
411 | * Inverse property: recordedAt |
||
412 | * |
||
413 | * @param CreativeWork $recordedIn |
||
414 | * @return EventTrait |
||
415 | */ |
||
416 | public function setRecordedIn(CreativeWork $recordedIn) |
||
421 | |||
422 | private $_remainingAttendeeCapacity; |
||
423 | |||
424 | /** |
||
425 | * @return int |
||
426 | */ |
||
427 | public function getRemainingAttendeeCapacity() |
||
431 | |||
432 | /** |
||
433 | * The number of attendee places for an event that remain unallocated. |
||
434 | * |
||
435 | * @param int $remainingAttendeeCapacity |
||
436 | * @return EventTrait |
||
437 | */ |
||
438 | public function setRemainingAttendeeCapacity($remainingAttendeeCapacity) |
||
443 | |||
444 | private $_sponsor; |
||
445 | |||
446 | /** |
||
447 | * @return Organization|Person |
||
448 | */ |
||
449 | public function getSponsor() |
||
453 | |||
454 | /** |
||
455 | * A person or organization that supports a thing through a pledge, promise, or financial contribution. |
||
456 | * e.g. a sponsor of a Medical Study or a corporate sponsor of an event. |
||
457 | * |
||
458 | * @param Organization|Person $sponsor |
||
459 | * @return EventTrait |
||
460 | */ |
||
461 | public function setSponsor($sponsor) |
||
466 | |||
467 | private $_startDate; |
||
468 | |||
469 | /** |
||
470 | * @return Date|DateTime |
||
471 | */ |
||
472 | public function getStartDate() |
||
476 | |||
477 | /** |
||
478 | * The start date and time of the item (in ISO 8601 date format). |
||
479 | * |
||
480 | * @param DateTimeValue $startDate |
||
481 | * @return EventTrait |
||
482 | */ |
||
483 | public function setStartDate(DateTimeValue $startDate) |
||
488 | |||
489 | private $_subEvent; |
||
490 | |||
491 | /** |
||
492 | * @return Event |
||
493 | */ |
||
494 | public function getSubEvent() |
||
498 | |||
499 | /** |
||
500 | * An Event that is part of this event. |
||
501 | * For example, a conference event includes many presentations, each of which is a subEvent of the conference. |
||
502 | * Supersedes subEvents. |
||
503 | * Inverse property: superEvent. |
||
504 | * |
||
505 | * @param Event $subEvent |
||
506 | */ |
||
507 | public function setSubEvent(Event $subEvent) |
||
511 | |||
512 | private $_superEvent; |
||
513 | |||
514 | /** |
||
515 | * @return Event |
||
516 | */ |
||
517 | public function getSuperEvent() |
||
521 | |||
522 | /** |
||
523 | * An event that this event is a part of. |
||
524 | * For example, a collection of individual music performances might each have a music festival as their superEvent. |
||
525 | * Inverse property: subEvent. |
||
526 | * |
||
527 | * @param Event $superEvent |
||
528 | * @return EventTrait |
||
529 | */ |
||
530 | public function setSuperEvent(Event $superEvent) |
||
535 | |||
536 | private $_translator; |
||
537 | |||
538 | /** |
||
539 | * @return Organization|Person |
||
540 | */ |
||
541 | public function getTranslator() |
||
545 | |||
546 | /** |
||
547 | * Organization or person who adapts a creative work to different languages, regional differences |
||
548 | * and technical requirements of a target market, or that translates during some event. |
||
549 | * |
||
550 | * @param Organization|Person $translator |
||
551 | * @return EventTrait |
||
552 | */ |
||
553 | public function setTranslator($translator) |
||
558 | |||
559 | private $_typicalAgeRange; |
||
560 | |||
561 | /** |
||
562 | * @return string |
||
563 | */ |
||
564 | public function getTypicalAgeRange() |
||
568 | |||
569 | /** |
||
570 | * The typical expected age range, e.g. '7-9', '11-'. |
||
571 | * |
||
572 | * @param string $typicalAgeRange |
||
573 | * @return EventTrait |
||
574 | */ |
||
575 | public function setTypicalAgeRange($typicalAgeRange) |
||
580 | |||
581 | private $_workFeatured; |
||
582 | |||
583 | /** |
||
584 | * @return CreativeWork |
||
585 | */ |
||
586 | public function getWorkFeatured() |
||
590 | |||
591 | /** |
||
592 | * A work featured in some event, e.g. exhibited in an ExhibitionEvent. |
||
593 | * Specific subproperties are available for workPerformed (e.g. a play), |
||
594 | * or a workPresented (a Movie at a ScreeningEvent). |
||
595 | * |
||
596 | * @param CreativeWork $workFeatured |
||
597 | * @return EventTrait |
||
598 | */ |
||
599 | public function setWorkFeatured(CreativeWork $workFeatured) |
||
604 | |||
605 | private $_workPerformed; |
||
606 | |||
607 | /** |
||
608 | * @return CreativeWork |
||
609 | */ |
||
610 | public function getWorkPerformed() |
||
614 | |||
615 | /** |
||
616 | * A work performed in some event, for example a play performed in a TheaterEvent. |
||
617 | * |
||
618 | * @param CreativeWork $workPerformed |
||
619 | * @return EventTrait |
||
620 | */ |
||
621 | public function setWorkPerformed(CreativeWork $workPerformed) |
||
626 | } |
||
627 |
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.