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 |
||
9 | class PlaceSerializer |
||
10 | { |
||
11 | /** |
||
12 | * @param Place $place |
||
13 | * |
||
14 | * @return array |
||
15 | */ |
||
16 | View Code Duplication | public function serialize(Place $place) |
|
34 | |||
35 | /** |
||
36 | * @param Place $place |
||
37 | * @param array $data |
||
38 | */ |
||
39 | private function setId(Place $place, array &$data) |
||
45 | |||
46 | /** |
||
47 | * @param Place $place |
||
48 | * @param array $data |
||
49 | */ |
||
50 | private function setName(Place $place, array &$data) |
||
56 | |||
57 | /** |
||
58 | * @param Place $place |
||
59 | * @param array $data |
||
60 | */ |
||
61 | private function setLocation(Place $place, array &$data) |
||
72 | |||
73 | private function createGeometryNode(array &$data) |
||
79 | |||
80 | /** |
||
81 | * @param Place $place |
||
82 | * @param array $data |
||
83 | */ |
||
84 | private function setTypes(Place $place, array &$data) |
||
91 | |||
92 | /** |
||
93 | * @param Place $place |
||
94 | * @param array $data |
||
95 | */ |
||
96 | private function setIcon(Place $place, array &$data) |
||
102 | |||
103 | /** |
||
104 | * @param Place $place |
||
105 | * @param array $data |
||
106 | */ |
||
107 | private function setVicinity(Place $place, array &$data) |
||
113 | |||
114 | /** |
||
115 | * @param Place $place |
||
116 | * @param array $data |
||
117 | */ |
||
118 | private function setFormattedAddress(Place $place, array &$data) |
||
124 | |||
125 | /** |
||
126 | * @param Place $place |
||
127 | * @param array $data |
||
128 | */ |
||
129 | private function setFormattedPhoneNumber(Place $place, array &$data) |
||
135 | |||
136 | /** |
||
137 | * @param Place $place |
||
138 | * @param array $data |
||
139 | */ |
||
140 | private function setInternationalPhoneNumber(Place $place, array &$data) |
||
146 | |||
147 | /** |
||
148 | * @param Place $place |
||
149 | * @param array $data |
||
150 | */ |
||
151 | private function setUrl(Place $place, array &$data) |
||
157 | |||
158 | /** |
||
159 | * @param Place $place |
||
160 | * @param array $data |
||
161 | */ |
||
162 | private function setWebsite(Place $place, array &$data) |
||
168 | |||
169 | /** |
||
170 | * @param TypeCollection $types |
||
171 | * |
||
172 | * @return array |
||
173 | */ |
||
174 | private function serializeTypes(TypeCollection $types) |
||
184 | } |
||
185 |
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.