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 |
||
8 | class DataArraySerializer extends BaseArraySerializer |
||
9 | { |
||
10 | /** |
||
11 | * Serialize a collection. |
||
12 | * |
||
13 | * @param string $resourceKey |
||
14 | * @param array $data |
||
15 | * |
||
16 | * @return array |
||
17 | */ |
||
18 | View Code Duplication | public function collection($resourceKey, array $data) |
|
26 | |||
27 | /** |
||
28 | * Serialize an item. |
||
29 | * |
||
30 | * @param string $resourceKey |
||
31 | * @param array $data |
||
32 | * |
||
33 | * @return array |
||
34 | */ |
||
35 | View Code Duplication | public function item($resourceKey, array $data) |
|
43 | |||
44 | /** |
||
45 | * @return array |
||
46 | */ |
||
47 | public function null() |
||
53 | |||
54 | /** |
||
55 | * Serialize the meta. |
||
56 | * |
||
57 | * @param array $meta |
||
58 | * |
||
59 | * @return array |
||
60 | */ |
||
61 | public function meta(array $meta) |
||
69 | } |
||
70 |
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.