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 |
||
36 | class EventCommandHandler extends OfferCommandHandler implements LoggerAwareInterface |
||
37 | { |
||
38 | use LoggerAwareTrait; |
||
39 | |||
40 | /** |
||
41 | * Handle an update the major info command. |
||
42 | * @param UpdateMajorInfo $updateMajorInfo |
||
43 | */ |
||
44 | View Code Duplication | public function handleUpdateMajorInfo(UpdateMajorInfo $updateMajorInfo) |
|
|
|||
45 | { |
||
46 | /** @var Event $event */ |
||
47 | $event = $this->offerRepository->load($updateMajorInfo->getItemId()); |
||
48 | |||
49 | $event->updateMajorInfo( |
||
50 | $updateMajorInfo->getTitle(), |
||
51 | $updateMajorInfo->getEventType(), |
||
52 | $updateMajorInfo->getLocation(), |
||
53 | $updateMajorInfo->getCalendar(), |
||
54 | $updateMajorInfo->getTheme() |
||
55 | ); |
||
56 | |||
57 | $this->offerRepository->save($event); |
||
58 | |||
59 | } |
||
60 | |||
61 | /** |
||
62 | * @param UpdateAudience $updateAudience |
||
63 | */ |
||
64 | public function handleUpdateAudience(UpdateAudience $updateAudience) |
||
65 | { |
||
66 | /** @var Event $event */ |
||
67 | $event = $this->offerRepository->load($updateAudience->getItemId()); |
||
68 | |||
69 | $event->updateAudience($updateAudience->getAudience()); |
||
70 | |||
71 | $this->offerRepository->save($event); |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * @return string |
||
76 | */ |
||
77 | protected function getAddLabelClassName() |
||
78 | { |
||
79 | return AddLabel::class; |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * @return string |
||
84 | */ |
||
85 | protected function getRemoveLabelClassName() |
||
86 | { |
||
87 | return RemoveLabel::class; |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * @return string |
||
92 | */ |
||
93 | protected function getAddImageClassName() |
||
94 | { |
||
95 | return AddImage::class; |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * @return string |
||
100 | */ |
||
101 | protected function getUpdateImageClassName() |
||
102 | { |
||
103 | return UpdateImage::class; |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * @return string |
||
108 | */ |
||
109 | protected function getRemoveImageClassName() |
||
110 | { |
||
111 | return RemoveImage::class; |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * @return string |
||
116 | */ |
||
117 | protected function getSelectMainImageClassName() |
||
118 | { |
||
119 | return SelectMainImage::class; |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * @return string |
||
124 | */ |
||
125 | protected function getTranslateTitleClassName() |
||
126 | { |
||
127 | return TranslateTitle::class; |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * @return string |
||
132 | */ |
||
133 | protected function getUpdateDescriptionClassName() |
||
137 | |||
138 | /** |
||
139 | * @return string |
||
140 | */ |
||
141 | protected function getUpdateTypicalAgeRangeClassName() |
||
145 | |||
146 | /** |
||
147 | * @return string |
||
148 | */ |
||
149 | protected function getDeleteTypicalAgeRangeClassName() |
||
153 | |||
154 | /** |
||
155 | * @return string |
||
156 | */ |
||
157 | protected function getUpdateOrganizerClassName() |
||
161 | |||
162 | /** |
||
163 | * @return string |
||
164 | */ |
||
165 | protected function getDeleteOrganizerClassName() |
||
169 | |||
170 | /** |
||
171 | * @return string |
||
172 | */ |
||
173 | protected function getUpdateContactPointClassName() |
||
177 | |||
178 | /** |
||
179 | * @return string |
||
180 | */ |
||
181 | protected function getUpdateBookingInfoClassName() |
||
185 | |||
186 | /** |
||
187 | * @return string |
||
188 | */ |
||
189 | protected function getUpdatePriceInfoClassName() |
||
193 | |||
194 | /** |
||
195 | * @return string |
||
196 | */ |
||
197 | protected function getDeleteOfferClassName() |
||
201 | |||
202 | protected function getPublishClassName() |
||
203 | { |
||
204 | return Publish::class; |
||
205 | } |
||
206 | |||
207 | protected function getApproveClassName() |
||
208 | { |
||
209 | return Approve::class; |
||
210 | } |
||
211 | |||
212 | protected function getRejectClassName() |
||
216 | |||
217 | protected function getFlagAsDuplicateClassName() |
||
221 | |||
222 | protected function getFlagAsInappropriateClassName() |
||
226 | } |
||
227 |
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.