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 |
||
23 | class RestController extends Controller |
||
24 | { |
||
25 | /** |
||
26 | * Check if repository has rate |
||
27 | * |
||
28 | * @see \RunOpenCode\ExchangeRate\Contract\ManagerInterface::has() |
||
29 | * |
||
30 | * @param Request $request |
||
31 | * |
||
32 | * @return JsonResponse |
||
33 | */ |
||
34 | 3 | View Code Duplication | public function hasAction(Request $request) |
50 | |||
51 | /** |
||
52 | * Get rate. |
||
53 | * |
||
54 | * @see \RunOpenCode\ExchangeRate\Contract\ManagerInterface::get() |
||
55 | * |
||
56 | * @param Request $request |
||
57 | * |
||
58 | * @return JsonResponse |
||
59 | */ |
||
60 | 2 | View Code Duplication | public function getAction(Request $request) |
76 | |||
77 | /** |
||
78 | * Get latest applicable rate. |
||
79 | * |
||
80 | * @see \RunOpenCode\ExchangeRate\Contract\ManagerInterface::latest() |
||
81 | * |
||
82 | * @param Request $request |
||
83 | * |
||
84 | * @return JsonResponse |
||
85 | */ |
||
86 | 2 | View Code Duplication | public function latestAction(Request $request) |
102 | |||
103 | /** |
||
104 | * Get rate applicable for today. |
||
105 | * |
||
106 | * @see \RunOpenCode\ExchangeRate\Contract\ManagerInterface::today() |
||
107 | * |
||
108 | * @param Request $request |
||
109 | * |
||
110 | * @return JsonResponse |
||
111 | */ |
||
112 | 2 | View Code Duplication | public function todayAction(Request $request) |
128 | |||
129 | /** |
||
130 | * Get applicable rate for given date. |
||
131 | * |
||
132 | * @see \RunOpenCode\ExchangeRate\Contract\ManagerInterface::historical() |
||
133 | * |
||
134 | * @param Request $request |
||
135 | * |
||
136 | * @return JsonResponse |
||
137 | */ |
||
138 | 2 | View Code Duplication | public function historicalAction(Request $request) |
154 | |||
155 | /** |
||
156 | * Extract params from request. |
||
157 | * |
||
158 | * @param Request $request |
||
159 | * |
||
160 | * @return array |
||
161 | */ |
||
162 | 11 | private function extractParametersFromRequest(Request $request) |
|
177 | |||
178 | /** |
||
179 | * Build exception response. |
||
180 | * |
||
181 | * @param \Exception $exception |
||
182 | * |
||
183 | * @return JsonResponse |
||
184 | */ |
||
185 | 5 | private function createExceptionResponse(\Exception $exception) |
|
193 | |||
194 | /** |
||
195 | * Build success response |
||
196 | * |
||
197 | * @param mixed $result |
||
198 | * @return JsonResponse |
||
199 | */ |
||
200 | private function createSuccessResponse($result) |
||
219 | |||
220 | /** |
||
221 | * Get manager. |
||
222 | * |
||
223 | * @return ManagerInterface |
||
224 | */ |
||
225 | 11 | private function getManager() |
|
229 | } |
||
230 |
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.