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 |
||
26 | class Directions extends AbstractService |
||
27 | { |
||
28 | /** |
||
29 | * @param HttpClient $client |
||
30 | * @param MessageFactory $messageFactory |
||
31 | */ |
||
32 | public function __construct(HttpClient $client, MessageFactory $messageFactory) |
||
36 | |||
37 | /** |
||
38 | * @param DirectionsRequest $request |
||
39 | * |
||
40 | * @return DirectionsResponse |
||
41 | */ |
||
42 | View Code Duplication | public function route(DirectionsRequest $request) |
|
49 | |||
50 | /** |
||
51 | * @param string $data |
||
52 | * |
||
53 | * @return mixed[] |
||
54 | */ |
||
55 | private function parse($data) |
||
68 | |||
69 | /** |
||
70 | * @param mixed[] $data |
||
71 | * |
||
72 | * @return DirectionsResponse |
||
73 | */ |
||
74 | private function buildResponse(array $data) |
||
90 | |||
91 | /** |
||
92 | * @param mixed[] $data |
||
93 | * |
||
94 | * @return DirectionsRoute[] |
||
95 | */ |
||
96 | private function buildRoutes(array $data) |
||
105 | |||
106 | /** |
||
107 | * @param mixed[] $data |
||
108 | * |
||
109 | * @return DirectionsRoute |
||
110 | */ |
||
111 | private function buildRoute(array $data) |
||
125 | |||
126 | /** |
||
127 | * @param mixed[] $data |
||
128 | * |
||
129 | * @return DirectionsGeocoded[] |
||
130 | */ |
||
131 | private function buildGeocodedWaypoints(array $data) |
||
140 | |||
141 | /** |
||
142 | * @param mixed[] $data |
||
143 | * |
||
144 | * @return DirectionsGeocoded |
||
145 | */ |
||
146 | private function buildGeocodedWaypoint(array $data) |
||
156 | |||
157 | /** |
||
158 | * @param mixed[] $data |
||
159 | * |
||
160 | * @return DirectionsLeg[] |
||
161 | */ |
||
162 | private function buildLegs(array $data) |
||
171 | |||
172 | /** |
||
173 | * @param mixed[] $data |
||
174 | * |
||
175 | * @return DirectionsLeg |
||
176 | */ |
||
177 | private function buildLeg(array $data) |
||
194 | |||
195 | /** |
||
196 | * @codeCoverageIgnore |
||
197 | * |
||
198 | * @param mixed[] $data |
||
199 | * |
||
200 | * @return DirectionsFare |
||
201 | */ |
||
202 | private function buildFare(array $data) |
||
211 | |||
212 | /** |
||
213 | * @param mixed[] $data |
||
214 | * |
||
215 | * @return DirectionsStep[] |
||
216 | */ |
||
217 | private function buildSteps(array $data) |
||
226 | |||
227 | /** |
||
228 | * @param mixed[] $data |
||
229 | * |
||
230 | * @return DirectionsStep |
||
231 | */ |
||
232 | private function buildStep(array $data) |
||
245 | |||
246 | /** |
||
247 | * @param mixed[] $data |
||
248 | * |
||
249 | * @return Bound |
||
250 | */ |
||
251 | private function buildBound(array $data) |
||
258 | |||
259 | /** |
||
260 | * @param mixed[] $data |
||
261 | * |
||
262 | * @return Coordinate |
||
263 | */ |
||
264 | private function buildCoordinate(array $data) |
||
268 | |||
269 | /** |
||
270 | * @param mixed[] $data |
||
271 | * |
||
272 | * @return Distance |
||
273 | */ |
||
274 | private function buildDistance(array $data) |
||
278 | |||
279 | /** |
||
280 | * @param mixed[] $data |
||
281 | * |
||
282 | * @return Duration |
||
283 | */ |
||
284 | private function buildDuration(array $data) |
||
288 | |||
289 | /** |
||
290 | * @param string[] $data |
||
291 | * |
||
292 | * @return EncodedPolyline |
||
293 | */ |
||
294 | private function buildEncodedPolyline(array $data) |
||
298 | } |
||
299 |
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.