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 BackwardsCompatiblePayloadSerializerFactory |
||
36 | { |
||
37 | |||
38 | private function __construct() |
||
42 | |||
43 | /** |
||
44 | * @return SerializerInterface |
||
45 | */ |
||
46 | public static function createSerializer() |
||
47 | { |
||
48 | $payloadManipulatingSerializer = new PayloadManipulatingSerializer( |
||
49 | new SimpleInterfaceSerializer() |
||
50 | ); |
||
51 | |||
52 | /* |
||
53 | * KEYWORDS EVENTS |
||
54 | */ |
||
55 | |||
56 | $payloadManipulatingSerializer->manipulateEventsOfClass( |
||
57 | 'CultuurNet\UDB3\UsedKeywordsMemory\Created', |
||
58 | function (array $serializedObject) { |
||
59 | $serializedObject['class'] = UsedLabelsMemoryCreated::class; |
||
60 | |||
61 | return $serializedObject; |
||
62 | } |
||
63 | ); |
||
64 | |||
65 | $payloadManipulatingSerializer->manipulateEventsOfClass( |
||
66 | 'CultuurNet\UDB3\UsedKeywordsMemory\KeywordUsed', |
||
67 | function (array $serializedObject) { |
||
68 | $serializedObject['class'] = LabelUsed::class; |
||
69 | |||
70 | $serializedObject = self::replaceKeywordWithLabel($serializedObject); |
||
71 | |||
72 | return $serializedObject; |
||
73 | } |
||
74 | ); |
||
75 | |||
76 | /* |
||
77 | * TRANSLATION EVENTS |
||
78 | */ |
||
79 | |||
80 | $payloadManipulatingSerializer->manipulateEventsOfClass( |
||
81 | 'CultuurNet\UDB3\Event\TitleTranslated', |
||
82 | function (array $serializedObject) { |
||
83 | $serializedObject['class'] = TitleTranslated::class; |
||
84 | |||
85 | $serializedObject = self::replaceEventIdWithItemId($serializedObject); |
||
86 | |||
87 | return $serializedObject; |
||
88 | } |
||
89 | ); |
||
90 | |||
91 | $payloadManipulatingSerializer->manipulateEventsOfClass( |
||
92 | 'CultuurNet\UDB3\Event\DescriptionTranslated', |
||
93 | function (array $serializedObject) { |
||
94 | $serializedObject['class'] = DescriptionTranslated::class; |
||
95 | |||
96 | $serializedObject = self::replaceEventIdWithItemId($serializedObject); |
||
97 | |||
98 | return $serializedObject; |
||
99 | } |
||
100 | ); |
||
101 | |||
102 | /* |
||
103 | * LABEL EVENTS |
||
104 | */ |
||
105 | |||
106 | $payloadManipulatingSerializer->manipulateEventsOfClass( |
||
107 | 'CultuurNet\UDB3\Event\Events\EventWasLabelled', |
||
108 | function (array $serializedObject) { |
||
109 | $serializedObject['class'] = LabelAdded::class; |
||
110 | |||
111 | $serializedObject = self::replaceEventIdWithItemId($serializedObject); |
||
112 | |||
113 | return $serializedObject; |
||
114 | } |
||
115 | ); |
||
116 | |||
117 | $payloadManipulatingSerializer->manipulateEventsOfClass( |
||
118 | 'CultuurNet\UDB3\Event\EventWasTagged', |
||
119 | View Code Duplication | function (array $serializedObject) { |
|
|
|||
120 | $serializedObject['class'] = LabelAdded::class; |
||
121 | |||
122 | $serializedObject = self::replaceEventIdWithItemId($serializedObject); |
||
123 | |||
124 | $serializedObject = self::replaceKeywordWithLabel($serializedObject); |
||
125 | |||
126 | return $serializedObject; |
||
127 | } |
||
128 | ); |
||
129 | |||
130 | $payloadManipulatingSerializer->manipulateEventsOfClass( |
||
131 | 'CultuurNet\UDB3\Event\TagErased', |
||
132 | View Code Duplication | function (array $serializedObject) { |
|
133 | $serializedObject['class'] = LabelDeleted::class; |
||
134 | |||
135 | $serializedObject = self::replaceEventIdWithItemId($serializedObject); |
||
136 | |||
137 | $serializedObject = self::replaceKeywordWithLabel($serializedObject); |
||
138 | |||
139 | return $serializedObject; |
||
140 | } |
||
141 | ); |
||
142 | |||
143 | $payloadManipulatingSerializer->manipulateEventsOfClass( |
||
144 | 'CultuurNet\UDB3\Event\Events\Unlabelled', |
||
145 | function (array $serializedObject) { |
||
146 | $serializedObject['class'] = LabelDeleted::class; |
||
147 | |||
148 | $serializedObject = self::replaceEventIdWithItemId($serializedObject); |
||
149 | |||
150 | return $serializedObject; |
||
151 | } |
||
152 | ); |
||
153 | |||
154 | $payloadManipulatingSerializer->manipulateEventsOfClass( |
||
155 | 'CultuurNet\UDB3\Event\Events\LabelsApplied', |
||
156 | function (array $serializedObject) { |
||
157 | $serializedObject['class'] = LabelsMerged::class; |
||
158 | |||
159 | $keywordsString = $serializedObject['payload']['keywords_string']; |
||
160 | |||
161 | $query = array(); |
||
162 | parse_str($keywordsString, $query); |
||
163 | |||
164 | $keywords = explode(';', $query['keywords']); |
||
165 | $visibles = explode(';', $query['visibles']); |
||
166 | |||
167 | $labelsArray = array(); |
||
168 | |||
169 | foreach ($keywords as $key => $keyword) { |
||
170 | $visible = 'true' === $visibles[$key]; |
||
171 | $labelsArray[] = new Label( |
||
172 | $keyword, |
||
173 | $visible |
||
174 | ); |
||
175 | } |
||
176 | |||
177 | $labels = array_map( |
||
178 | function (Label $label) { |
||
179 | return [ |
||
180 | 'text' => (string) $label, |
||
181 | 'visible' => $label->isVisible(), |
||
182 | ]; |
||
183 | }, |
||
184 | $labelsArray |
||
185 | ); |
||
186 | |||
187 | $serializedObject['payload']['labels'] = $labels; |
||
188 | unset($serializedObject['payload']['keywords_string']); |
||
189 | |||
190 | return $serializedObject; |
||
191 | } |
||
192 | ); |
||
193 | |||
194 | /** |
||
195 | * UBD2 IMPORT |
||
196 | */ |
||
197 | |||
198 | $payloadManipulatingSerializer->manipulateEventsOfClass( |
||
199 | 'CultuurNet\UDB3\Event\EventImportedFromUDB2', |
||
200 | function (array $serializedObject) { |
||
201 | $serializedObject['class'] = EventImportedFromUDB2::class; |
||
202 | |||
203 | return $serializedObject; |
||
204 | } |
||
205 | ); |
||
206 | |||
207 | /** |
||
208 | * BOOKING INFO |
||
209 | */ |
||
210 | |||
211 | $payloadManipulatingSerializer->manipulateEventsOfClass( |
||
212 | BookingInfoUpdated::class, |
||
213 | function (array $serializedObject) { |
||
214 | |||
215 | $serializedObject = self::replaceEventIdWithItemId($serializedObject); |
||
216 | |||
217 | return $serializedObject; |
||
218 | } |
||
219 | ); |
||
220 | |||
221 | /** |
||
222 | * TYPICAL AGE RANGE |
||
223 | */ |
||
224 | |||
225 | $payloadManipulatingSerializer->manipulateEventsOfClass( |
||
226 | TypicalAgeRangeDeleted::class, |
||
227 | function (array $serializedObject) { |
||
228 | |||
229 | $serializedObject = self::replaceEventIdWithItemId($serializedObject); |
||
230 | |||
231 | return $serializedObject; |
||
232 | } |
||
233 | ); |
||
234 | |||
235 | $payloadManipulatingSerializer->manipulateEventsOfClass( |
||
236 | TypicalAgeRangeUpdated::class, |
||
237 | function (array $serializedObject) { |
||
238 | |||
239 | $serializedObject = self::replaceEventIdWithItemId($serializedObject); |
||
240 | |||
241 | return $serializedObject; |
||
242 | } |
||
243 | ); |
||
244 | |||
245 | /** |
||
246 | * CONTACT POINT |
||
247 | */ |
||
248 | |||
249 | $payloadManipulatingSerializer->manipulateEventsOfClass( |
||
250 | ContactPointUpdated::class, |
||
251 | function (array $serializedObject) { |
||
252 | |||
253 | $serializedObject = self::replaceEventIdWithItemId($serializedObject); |
||
254 | |||
255 | return $serializedObject; |
||
256 | } |
||
257 | ); |
||
258 | |||
259 | /** |
||
260 | * MAJOR INFO |
||
261 | */ |
||
262 | |||
263 | $payloadManipulatingSerializer->manipulateEventsOfClass( |
||
264 | MajorInfoUpdated::class, |
||
265 | function (array $serializedObject) { |
||
266 | |||
267 | $serializedObject = self::replaceEventIdWithItemId($serializedObject); |
||
268 | |||
269 | return $serializedObject; |
||
270 | } |
||
271 | ); |
||
272 | |||
273 | /** |
||
274 | * ORGANIZER |
||
275 | */ |
||
276 | |||
277 | $payloadManipulatingSerializer->manipulateEventsOfClass( |
||
278 | OrganizerUpdated::class, |
||
279 | function (array $serializedObject) { |
||
280 | |||
281 | $serializedObject = self::replaceEventIdWithItemId($serializedObject); |
||
282 | |||
283 | return $serializedObject; |
||
284 | } |
||
285 | ); |
||
286 | |||
287 | $payloadManipulatingSerializer->manipulateEventsOfClass( |
||
288 | OrganizerDeleted::class, |
||
289 | function (array $serializedObject) { |
||
290 | |||
291 | $serializedObject = self::replaceEventIdWithItemId($serializedObject); |
||
292 | |||
293 | return $serializedObject; |
||
294 | } |
||
295 | ); |
||
296 | |||
297 | return $payloadManipulatingSerializer; |
||
298 | } |
||
299 | |||
300 | /** |
||
301 | * @param array $serializedObject |
||
302 | * @return array |
||
303 | */ |
||
304 | private static function replaceEventIdWithItemId(array $serializedObject) |
||
314 | |||
315 | /** |
||
316 | * @param array $serializedObject |
||
317 | * @return array |
||
318 | */ |
||
319 | private static function replaceKeywordWithLabel(array $serializedObject) |
||
327 | } |
||
328 |
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.