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 PersonTrait 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 PersonTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | trait PersonTrait |
||
16 | { |
||
17 | private $_additionalName; |
||
18 | |||
19 | /** |
||
20 | * @return string |
||
21 | */ |
||
22 | public function getAdditionalName() |
||
26 | |||
27 | /** |
||
28 | * An additional name for a Person, can be used for a middle name. |
||
29 | * |
||
30 | * @param string $additionalName |
||
31 | * @return PersonTrait |
||
|
|||
32 | */ |
||
33 | public function setAdditionalName($additionalName) |
||
38 | |||
39 | private $_address; |
||
40 | |||
41 | /** |
||
42 | * @return PostalAddress|string |
||
43 | */ |
||
44 | public function getAddress() |
||
48 | |||
49 | /** |
||
50 | * Physical address of the item. |
||
51 | * |
||
52 | * @param PostalAddress|string $address |
||
53 | * @return PersonTrait |
||
54 | */ |
||
55 | public function setAddress(PostalAddress $address) |
||
60 | |||
61 | private $_affiliation; |
||
62 | |||
63 | /** |
||
64 | * @return Organization |
||
65 | */ |
||
66 | public function getAffiliation() |
||
70 | |||
71 | /** |
||
72 | * An organization that this person is affiliated with. For example, a school/university, a club, or a team. |
||
73 | * |
||
74 | * @param Organization $affiliation |
||
75 | * @return PersonTrait |
||
76 | */ |
||
77 | public function setAffiliation(Organization $affiliation) |
||
82 | |||
83 | private $_award; |
||
84 | |||
85 | /** |
||
86 | * @return string |
||
87 | */ |
||
88 | public function getAward() |
||
92 | |||
93 | /** |
||
94 | * An award won by or for this item. |
||
95 | * Supersedes awards. |
||
96 | * |
||
97 | * @param string $award |
||
98 | * @return PersonTrait |
||
99 | */ |
||
100 | public function setAward($award) |
||
105 | |||
106 | private $_birthDate; |
||
107 | |||
108 | /** |
||
109 | * @return string |
||
110 | */ |
||
111 | public function getBirthDate() |
||
115 | |||
116 | /** |
||
117 | * Date of birth. |
||
118 | * |
||
119 | * @param DateValue $birthDate |
||
120 | * @return PersonTrait |
||
121 | */ |
||
122 | public function setBirthDate(DateValue $birthDate) |
||
127 | |||
128 | private $_birthPlace; |
||
129 | |||
130 | /** |
||
131 | * @return Place |
||
132 | */ |
||
133 | public function getBirthPlace() |
||
137 | |||
138 | /** |
||
139 | * The place where the person was born. |
||
140 | * |
||
141 | * @param Place $birthPlace |
||
142 | * @return PersonTrait |
||
143 | */ |
||
144 | public function setBirthPlace($birthPlace) |
||
149 | |||
150 | private $_brand; |
||
151 | |||
152 | /** |
||
153 | * @return Brand|Organization |
||
154 | */ |
||
155 | public function getBrand() |
||
159 | |||
160 | /** |
||
161 | * The brand(s) associated with a product or service, or the brand(s) maintained by an organization or business person. |
||
162 | * |
||
163 | * @param Brand|Organization $brand |
||
164 | * @return PersonTrait |
||
165 | */ |
||
166 | public function setBrand($brand) |
||
171 | |||
172 | private $_children; |
||
173 | |||
174 | /** |
||
175 | * @return Person |
||
176 | */ |
||
177 | public function getChildren() |
||
181 | |||
182 | /** |
||
183 | * A child of the person. |
||
184 | * |
||
185 | * @param Person $children |
||
186 | * @return PersonTrait |
||
187 | */ |
||
188 | public function setChildren(Person $children) |
||
193 | |||
194 | private $_colleague; |
||
195 | |||
196 | /** |
||
197 | * @return Person |
||
198 | */ |
||
199 | public function getColleague() |
||
203 | |||
204 | /** |
||
205 | * A colleague of the person. |
||
206 | * |
||
207 | * Supersedes colleagues. |
||
208 | * |
||
209 | * @param Person $colleague |
||
210 | * @return PersonTrait |
||
211 | */ |
||
212 | public function setColleague(Person $colleague) |
||
217 | |||
218 | private $_contactPoint; |
||
219 | |||
220 | /** |
||
221 | * @return ContactPoint |
||
222 | */ |
||
223 | public function getContactPoint() |
||
227 | |||
228 | /** |
||
229 | * A contact point for a person or organization. |
||
230 | * |
||
231 | * Supersedes contactPoints. |
||
232 | * |
||
233 | * @param ContactPoint $contactPoint |
||
234 | * @return PersonTrait |
||
235 | */ |
||
236 | public function setContactPoint(ContactPoint $contactPoint) |
||
241 | |||
242 | private $_deathDate; |
||
243 | |||
244 | /** |
||
245 | * @return string |
||
246 | */ |
||
247 | public function getDeathDate() |
||
251 | |||
252 | /** |
||
253 | * Date of death. |
||
254 | * |
||
255 | * @param DateValue $deathDate |
||
256 | * @return PersonTrait |
||
257 | */ |
||
258 | public function setDeathDate(DateValue $deathDate) |
||
263 | |||
264 | private $_deathPlace; |
||
265 | |||
266 | /** |
||
267 | * @return Place |
||
268 | */ |
||
269 | public function getDeathPlace() |
||
273 | |||
274 | /** |
||
275 | * The place where the person died. |
||
276 | * |
||
277 | * @param Place $deathPlace |
||
278 | * @return PersonTrait |
||
279 | */ |
||
280 | public function setDeathPlace(Place $deathPlace) |
||
285 | |||
286 | private $_duns; |
||
287 | |||
288 | /** |
||
289 | * @return string |
||
290 | */ |
||
291 | public function getDuns() |
||
295 | |||
296 | /** |
||
297 | * The Dun & Bradstreet DUNS number for identifying an organization or business person. |
||
298 | * |
||
299 | * @param string $duns |
||
300 | * @return PersonTrait |
||
301 | */ |
||
302 | public function setDuns(Person $duns) |
||
307 | |||
308 | private $_email; |
||
309 | |||
310 | /** |
||
311 | * @return string |
||
312 | */ |
||
313 | public function getEmail() |
||
317 | |||
318 | /** |
||
319 | * Email address. |
||
320 | * |
||
321 | * @param string $email |
||
322 | * @return PersonTrait |
||
323 | */ |
||
324 | public function setEmail($email) |
||
329 | |||
330 | private $_familyName; |
||
331 | |||
332 | /** |
||
333 | * @return string |
||
334 | */ |
||
335 | public function getFamilyName() |
||
339 | |||
340 | /** |
||
341 | * Family name. In the U.S., the last name of an Person. |
||
342 | * This can be used along with givenName instead of the name property. |
||
343 | * |
||
344 | * @param string $familyName |
||
345 | * @return PersonTrait |
||
346 | */ |
||
347 | public function setFamilyName($familyName) |
||
352 | |||
353 | private $_faxNumber; |
||
354 | |||
355 | /** |
||
356 | * @return string |
||
357 | */ |
||
358 | public function getFaxNumber() |
||
362 | |||
363 | /** |
||
364 | * The fax number. |
||
365 | * |
||
366 | * @param string $faxNumber |
||
367 | * @return PersonTrait |
||
368 | */ |
||
369 | public function setFaxNumber($faxNumber) |
||
374 | |||
375 | private $_follows; |
||
376 | |||
377 | /** |
||
378 | * @return Person |
||
379 | */ |
||
380 | public function getFollows() |
||
384 | |||
385 | /** |
||
386 | * The most generic uni-directional social relation. |
||
387 | * |
||
388 | * @param Person $follows |
||
389 | * @return PersonTrait |
||
390 | */ |
||
391 | public function setFollows(Person $follows) |
||
396 | |||
397 | private $_funder; |
||
398 | |||
399 | /** |
||
400 | * @return Organization|Person |
||
401 | */ |
||
402 | public function getFunder() |
||
406 | |||
407 | /** |
||
408 | * A person or organization that supports (sponsors) something through some kind of financial contribution. |
||
409 | * |
||
410 | * @param Organization|Person $funder |
||
411 | * @return PersonTrait |
||
412 | */ |
||
413 | View Code Duplication | public function setFunder($funder) |
|
420 | |||
421 | private $_gender; |
||
422 | |||
423 | /** |
||
424 | * @return GenderType|string |
||
425 | */ |
||
426 | public function getGender() |
||
430 | |||
431 | /** |
||
432 | * Gender of the person. While http://schema.org/Male and http://schema.org/Female may be used, |
||
433 | * text strings are also acceptable for people who do not identify as a binary gender. |
||
434 | * |
||
435 | * @param GenderType|string $gender |
||
436 | * @return PersonTrait |
||
437 | */ |
||
438 | public function setGender($gender) |
||
443 | |||
444 | private $_givenName; |
||
445 | |||
446 | /** |
||
447 | * @return string |
||
448 | */ |
||
449 | public function getGivenName() |
||
453 | |||
454 | /** |
||
455 | * Given name. In the U.S., the first name of a Person. |
||
456 | * This can be used along with familyName instead of the name property. |
||
457 | * |
||
458 | * @param string $givenName |
||
459 | * @return PersonTrait |
||
460 | */ |
||
461 | public function setGivenName($givenName) |
||
466 | |||
467 | private $_globalLocationNumber; |
||
468 | |||
469 | /** |
||
470 | * @return string |
||
471 | */ |
||
472 | public function getGlobalLocationNumber() |
||
476 | |||
477 | /** |
||
478 | * The Global Location Number (GLN, sometimes also referred to as International Location Number or ILN) |
||
479 | * of the respective organization, person, or place. |
||
480 | * The GLN is a 13-digit number used to identify parties and physical locations. |
||
481 | * |
||
482 | * @param string $globalLocationNumber |
||
483 | * @return PersonTrait |
||
484 | */ |
||
485 | public function setGlobalLocationNumber($globalLocationNumber) |
||
490 | |||
491 | private $_hasPOS; |
||
492 | |||
493 | /** |
||
494 | * @return Place |
||
495 | */ |
||
496 | public function getHasPOS() |
||
500 | |||
501 | /** |
||
502 | * Points-of-Sales operated by the organization or person. |
||
503 | * |
||
504 | * @param Place $hasPOS |
||
505 | * @return PersonTrait |
||
506 | */ |
||
507 | public function setHasPOS($hasPOS) |
||
512 | |||
513 | private $_height; |
||
514 | |||
515 | /** |
||
516 | * @return Distance|QuantitativeValue |
||
517 | */ |
||
518 | public function getHeight() |
||
522 | |||
523 | /** |
||
524 | * The height of the item. |
||
525 | * |
||
526 | * @param Distance|QuantitativeValue $height |
||
527 | * @return PersonTrait |
||
528 | */ |
||
529 | public function setHeight($height) |
||
534 | |||
535 | private $_homeLocation; |
||
536 | |||
537 | /** |
||
538 | * @return ContactPoint|Place |
||
539 | */ |
||
540 | public function getHomeLocation() |
||
544 | |||
545 | /** |
||
546 | * A contact location for a person's residence. |
||
547 | * |
||
548 | * @param ContactPoint|Place $homeLocation |
||
549 | * @return PersonTrait |
||
550 | */ |
||
551 | public function setHomeLocation($homeLocation) |
||
556 | |||
557 | private $_honorificPrefix; |
||
558 | |||
559 | /** |
||
560 | * @return string |
||
561 | */ |
||
562 | public function getHonorificPrefix() |
||
566 | |||
567 | /** |
||
568 | * An honorific prefix preceding a Person's name such as Dr/Mrs/Mr. |
||
569 | * |
||
570 | * @param string $honorificPrefix |
||
571 | * @return PersonTrait |
||
572 | */ |
||
573 | public function setHonorificPrefix($honorificPrefix) |
||
578 | |||
579 | private $_honorificSuffix; |
||
580 | |||
581 | /** |
||
582 | * @return string |
||
583 | */ |
||
584 | public function getHonorificSuffix() |
||
588 | |||
589 | /** |
||
590 | * An honorific suffix preceding a Person's name such as M.D. /PhD/MSCSW. |
||
591 | * |
||
592 | * @param string $honorificSuffix |
||
593 | * @return PersonTrait |
||
594 | */ |
||
595 | public function setHonorificSuffix($honorificSuffix) |
||
600 | |||
601 | private $_isicV4; |
||
602 | |||
603 | /** |
||
604 | * @return string |
||
605 | */ |
||
606 | public function getIsicV4() |
||
610 | |||
611 | /** |
||
612 | * The International Standard of Industrial Classification of All Economic Activities (ISIC), |
||
613 | * Revision 4 code for a particular organization, business person, or place. |
||
614 | * |
||
615 | * @param string $isicV4 |
||
616 | * @return PersonTrait |
||
617 | */ |
||
618 | public function setIsicV4($isicV4) |
||
623 | |||
624 | private $_jobTitle; |
||
625 | |||
626 | /** |
||
627 | * @return string |
||
628 | */ |
||
629 | public function getJobTitle() |
||
633 | |||
634 | /** |
||
635 | * The job title of the person (for example, Financial Manager). |
||
636 | * |
||
637 | * @param string $jobTitle |
||
638 | * @return PersonTrait |
||
639 | */ |
||
640 | public function setJobTitle($jobTitle) |
||
645 | |||
646 | private $_knows; |
||
647 | |||
648 | /** |
||
649 | * @return Person |
||
650 | */ |
||
651 | public function getKnows() |
||
655 | |||
656 | /** |
||
657 | * The most generic bi-directional social/work relation. |
||
658 | * |
||
659 | * @param Person $knows |
||
660 | * @return PersonTrait |
||
661 | */ |
||
662 | public function setKnows(Person $knows) |
||
667 | |||
668 | private $_makesOffer; |
||
669 | |||
670 | /** |
||
671 | * @return Offer |
||
672 | */ |
||
673 | public function getMakesOffer() |
||
677 | |||
678 | /** |
||
679 | * A pointer to products or services offered by the organization or person. |
||
680 | * Inverse property: offeredBy. |
||
681 | * |
||
682 | * @param Offer $makesOffer |
||
683 | * @return PersonTrait |
||
684 | */ |
||
685 | public function setMakesOffer(Offer $makesOffer) |
||
690 | |||
691 | private $_memberOf; |
||
692 | |||
693 | /** |
||
694 | * @return Organization|ProgramMembership |
||
695 | */ |
||
696 | public function getMemberOf() |
||
700 | |||
701 | /** |
||
702 | * An Organization (or ProgramMembership) to which this Person or Organization belongs. |
||
703 | * Inverse property: member. |
||
704 | * |
||
705 | * @param Organization|ProgramMembership $memberOf |
||
706 | * @return PersonTrait |
||
707 | */ |
||
708 | public function setMemberOf($memberOf) |
||
713 | |||
714 | private $_naics; |
||
715 | |||
716 | /** |
||
717 | * @return string |
||
718 | */ |
||
719 | public function getNaics() |
||
723 | |||
724 | /** |
||
725 | * The North American Industry Classification System (NAICS) code for a particular organization or business person. |
||
726 | * |
||
727 | * @param string $naics |
||
728 | * @return PersonTrait |
||
729 | */ |
||
730 | public function setNaics($naics) |
||
735 | |||
736 | private $_nationality; |
||
737 | |||
738 | /** |
||
739 | * @return Country |
||
740 | */ |
||
741 | public function getNationality() |
||
745 | |||
746 | /** |
||
747 | * Nationality of the person. |
||
748 | * |
||
749 | * @param Country $nationality |
||
750 | * @return PersonTrait |
||
751 | */ |
||
752 | public function setNationality(Country $nationality) |
||
757 | |||
758 | private $_parent; |
||
759 | |||
760 | /** |
||
761 | * @return Person |
||
762 | */ |
||
763 | public function getParent() |
||
767 | |||
768 | /** |
||
769 | * A parent of this person. Supersedes parents. |
||
770 | * |
||
771 | * @param Person $parent |
||
772 | * @return PersonTrait |
||
773 | */ |
||
774 | public function setParent(Person $parent) |
||
779 | |||
780 | private $_performerIn; |
||
781 | |||
782 | /** |
||
783 | * @return Event |
||
784 | */ |
||
785 | public function getPerformerIn() |
||
789 | |||
790 | /** |
||
791 | * Event that this person is a performer or participant in. |
||
792 | * |
||
793 | * @param Event $performerIn |
||
794 | * @return PersonTrait |
||
795 | */ |
||
796 | public function setPerformerIn(Event $performerIn) |
||
801 | |||
802 | private $_publishingPrinciples; |
||
803 | |||
804 | /** |
||
805 | * @return CreativeWork|URL |
||
806 | */ |
||
807 | public function getPublishingPrinciples() |
||
811 | |||
812 | /** |
||
813 | * The publishingPrinciples property indicates (typically via URL) a document describing the editorial principles |
||
814 | * of an Organization (or individual e.g. a Person writing a blog) that relate to their activities as a publisher, |
||
815 | * e.g. ethics or diversity policies. When applied to a CreativeWork (e.g. NewsArticle) the principles are those |
||
816 | * of the party primarily responsible for the creation of the CreativeWork. |
||
817 | * |
||
818 | * While such policies are most typically expressed in natural language, sometimes related information |
||
819 | * (e.g. indicating a funder) can be expressed using schema.org terminology. |
||
820 | * |
||
821 | * @param CreativeWork|URL $publishingPrinciples |
||
822 | * @return PersonTrait |
||
823 | */ |
||
824 | public function setPublishingPrinciples($publishingPrinciples) |
||
829 | |||
830 | private $_relatedTo; |
||
831 | |||
832 | /** |
||
833 | * @return Person |
||
834 | */ |
||
835 | public function getRelatedTo() |
||
839 | |||
840 | /** |
||
841 | * The most generic familial relation. |
||
842 | * |
||
843 | * @param Person $relatedTo |
||
844 | * @return PersonTrait |
||
845 | */ |
||
846 | public function setRelatedTo(Person $relatedTo) |
||
851 | |||
852 | private $_sibling; |
||
853 | |||
854 | /** |
||
855 | * @return Person |
||
856 | */ |
||
857 | public function getSibling() |
||
861 | |||
862 | /** |
||
863 | * A sibling of the person. Supersedes siblings. |
||
864 | * |
||
865 | * @param Person $sibling |
||
866 | * @return PersonTrait |
||
867 | */ |
||
868 | public function setSibling(Person $sibling) |
||
873 | |||
874 | private $_sponsor; |
||
875 | |||
876 | /** |
||
877 | * @return Organization|Person |
||
878 | */ |
||
879 | public function getSponsor() |
||
883 | |||
884 | /** |
||
885 | * A person or organization that supports a thing through a pledge, promise, or financial contribution. |
||
886 | * e.g. a sponsor of a Medical Study or a corporate sponsor of an event. |
||
887 | * |
||
888 | * @param Organization|Person $sponsor |
||
889 | * @return PersonTrait |
||
890 | */ |
||
891 | View Code Duplication | public function setSponsor($sponsor) |
|
898 | |||
899 | private $_spouse; |
||
900 | |||
901 | /** |
||
902 | * @return Person |
||
903 | */ |
||
904 | public function getSpouse() |
||
908 | |||
909 | /** |
||
910 | * The person's spouse. |
||
911 | * |
||
912 | * @param Person $spouse |
||
913 | * @return PersonTrait |
||
914 | */ |
||
915 | public function setSpouse(Person $spouse) |
||
920 | |||
921 | private $_taxID; |
||
922 | |||
923 | /** |
||
924 | * @return string |
||
925 | */ |
||
926 | public function getTaxID() |
||
930 | |||
931 | /** |
||
932 | * The Tax / Fiscal ID of the organization or person, e.g. the TIN in the US or the CIF/NIF in Spain. |
||
933 | * |
||
934 | * @param string $taxID |
||
935 | * @return PersonTrait |
||
936 | */ |
||
937 | public function setTaxID($taxID) |
||
942 | |||
943 | private $_telephone; |
||
944 | |||
945 | /** |
||
946 | * @return string |
||
947 | */ |
||
948 | public function getTelephone() |
||
952 | |||
953 | /** |
||
954 | * The telephone number. |
||
955 | * |
||
956 | * @param string $telephone |
||
957 | * @return PersonTrait |
||
958 | */ |
||
959 | public function setTelephone($telephone) |
||
964 | |||
965 | private $_vatID; |
||
966 | |||
967 | /** |
||
968 | * @return string |
||
969 | */ |
||
970 | public function getVatID() |
||
974 | |||
975 | /** |
||
976 | * The Value-added Tax ID of the organization or person. |
||
977 | * |
||
978 | * @param string $vatID |
||
979 | * @return PersonTrait |
||
980 | */ |
||
981 | public function setVatID($vatID) |
||
986 | |||
987 | private $_workLocation; |
||
988 | |||
989 | /** |
||
990 | * @return ContactPoint|Place |
||
991 | */ |
||
992 | public function getWorkLocation() |
||
996 | |||
997 | /** |
||
998 | * A contact location for a person's place of work. |
||
999 | * |
||
1000 | * @param ContactPoint|Place $workLocation |
||
1001 | * @return PersonTrait |
||
1002 | */ |
||
1003 | public function setWorkLocation($workLocation) |
||
1008 | |||
1009 | private $_worksFor; |
||
1010 | |||
1011 | /** |
||
1012 | * @return Organization |
||
1013 | */ |
||
1014 | public function getWorksFor() |
||
1018 | |||
1019 | /** |
||
1020 | * Organizations that the person works for. |
||
1021 | * |
||
1022 | * @param Organization $worksFor |
||
1023 | * @return PersonTrait |
||
1024 | */ |
||
1025 | public function setWorksFor(Organization $worksFor) |
||
1030 | } |
||
1031 |
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.