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 |
||
34 | class EventCommandHandler extends Udb3CommandHandler implements LoggerAwareInterface |
||
35 | { |
||
36 | use LoggerAwareTrait; |
||
37 | |||
38 | /** |
||
39 | * @var RepositoryInterface |
||
40 | */ |
||
41 | protected $eventRepository; |
||
42 | |||
43 | /** |
||
44 | * @var SearchServiceInterface |
||
45 | */ |
||
46 | protected $searchService; |
||
47 | |||
48 | public function __construct( |
||
55 | |||
56 | public function handleLabelEvents(LabelEvents $labelEvents) |
||
62 | |||
63 | public function handleLabelQuery(LabelQuery $labelQuery) |
||
64 | { |
||
65 | $query = $labelQuery->getQuery(); |
||
66 | |||
67 | // do a pre query to test if the query is valid and check the item count |
||
68 | $preQueryResult = $this->searchService->search($query, 1, 0); |
||
69 | $totalItemCount = $preQueryResult->getTotalItems()->toNative(); |
||
70 | |||
71 | if (0 === $totalItemCount) { |
||
72 | return; |
||
73 | } |
||
74 | |||
75 | // change this pageSize value to increase or decrease the page size; |
||
76 | $pageSize = 10; |
||
77 | $pageCount = ceil($totalItemCount / $pageSize); |
||
78 | $pageCounter = 0; |
||
79 | $labelledEventIds = []; |
||
80 | |||
81 | //Page querying the search service; |
||
82 | while ($pageCounter < $pageCount) { |
||
83 | $start = $pageCounter * $pageSize; |
||
84 | // Sort ascending by creation date to make sure we get a quite consistent paging. |
||
85 | $sort = 'creationdate asc'; |
||
86 | $results = $this->searchService->search( |
||
87 | $query, |
||
88 | $pageSize, |
||
89 | $start, |
||
90 | $sort |
||
91 | ); |
||
92 | |||
93 | // Iterate the results of the current page and get their IDs |
||
94 | // by stripping them from the json-LD representation |
||
95 | foreach ($results->getItems() as $event) { |
||
96 | $expoId = explode('/', $event['@id']); |
||
97 | $eventId = array_pop($expoId); |
||
98 | |||
99 | if (!array_key_exists($eventId, $labelledEventIds)) { |
||
100 | $labelledEventIds[$eventId] = $pageCounter; |
||
101 | |||
102 | $this->labelEvent($labelQuery->getLabel(), $eventId); |
||
103 | } else { |
||
104 | if ($this->logger) { |
||
105 | $this->logger->error( |
||
106 | 'query_duplicate_event', |
||
107 | array( |
||
108 | 'query' => $query, |
||
109 | 'error' => "found duplicate event {$eventId} on page {$pageCounter}, occurred first time on page {$labelledEventIds[$eventId]}" |
||
110 | ) |
||
111 | ); |
||
112 | } |
||
113 | } |
||
114 | } |
||
115 | ++$pageCounter; |
||
116 | }; |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * Labels a single event with a keyword. |
||
121 | * |
||
122 | * @param Label $label |
||
123 | * @param $eventId |
||
124 | */ |
||
125 | private function labelEvent(Label $label, $eventId) |
||
126 | { |
||
127 | /** @var Event $event */ |
||
128 | $event = $this->eventRepository->load($eventId); |
||
129 | $event->label($label); |
||
130 | try { |
||
131 | $this->eventRepository->save($event); |
||
132 | |||
133 | if ($this->logger) { |
||
134 | $this->logger->info( |
||
135 | 'event_was_labelled', |
||
136 | array( |
||
137 | 'event_id' => $eventId, |
||
138 | ) |
||
139 | ); |
||
140 | } |
||
141 | } catch (\Exception $e) { |
||
142 | if ($this->logger) { |
||
143 | $this->logger->error( |
||
144 | 'event_was_not_labelled', |
||
145 | array( |
||
146 | 'event_id' => $eventId, |
||
147 | 'error' => $e->getMessage(), |
||
148 | 'exception_class' => get_class($e), |
||
149 | ) |
||
150 | ); |
||
151 | } |
||
152 | } |
||
153 | } |
||
154 | |||
155 | public function handleTranslateTitle(TranslateTitle $translateTitle) |
||
167 | |||
168 | public function handleTranslateDescription( |
||
182 | |||
183 | /** |
||
184 | * Handle an update command to update the main description. |
||
185 | */ |
||
186 | public function handleUpdateDescription(UpdateDescription $updateDescription) |
||
199 | |||
200 | /** |
||
201 | * Handle an update command to update the typical age range. |
||
202 | */ |
||
203 | public function handleUpdateTypicalAgeRange(UpdateTypicalAgeRange $updateTypicalAgeRange) |
||
216 | |||
217 | /** |
||
218 | * Handle the deletion of typical age range on an event. |
||
219 | */ |
||
220 | public function handleDeleteTypicalAgeRange(DeleteTypicalAgeRange $deleteTypicalAgeRange) |
||
231 | |||
232 | /** |
||
233 | * Handle an update command to update organizer. |
||
234 | */ |
||
235 | public function handleUpdateOrganizer(UpdateOrganizer $updateOrganizer) |
||
248 | |||
249 | /** |
||
250 | * Handle an update command to delete the organizer. |
||
251 | */ |
||
252 | public function handleDeleteOrganizer(DeleteOrganizer $deleteOrganizer) |
||
265 | |||
266 | /** |
||
267 | * Handle an update command to updated the contact point. |
||
268 | */ |
||
269 | public function handleUpdateContactPoint(UpdateContactPoint $updateContactPoint) |
||
282 | |||
283 | /** |
||
284 | * Handle an update command to updated the booking info. |
||
285 | */ |
||
286 | public function handleUpdateBookingInfo(UpdateBookingInfo $updateBookingInfo) |
||
299 | |||
300 | /** |
||
301 | * Handle an add image command. |
||
302 | * @param AddImage $addImage |
||
303 | */ |
||
304 | public function handleAddImage(AddImage $addImage) |
||
317 | |||
318 | /** |
||
319 | * Handle an update image command. |
||
320 | * @param UpdateImage $updateImage |
||
321 | */ |
||
322 | public function handleUpdateImage(UpdateImage $updateImage) |
||
331 | |||
332 | /** |
||
333 | * Handle a remove image command. |
||
334 | * @param RemoveImage $removeImage |
||
335 | */ |
||
336 | public function handleRemoveImage(RemoveImage $removeImage) |
||
345 | |||
346 | /** |
||
347 | * Handle an update the major info command. |
||
348 | */ |
||
349 | View Code Duplication | public function handleUpdateMajorInfo(UpdateMajorInfo $updateMajorInfo) |
|
366 | |||
367 | /** |
||
368 | * Handle a delete event command. |
||
369 | */ |
||
370 | public function handleDeleteEvent(DeleteEvent $deleteEvent) |
||
380 | |||
381 | public function handleApplyLabel(ApplyLabel $label) |
||
389 | |||
390 | public function handleUnlabel(Unlabel $label) |
||
398 | } |
||
399 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.