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 |
||
20 | class OrderedSerializer extends Serializer |
||
21 | { |
||
22 | /** |
||
23 | * {@inheritdoc} |
||
24 | */ |
||
25 | View Code Duplication | public function normalize($data, $format = null, array $context = []) |
|
33 | |||
34 | /** |
||
35 | * {@inheritdoc} |
||
36 | */ |
||
37 | View Code Duplication | public function denormalize($data, $type, $format = null, array $context = []) |
|
46 | |||
47 | /** |
||
48 | * Orders objects if can be done. |
||
49 | * |
||
50 | * @param array $data Data to order. |
||
51 | * |
||
52 | * @return array |
||
53 | */ |
||
54 | private function order(array $data) |
||
71 | |||
72 | /** |
||
73 | * Filters out data which can be ordered. |
||
74 | * |
||
75 | * @param array $array Data to filter out. |
||
76 | * |
||
77 | * @return array |
||
78 | */ |
||
79 | private function filterOrderable($array) |
||
88 | } |
||
89 |
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.