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 |
||
16 | abstract class AbstractRestfulController extends AbstractController |
||
17 | { |
||
18 | |||
19 | /** |
||
20 | * GET |
||
21 | * |
||
22 | * @return void |
||
23 | */ |
||
24 | public function get() |
||
27 | |||
28 | /** |
||
29 | * POST |
||
30 | * |
||
31 | * @return void |
||
32 | */ |
||
33 | public function create() |
||
36 | |||
37 | /** |
||
38 | * UPDATE |
||
39 | * |
||
40 | * @return void |
||
41 | */ |
||
42 | public function update() |
||
45 | |||
46 | /** |
||
47 | * DELETE |
||
48 | * |
||
49 | * @return void |
||
50 | */ |
||
51 | public function delete() |
||
54 | |||
55 | /** |
||
56 | * Return success response |
||
57 | * |
||
58 | * @param array $data The response data |
||
59 | * |
||
60 | * @return JsonResponse |
||
61 | */ |
||
62 | View Code Duplication | protected function success($data = []) |
|
78 | |||
79 | /** |
||
80 | * Return erroneous response |
||
81 | * |
||
82 | * @param array $errors The response errors |
||
83 | * |
||
84 | * @return JsonResponse |
||
85 | */ |
||
86 | View Code Duplication | protected function error($errors = []) |
|
101 | |||
102 | } |
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.