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:
1 | <?php |
||
18 | View Code Duplication | class PlaceCreated extends PlaceEvent |
|
|
|||
19 | { |
||
20 | /** |
||
21 | * @var Language |
||
22 | */ |
||
23 | private $mainLanguage; |
||
24 | |||
25 | /** |
||
26 | * @var Title |
||
27 | */ |
||
28 | private $title; |
||
29 | |||
30 | /** |
||
31 | * @var EventType |
||
32 | */ |
||
33 | private $eventType; |
||
34 | |||
35 | /** |
||
36 | * @var Theme |
||
37 | */ |
||
38 | private $theme = null; |
||
39 | |||
40 | /** |
||
41 | * @var Address |
||
42 | */ |
||
43 | private $address; |
||
44 | |||
45 | /** |
||
46 | * @var CalendarInterface |
||
47 | */ |
||
48 | private $calendar; |
||
49 | |||
50 | /** |
||
51 | * @var DateTimeImmutable|null |
||
52 | */ |
||
53 | private $publicationDate = null; |
||
54 | |||
55 | /** |
||
56 | * @param string $placeId |
||
57 | * @param Language $mainLanguage |
||
58 | * @param Title $title |
||
59 | * @param EventType $eventType |
||
60 | * @param Address $address |
||
61 | * @param CalendarInterface $calendar |
||
62 | * @param Theme|null $theme |
||
63 | * @param DateTimeImmutable|null $publicationDate |
||
64 | */ |
||
65 | public function __construct( |
||
66 | $placeId, |
||
67 | Language $mainLanguage, |
||
68 | Title $title, |
||
69 | EventType $eventType, |
||
70 | Address $address, |
||
71 | CalendarInterface $calendar, |
||
72 | Theme $theme = null, |
||
73 | DateTimeImmutable $publicationDate = null |
||
74 | ) { |
||
75 | parent::__construct($placeId); |
||
76 | |||
77 | $this->mainLanguage = $mainLanguage; |
||
78 | $this->title = $title; |
||
79 | $this->eventType = $eventType; |
||
80 | $this->address = $address; |
||
81 | $this->calendar = $calendar; |
||
82 | $this->theme = $theme; |
||
83 | $this->publicationDate = $publicationDate; |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * @return Language |
||
88 | */ |
||
89 | public function getMainLanguage() |
||
90 | { |
||
91 | return $this->mainLanguage; |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * @return Title |
||
96 | */ |
||
97 | public function getTitle() |
||
98 | { |
||
99 | return $this->title; |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * @return EventType |
||
104 | */ |
||
105 | public function getEventType() |
||
106 | { |
||
107 | return $this->eventType; |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * @return Theme |
||
112 | */ |
||
113 | public function getTheme() |
||
114 | { |
||
115 | return $this->theme; |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * @return CalendarInterface |
||
120 | */ |
||
121 | public function getCalendar() |
||
122 | { |
||
123 | return $this->calendar; |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * @return Address |
||
128 | */ |
||
129 | public function getAddress() |
||
130 | { |
||
131 | return $this->address; |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * @return DateTimeImmutable|null |
||
136 | */ |
||
137 | public function getPublicationDate() |
||
138 | { |
||
139 | return $this->publicationDate; |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * @return array |
||
144 | */ |
||
145 | public function serialize() |
||
165 | |||
166 | /** |
||
167 | * @return static |
||
168 | */ |
||
169 | public static function deserialize(array $data) |
||
193 | } |
||
194 |
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.