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 | class TransfersController extends AbstractController |
||
17 | { |
||
18 | /** |
||
19 | * @Route("/", name="index", methods={"GET"}) |
||
20 | * |
||
21 | * @return Response |
||
22 | */ |
||
23 | public function index(): Response |
||
27 | |||
28 | /** |
||
29 | * @Route("/create", name="create-get", methods={"GET"}) |
||
30 | * |
||
31 | * @return Response |
||
32 | */ |
||
33 | public function createTransferGet(): Response |
||
37 | |||
38 | /** |
||
39 | * @Route("/create", name="create-post", methods={"POST"}) |
||
40 | * |
||
41 | * @return Response |
||
42 | */ |
||
43 | View Code Duplication | public function createTransferPost(): Response |
|
54 | |||
55 | /** |
||
56 | * @Route("/search", name="search", methods={"GET"}) |
||
57 | * |
||
58 | * @return Response |
||
59 | * @throws HyperwalletApiException |
||
60 | */ |
||
61 | public function searchTransfer(): Response |
||
68 | |||
69 | /** |
||
70 | * @Route("/{transferToken}", name="get", methods={"GET"}) |
||
71 | * |
||
72 | * @param string $transferToken |
||
73 | * |
||
74 | * @return Response |
||
75 | */ |
||
76 | View Code Duplication | public function getTransfer(string $transferToken): Response |
|
84 | |||
85 | /** |
||
86 | * @Route("/{transferToken}/commit", name="commit", methods={"GET"}) |
||
87 | * |
||
88 | * @param string $transferToken |
||
89 | * |
||
90 | * @return Response |
||
91 | */ |
||
92 | public function commitTransfer(string $transferToken): Response |
||
100 | } |
||
101 |
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.