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 |
||
31 | class TranslationManager |
||
32 | { |
||
33 | /** |
||
34 | * @var Repository |
||
35 | */ |
||
36 | private $repository; |
||
37 | |||
38 | /** |
||
39 | * @var PropertyAccessorInterface |
||
40 | */ |
||
41 | private $accessor; |
||
42 | |||
43 | /** |
||
44 | * @var EventDispatcherInterface |
||
45 | */ |
||
46 | private $dispatcher; |
||
47 | |||
48 | /** |
||
49 | * @param Repository $repository |
||
50 | * @param EventDispatcherInterface $dispatcher |
||
51 | */ |
||
52 | public function __construct(Repository $repository, EventDispatcherInterface $dispatcher) |
||
57 | |||
58 | /** |
||
59 | * Adds object to translations. |
||
60 | * |
||
61 | * @param Request $request |
||
62 | */ |
||
63 | View Code Duplication | public function add(Request $request) |
|
70 | |||
71 | /** |
||
72 | * Edits object from translation. |
||
73 | * |
||
74 | * @param Request $request Http request object. |
||
75 | */ |
||
76 | public function edit(Request $request) |
||
87 | |||
88 | /** |
||
89 | * Removes object from translations. |
||
90 | * |
||
91 | * @param Request $request Http request object. |
||
92 | */ |
||
93 | View Code Duplication | public function delete(Request $request) |
|
100 | |||
101 | /** |
||
102 | * Returns specific values from objects. |
||
103 | * |
||
104 | * @param Request $request Http request object. |
||
105 | * |
||
106 | * @return array |
||
107 | */ |
||
108 | public function get(Request $request) |
||
134 | |||
135 | /** |
||
136 | * Adds object to translation. |
||
137 | * |
||
138 | * @param object $document |
||
139 | * @param array $options |
||
140 | */ |
||
141 | private function addObject($document, $options) |
||
166 | |||
167 | /** |
||
168 | * Removes message from document based on options. |
||
169 | * |
||
170 | * @param object $document |
||
171 | * @param array $options |
||
172 | */ |
||
173 | private function deleteObject($document, $options) |
||
187 | |||
188 | /** |
||
189 | * Edits message from document based on options. |
||
190 | * |
||
191 | * @param object $document |
||
192 | * @param array $options |
||
193 | */ |
||
194 | private function editObject($document, $options) |
||
217 | |||
218 | /** |
||
219 | * Finds object by property and its value from iterator and returns key. |
||
220 | * |
||
221 | * @param \Iterator $objects |
||
222 | * @param array $options |
||
223 | * |
||
224 | * @return int |
||
225 | */ |
||
226 | private function findObject($objects, $options) |
||
245 | |||
246 | /** |
||
247 | * Parses http request content from json to array. |
||
248 | * |
||
249 | * @param Request $request Http request object. |
||
250 | * |
||
251 | * @return array |
||
252 | * |
||
253 | * @throws BadRequestHttpException |
||
254 | */ |
||
255 | View Code Duplication | private function parseJsonContent(Request $request) |
|
265 | |||
266 | /** |
||
267 | * Commits document into elasticsearch client. |
||
268 | * |
||
269 | * @param object $document |
||
270 | */ |
||
271 | private function commitTranslation($document) |
||
276 | |||
277 | /** |
||
278 | * Returns translation from elasticsearch. |
||
279 | * |
||
280 | * @param string $id |
||
281 | * |
||
282 | * @return object |
||
283 | * |
||
284 | * @throws BadRequestHttpException |
||
285 | */ |
||
286 | private function getTranslation($id) |
||
300 | |||
301 | /** |
||
302 | * Returns property accessor instance. |
||
303 | * |
||
304 | * @return PropertyAccessorInterface |
||
305 | */ |
||
306 | private function getAccessor() |
||
317 | |||
318 | /** |
||
319 | * Sets `updated_at` property. |
||
320 | * |
||
321 | * @param object $object |
||
322 | */ |
||
323 | private function updateTimestamp($object) |
||
331 | |||
332 | /** |
||
333 | * Sets object properties into provided object. |
||
334 | * |
||
335 | * @param object $object Object to set properties into. |
||
336 | * @param array $properties Array of properties to set. |
||
337 | */ |
||
338 | private function setObjectProperties($object, $properties) |
||
344 | } |
||
345 |
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.