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 |
||
35 | class EventCommandHandler extends OfferCommandHandler implements LoggerAwareInterface |
||
36 | { |
||
37 | use LoggerAwareTrait; |
||
38 | |||
39 | /** |
||
40 | * @var SearchServiceInterface |
||
41 | */ |
||
42 | protected $searchService; |
||
43 | |||
44 | public function __construct( |
||
51 | |||
52 | /** |
||
53 | * Handle an update command to update the main description. |
||
54 | */ |
||
55 | public function handleUpdateDescription(UpdateDescription $updateDescription) |
||
56 | { |
||
57 | |||
58 | /** @var Event $event */ |
||
59 | $event = $this->repository->load($updateDescription->getId()); |
||
60 | |||
61 | $event->updateDescription( |
||
62 | $updateDescription->getDescription() |
||
63 | ); |
||
64 | |||
65 | $this->repository->save($event); |
||
66 | |||
67 | } |
||
68 | |||
69 | /** |
||
70 | * Handle an update command to update the typical age range. |
||
71 | */ |
||
72 | public function handleUpdateTypicalAgeRange(UpdateTypicalAgeRange $updateTypicalAgeRange) |
||
73 | { |
||
74 | |||
75 | /** @var Event $event */ |
||
76 | $event = $this->repository->load($updateTypicalAgeRange->getId()); |
||
77 | |||
78 | $event->updateTypicalAgeRange( |
||
79 | $updateTypicalAgeRange->getTypicalAgeRange() |
||
80 | ); |
||
81 | |||
82 | $this->repository->save($event); |
||
83 | |||
84 | } |
||
85 | |||
86 | /** |
||
87 | * Handle the deletion of typical age range on an event. |
||
88 | */ |
||
89 | public function handleDeleteTypicalAgeRange(DeleteTypicalAgeRange $deleteTypicalAgeRange) |
||
90 | { |
||
91 | |||
92 | /** @var Event $event */ |
||
93 | $event = $this->repository->load($deleteTypicalAgeRange->getId()); |
||
94 | |||
95 | $event->deleteTypicalAgeRange(); |
||
96 | |||
97 | $this->repository->save($event); |
||
98 | |||
99 | } |
||
100 | |||
101 | /** |
||
102 | * Handle an update command to update organizer. |
||
103 | */ |
||
104 | public function handleUpdateOrganizer(UpdateOrganizer $updateOrganizer) |
||
105 | { |
||
106 | |||
107 | /** @var Event $event */ |
||
108 | $event = $this->repository->load($updateOrganizer->getId()); |
||
109 | |||
110 | $event->updateOrganizer( |
||
111 | $updateOrganizer->getOrganizerId() |
||
112 | ); |
||
113 | |||
114 | $this->repository->save($event); |
||
115 | |||
116 | } |
||
117 | |||
118 | /** |
||
119 | * Handle an update command to delete the organizer. |
||
120 | */ |
||
121 | public function handleDeleteOrganizer(DeleteOrganizer $deleteOrganizer) |
||
122 | { |
||
123 | |||
124 | /** @var Event $event */ |
||
125 | $event = $this->repository->load($deleteOrganizer->getId()); |
||
126 | |||
127 | $event->deleteOrganizer( |
||
128 | $deleteOrganizer->getOrganizerId() |
||
129 | ); |
||
130 | |||
131 | $this->repository->save($event); |
||
132 | |||
133 | } |
||
134 | |||
135 | /** |
||
136 | * Handle an update command to updated the contact point. |
||
137 | */ |
||
138 | public function handleUpdateContactPoint(UpdateContactPoint $updateContactPoint) |
||
139 | { |
||
140 | |||
141 | /** @var Event $event */ |
||
142 | $event = $this->repository->load($updateContactPoint->getId()); |
||
143 | |||
144 | $event->updateContactPoint( |
||
145 | $updateContactPoint->getContactPoint() |
||
146 | ); |
||
147 | |||
148 | $this->repository->save($event); |
||
149 | |||
150 | } |
||
151 | |||
152 | /** |
||
153 | * Handle an update command to updated the booking info. |
||
154 | */ |
||
155 | public function handleUpdateBookingInfo(UpdateBookingInfo $updateBookingInfo) |
||
156 | { |
||
157 | |||
158 | /** @var Event $event */ |
||
159 | $event = $this->repository->load($updateBookingInfo->getId()); |
||
160 | |||
161 | $event->updateBookingInfo( |
||
162 | $updateBookingInfo->getBookingInfo() |
||
163 | ); |
||
164 | |||
165 | $this->repository->save($event); |
||
166 | |||
167 | } |
||
168 | |||
169 | /** |
||
170 | * Handle an update the major info command. |
||
171 | */ |
||
172 | View Code Duplication | public function handleUpdateMajorInfo(UpdateMajorInfo $updateMajorInfo) |
|
|
|||
173 | { |
||
174 | |||
175 | /** @var Event $event */ |
||
176 | $event = $this->repository->load($updateMajorInfo->getId()); |
||
177 | |||
178 | $event->updateMajorInfo( |
||
179 | $updateMajorInfo->getTitle(), |
||
180 | $updateMajorInfo->getEventType(), |
||
181 | $updateMajorInfo->getLocation(), |
||
182 | $updateMajorInfo->getCalendar(), |
||
183 | $updateMajorInfo->getTheme() |
||
184 | ); |
||
185 | |||
186 | $this->repository->save($event); |
||
187 | |||
188 | } |
||
189 | |||
190 | /** |
||
191 | * Handle a delete event command. |
||
192 | */ |
||
193 | public function handleDeleteEvent(DeleteEvent $deleteEvent) |
||
194 | { |
||
195 | |||
196 | /** @var Event $event */ |
||
197 | $event = $this->repository->load($deleteEvent->getId()); |
||
198 | $event->deleteEvent(); |
||
199 | |||
200 | $this->repository->save($event); |
||
201 | |||
202 | } |
||
203 | |||
204 | /** |
||
205 | * @return string |
||
206 | */ |
||
207 | protected function getAddLabelClassName() |
||
211 | |||
212 | /** |
||
213 | * @return string |
||
214 | */ |
||
215 | protected function getDeleteLabelClassName() |
||
219 | |||
220 | /** |
||
221 | * @return string |
||
222 | */ |
||
223 | protected function getAddImageClassName() |
||
227 | |||
228 | protected function getUpdateImageClassName() |
||
232 | |||
233 | protected function getRemoveImageClassName() |
||
237 | |||
238 | /** |
||
239 | * @return string |
||
240 | */ |
||
241 | protected function getTranslateTitleClassName() |
||
245 | |||
246 | /** |
||
247 | * @return string |
||
248 | */ |
||
249 | protected function getTranslateDescriptionClassName() |
||
253 | } |
||
254 |
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.