Conditions | 7 |
Paths | 4 |
Total Lines | 290 |
Code Lines | 153 |
Lines | 18 |
Ratio | 6.21 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
54 | public static function createSerializer(ReadRepositoryInterface $labelRepository) |
||
55 | { |
||
56 | $payloadManipulatingSerializer = new PayloadManipulatingSerializer( |
||
57 | new SimpleInterfaceSerializer() |
||
58 | ); |
||
59 | |||
60 | /* |
||
61 | * CREATE EVENTS |
||
62 | * |
||
63 | */ |
||
64 | |||
65 | $payloadManipulatingSerializer->manipulateEventsOfClass( |
||
66 | 'CultuurNet\UDB3\Event\Events\EventCreated', |
||
67 | function (array $serializedObject) { |
||
68 | return self::addDefaultMainLanguage($serializedObject); |
||
69 | } |
||
70 | ); |
||
71 | |||
72 | $payloadManipulatingSerializer->manipulateEventsOfClass( |
||
73 | 'CultuurNet\UDB3\Place\Events\PlaceCreated', |
||
74 | function (array $serializedObject) { |
||
75 | return self::addDefaultMainLanguage($serializedObject); |
||
76 | } |
||
77 | ); |
||
78 | |||
79 | $payloadManipulatingSerializer->manipulateEventsOfClass( |
||
80 | 'CultuurNet\UDB3\Organizer\Events\OrganizerCreatedWithUniqueWebsite', |
||
81 | function (array $serializedObject) { |
||
82 | return self::addDefaultMainLanguage($serializedObject); |
||
83 | } |
||
84 | ); |
||
85 | |||
86 | /* |
||
87 | * TRANSLATION EVENTS |
||
88 | */ |
||
89 | |||
90 | $payloadManipulatingSerializer->manipulateEventsOfClass( |
||
91 | 'CultuurNet\UDB3\Event\TitleTranslated', |
||
92 | function (array $serializedObject) { |
||
93 | $serializedObject['class'] = TitleTranslated::class; |
||
94 | |||
95 | $serializedObject = self::replaceEventIdWithItemId($serializedObject); |
||
96 | |||
97 | return $serializedObject; |
||
98 | } |
||
99 | ); |
||
100 | |||
101 | $payloadManipulatingSerializer->manipulateEventsOfClass( |
||
102 | 'CultuurNet\UDB3\Event\DescriptionTranslated', |
||
103 | function (array $serializedObject) { |
||
104 | $serializedObject['class'] = DescriptionTranslated::class; |
||
105 | |||
106 | $serializedObject = self::replaceEventIdWithItemId($serializedObject); |
||
107 | |||
108 | return $serializedObject; |
||
109 | } |
||
110 | ); |
||
111 | |||
112 | /* |
||
113 | * LABEL EVENTS |
||
114 | */ |
||
115 | |||
116 | $payloadManipulatingSerializer->manipulateEventsOfClass( |
||
117 | 'CultuurNet\UDB3\Label\Events\MadeInvisible', |
||
118 | function (array $serializedObject) use ($labelRepository) { |
||
119 | return self::addLabelName($serializedObject, $labelRepository); |
||
120 | } |
||
121 | ); |
||
122 | |||
123 | $payloadManipulatingSerializer->manipulateEventsOfClass( |
||
124 | 'CultuurNet\UDB3\Label\Events\MadeVisible', |
||
125 | function (array $serializedObject) use ($labelRepository) { |
||
126 | return self::addLabelName($serializedObject, $labelRepository); |
||
127 | } |
||
128 | ); |
||
129 | |||
130 | $payloadManipulatingSerializer->manipulateEventsOfClass( |
||
131 | 'CultuurNet\UDB3\Label\Events\MadePrivate', |
||
132 | function (array $serializedObject) use ($labelRepository) { |
||
133 | return self::addLabelName($serializedObject, $labelRepository); |
||
134 | } |
||
135 | ); |
||
136 | |||
137 | $payloadManipulatingSerializer->manipulateEventsOfClass( |
||
138 | 'CultuurNet\UDB3\Label\Events\MadePublic', |
||
139 | function (array $serializedObject) use ($labelRepository) { |
||
140 | return self::addLabelName($serializedObject, $labelRepository); |
||
141 | } |
||
142 | ); |
||
143 | |||
144 | $payloadManipulatingSerializer->manipulateEventsOfClass( |
||
145 | 'CultuurNet\UDB3\Organizer\Events\LabelAdded', |
||
146 | function (array $serializedObject) use ($labelRepository) { |
||
147 | return self::fixOrganizerLabelEvent($serializedObject, $labelRepository); |
||
148 | } |
||
149 | ); |
||
150 | |||
151 | $payloadManipulatingSerializer->manipulateEventsOfClass( |
||
152 | 'CultuurNet\UDB3\Organizer\Events\LabelRemoved', |
||
153 | function (array $serializedObject) use ($labelRepository) { |
||
154 | return self::fixOrganizerLabelEvent($serializedObject, $labelRepository); |
||
155 | } |
||
156 | ); |
||
157 | |||
158 | $payloadManipulatingSerializer->manipulateEventsOfClass( |
||
159 | 'CultuurNet\UDB3\Event\Events\EventWasLabelled', |
||
160 | function (array $serializedObject) { |
||
161 | $serializedObject['class'] = LabelAdded::class; |
||
162 | |||
163 | $serializedObject = self::replaceEventIdWithItemId($serializedObject); |
||
164 | |||
165 | return $serializedObject; |
||
166 | } |
||
167 | ); |
||
168 | |||
169 | $payloadManipulatingSerializer->manipulateEventsOfClass( |
||
170 | 'CultuurNet\UDB3\Event\EventWasTagged', |
||
171 | View Code Duplication | function (array $serializedObject) { |
|
|
|||
172 | $serializedObject['class'] = LabelAdded::class; |
||
173 | |||
174 | $serializedObject = self::replaceEventIdWithItemId($serializedObject); |
||
175 | |||
176 | $serializedObject = self::replaceKeywordWithLabel($serializedObject); |
||
177 | |||
178 | return $serializedObject; |
||
179 | } |
||
180 | ); |
||
181 | |||
182 | $payloadManipulatingSerializer->manipulateEventsOfClass( |
||
183 | 'CultuurNet\UDB3\Event\TagErased', |
||
184 | View Code Duplication | function (array $serializedObject) { |
|
185 | $serializedObject['class'] = LabelRemoved::class; |
||
186 | |||
187 | $serializedObject = self::replaceEventIdWithItemId($serializedObject); |
||
188 | |||
189 | $serializedObject = self::replaceKeywordWithLabel($serializedObject); |
||
190 | |||
191 | return $serializedObject; |
||
192 | } |
||
193 | ); |
||
194 | |||
195 | $payloadManipulatingSerializer->manipulateEventsOfClass( |
||
196 | 'CultuurNet\UDB3\Event\Events\Unlabelled', |
||
197 | function (array $serializedObject) { |
||
198 | $serializedObject['class'] = LabelRemoved::class; |
||
199 | |||
200 | $serializedObject = self::replaceEventIdWithItemId($serializedObject); |
||
201 | |||
202 | return $serializedObject; |
||
203 | } |
||
204 | ); |
||
205 | |||
206 | /** |
||
207 | * UBD2 IMPORT |
||
208 | */ |
||
209 | |||
210 | $payloadManipulatingSerializer->manipulateEventsOfClass( |
||
211 | 'CultuurNet\UDB3\Event\EventImportedFromUDB2', |
||
212 | function (array $serializedObject) { |
||
213 | $serializedObject['class'] = EventImportedFromUDB2::class; |
||
214 | |||
215 | return $serializedObject; |
||
216 | } |
||
217 | ); |
||
218 | |||
219 | /** |
||
220 | * PLACE FACILITIES EVENT |
||
221 | */ |
||
222 | $payloadManipulatingSerializer->manipulateEventsOfClass( |
||
223 | 'CultuurNet\UDB3\Place\Events\FacilitiesUpdated', |
||
224 | function (array $serializedObject) { |
||
225 | $serializedObject = self::replacePlaceIdWithItemId($serializedObject); |
||
226 | |||
227 | return $serializedObject; |
||
228 | } |
||
229 | ); |
||
230 | |||
231 | /** |
||
232 | * GEOCOORDINATES UPDATED EVENT |
||
233 | */ |
||
234 | $payloadManipulatingSerializer->manipulateEventsOfClass( |
||
235 | 'CultuurNet\UDB3\Place\Events\GeoCoordinatesUpdated', |
||
236 | function (array $serializedObject) { |
||
237 | $serializedObject = self::replacePlaceIdWithItemId($serializedObject); |
||
238 | |||
239 | return $serializedObject; |
||
240 | } |
||
241 | ); |
||
242 | |||
243 | /** |
||
244 | * BOOKING INFO EVENT |
||
245 | */ |
||
246 | $manipulateAvailability = function (array $serializedBookingInfo, $propertyName) { |
||
247 | if (!isset($serializedBookingInfo[$propertyName]) || empty($serializedBookingInfo[$propertyName])) { |
||
248 | $serializedBookingInfo[$propertyName] = null; |
||
249 | return $serializedBookingInfo; |
||
250 | } |
||
251 | |||
252 | $dateTimeString = $serializedBookingInfo[$propertyName]; |
||
253 | |||
254 | $dateTimeFromAtom = \DateTimeImmutable::createFromFormat(\DATE_ATOM, $dateTimeString); |
||
255 | if ($dateTimeFromAtom) { |
||
256 | $serializedBookingInfo[$propertyName] = $dateTimeFromAtom->format(\DATE_ATOM); |
||
257 | return $serializedBookingInfo; |
||
258 | } |
||
259 | |||
260 | $dateTimeFromAtomWithMilliseconds = \DateTimeImmutable::createFromFormat( |
||
261 | 'Y-m-d\TH:i:s.uP', |
||
262 | $dateTimeString |
||
263 | ); |
||
264 | if ($dateTimeFromAtomWithMilliseconds) { |
||
265 | $serializedBookingInfo[$propertyName] = $dateTimeFromAtomWithMilliseconds->format(\DATE_ATOM); |
||
266 | return $serializedBookingInfo; |
||
267 | } |
||
268 | |||
269 | unset($serializedBookingInfo[$propertyName]); |
||
270 | return $serializedBookingInfo; |
||
271 | }; |
||
272 | |||
273 | $manipulateBookingInfoEvent = function (array $serializedEvent) use ($manipulateAvailability) { |
||
274 | $serializedEvent = self::replaceEventIdWithItemId($serializedEvent); |
||
275 | $serializedEvent = self::replacePlaceIdWithItemId($serializedEvent); |
||
276 | |||
277 | $serializedBookingInfo = $serializedEvent['payload']['bookingInfo']; |
||
278 | $serializedBookingInfo = $manipulateAvailability($serializedBookingInfo, 'availabilityStarts'); |
||
279 | $serializedBookingInfo = $manipulateAvailability($serializedBookingInfo, 'availabilityEnds'); |
||
280 | $serializedEvent['payload']['bookingInfo'] = $serializedBookingInfo; |
||
281 | |||
282 | return $serializedEvent; |
||
283 | }; |
||
284 | |||
285 | $payloadManipulatingSerializer->manipulateEventsOfClass( |
||
286 | EventBookingInfoUpdated::class, |
||
287 | $manipulateBookingInfoEvent |
||
288 | ); |
||
289 | |||
290 | $payloadManipulatingSerializer->manipulateEventsOfClass( |
||
291 | PlaceBookingInfoUpdated::class, |
||
292 | $manipulateBookingInfoEvent |
||
293 | ); |
||
294 | |||
295 | /** |
||
296 | * EventEvent to AbstractEvent (Offer) |
||
297 | */ |
||
298 | $refactoredEventEvents = [ |
||
299 | EventTypicalAgeRangeDeleted::class, |
||
300 | EventTypicalAgeRangeUpdated::class, |
||
301 | EventContactPointUpdated::class, |
||
302 | MajorInfoUpdated::class, |
||
303 | EventOrganizerUpdated::class, |
||
304 | EventOrganizerDeleted::class, |
||
305 | EventDescriptionUpdated::class, |
||
306 | EventDeleted::class, |
||
307 | ]; |
||
308 | |||
309 | foreach ($refactoredEventEvents as $refactoredEventEvent) { |
||
310 | $payloadManipulatingSerializer->manipulateEventsOfClass( |
||
311 | $refactoredEventEvent, |
||
312 | function (array $serializedObject) { |
||
313 | $serializedObject = self::replaceEventIdWithItemId($serializedObject); |
||
314 | return $serializedObject; |
||
315 | } |
||
316 | ); |
||
317 | } |
||
318 | |||
319 | /** |
||
320 | * PlaceEvent to AbstractEvent (Offer) |
||
321 | */ |
||
322 | $refactoredPlaceEvents = [ |
||
323 | PlaceOrganizerUpdated::class, |
||
324 | PlaceOrganizerDeleted::class, |
||
325 | PlaceTypicalAgeRangeDeleted::class, |
||
326 | PlaceTypicalAgeRangeUpdated::class, |
||
327 | PlaceContactPointUpdated::class, |
||
328 | PlaceDescriptionUpdated::class, |
||
329 | PlaceDeleted::class, |
||
330 | ]; |
||
331 | |||
332 | foreach ($refactoredPlaceEvents as $refactoredPlaceEvent) { |
||
333 | $payloadManipulatingSerializer->manipulateEventsOfClass( |
||
334 | $refactoredPlaceEvent, |
||
335 | function (array $serializedObject) { |
||
336 | $serializedObject = self::replacePlaceIdWithItemId($serializedObject); |
||
337 | return $serializedObject; |
||
338 | } |
||
339 | ); |
||
340 | } |
||
341 | |||
342 | return $payloadManipulatingSerializer; |
||
343 | } |
||
344 | |||
447 |
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.