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 DistanceMatrixService 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 DistanceMatrixRequestInterface $request |
||
41 | * |
||
42 | * @return DistanceMatrixResponse |
||
43 | */ |
||
44 | View Code Duplication | public function process(DistanceMatrixRequestInterface $request) |
|
51 | |||
52 | /** |
||
53 | * @param string $data |
||
54 | * |
||
55 | * @return mixed[] |
||
56 | */ |
||
57 | private function parse($data) |
||
70 | |||
71 | /** |
||
72 | * @param mixed[] $data |
||
73 | * |
||
74 | * @return DistanceMatrixResponse |
||
75 | */ |
||
76 | private function buildResponse(array $data) |
||
86 | |||
87 | /** |
||
88 | * @param mixed[] $data |
||
89 | * |
||
90 | * @return DistanceMatrixRow[] |
||
91 | */ |
||
92 | private function buildRows($data) |
||
102 | |||
103 | /** |
||
104 | * @param mixed[] $data |
||
105 | * |
||
106 | * @return DistanceMatrixRow |
||
107 | */ |
||
108 | private function buildRow($data) |
||
115 | |||
116 | /** |
||
117 | * @param mixed[] $data |
||
118 | * |
||
119 | * @return DistanceMatrixElement[] |
||
120 | */ |
||
121 | private function buildElements(array $data) |
||
131 | |||
132 | /** |
||
133 | * @param mixed[] $data |
||
134 | * |
||
135 | * @return DistanceMatrixElement |
||
136 | */ |
||
137 | private function buildElement(array $data) |
||
160 | |||
161 | /** |
||
162 | * @param mixed[] $data |
||
163 | * |
||
164 | * @return Distance |
||
165 | */ |
||
166 | private function buildDistance(array $data) |
||
170 | |||
171 | /** |
||
172 | * @param mixed[] $data |
||
173 | * |
||
174 | * @return Duration |
||
175 | */ |
||
176 | private function buildDuration(array $data) |
||
180 | |||
181 | /** |
||
182 | * @param mixed[] $data |
||
183 | * |
||
184 | * @return Fare |
||
185 | */ |
||
186 | private function buildFare(array $data) |
||
190 | } |
||
191 |
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.