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 |
||
19 | class MapMapper extends AbstractCollectionMapper |
||
20 | { |
||
21 | /** |
||
22 | * @param Slumberer $slumberer |
||
23 | * @param array|\Traversable $value |
||
24 | * |
||
25 | * @return null|\stdClass We return a std class as this will ensure json-encode will create something like {"0":1} |
||
26 | 17 | */ |
|
27 | View Code Duplication | public function slumber(Slumberer $slumberer, $value) |
|
41 | 5 | ||
42 | 5 | /** |
|
43 | * @param Awaker $awaker |
||
44 | * @param array|\Traversable $value |
||
45 | * |
||
46 | 9 | * @return array|Collection |
|
47 | */ |
||
48 | View Code Duplication | public function awake(Awaker $awaker, $value) |
|
62 | 8 | ||
63 | 8 | /** |
|
64 | * @param Slumberer $slumberer |
||
65 | 8 | * @param array $value |
|
66 | * @param Mapper $nested |
||
67 | 6 | * |
|
68 | * @return \stdClass |
||
69 | */ |
||
70 | 6 | private function slumberKeepNulls(Slumberer $slumberer, $value, Mapper $nested) |
|
80 | |||
81 | /** |
||
82 | * @param Slumberer $slumberer |
||
83 | * @param array $value |
||
84 | * @param Mapper $nested |
||
85 | * |
||
86 | * @return \stdClass |
||
87 | */ |
||
88 | private function slumberFilterNulls(Slumberer $slumberer, $value, Mapper $nested) |
||
104 | |||
105 | /** |
||
106 | * @param Awaker $awaker |
||
107 | * @param array $value |
||
108 | * @param Mapper $nested |
||
109 | * |
||
110 | * @return array |
||
111 | */ |
||
112 | private function awakeKeepNulls(Awaker $awaker, $value, Mapper $nested) |
||
123 | |||
124 | /** |
||
125 | * @param Awaker $awaker |
||
126 | * @param array $value |
||
127 | * @param Mapper $nested |
||
128 | * |
||
129 | * @return array |
||
130 | */ |
||
131 | private function awakeFilterNulls(Awaker $awaker, $value, Mapper $nested) |
||
147 | } |
||
148 |
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.