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 |
||
28 | class GeocoderService extends AbstractService |
||
29 | { |
||
30 | /** |
||
31 | * @param HttpClient $client |
||
32 | * @param MessageFactory $messageFactory |
||
33 | */ |
||
34 | public function __construct(HttpClient $client, MessageFactory $messageFactory) |
||
38 | |||
39 | /** |
||
40 | * @param GeocoderRequestInterface $request |
||
41 | * |
||
42 | * @return GeocoderResponse |
||
43 | */ |
||
44 | View Code Duplication | public function geocode(GeocoderRequestInterface $request) |
|
51 | |||
52 | /** |
||
53 | * @param string $data |
||
54 | * |
||
55 | * @return mixed[] |
||
56 | */ |
||
57 | private function parse($data) |
||
69 | |||
70 | /** |
||
71 | * @param mixed[] $data |
||
72 | * |
||
73 | * @return GeocoderResponse |
||
74 | */ |
||
75 | View Code Duplication | private function buildResponse(array $data) |
|
83 | |||
84 | /** |
||
85 | * @param mixed[] $data |
||
86 | * |
||
87 | * @return GeocoderResult[] |
||
88 | */ |
||
89 | private function buildResults(array $data) |
||
99 | |||
100 | /** |
||
101 | * @param mixed[] $data |
||
102 | * |
||
103 | * @return GeocoderResult |
||
104 | */ |
||
105 | private function buildResult(array $data) |
||
123 | |||
124 | /** |
||
125 | * @param mixed[] $data |
||
126 | * |
||
127 | * @return GeocoderAddress[] |
||
128 | */ |
||
129 | private function buildAddresses(array $data) |
||
139 | |||
140 | /** |
||
141 | * @param mixed[] $data |
||
142 | * |
||
143 | * @return GeocoderAddress |
||
144 | */ |
||
145 | private function buildAddress(array $data) |
||
154 | |||
155 | /** |
||
156 | * @param mixed[] $data |
||
157 | * |
||
158 | * @return GeocoderGeometry |
||
159 | */ |
||
160 | private function buildGeometry(array $data) |
||
173 | |||
174 | /** |
||
175 | * @param mixed[] $data |
||
176 | * |
||
177 | * @return Bound |
||
178 | */ |
||
179 | private function buildBound(array $data) |
||
186 | |||
187 | /** |
||
188 | * @param mixed[] $data |
||
189 | * |
||
190 | * @return Coordinate |
||
191 | */ |
||
192 | private function buildCoordinate(array $data) |
||
196 | } |
||
197 |
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.