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 |
||
21 | class CachesController extends AbstractController |
||
22 | { |
||
23 | private $connection; |
||
24 | |||
25 | private $cachesRepository; |
||
26 | |||
27 | /** |
||
28 | * CachesController constructor. |
||
29 | * |
||
30 | * @param Connection $connection |
||
31 | * @param CachesRepository $cachesRepository |
||
32 | */ |
||
33 | public function __construct(Connection $connection, CachesRepository $cachesRepository) |
||
38 | |||
39 | /** |
||
40 | * @param Request $request |
||
41 | * @Route("/caches", name="caches_index") |
||
42 | * |
||
43 | * @return Response |
||
44 | */ |
||
45 | View Code Duplication | public function cachesController_index(Request $request) |
|
|
|||
46 | : Response { |
||
47 | $fetchedCaches = ''; |
||
48 | |||
49 | // create input field for caches_by_searchfield |
||
50 | $form = $this->createForm(CachesFormType::class); |
||
51 | |||
52 | // see: https://symfonycasts.com/screencast/symfony-forms/form-submit |
||
53 | // handles the request (submit-button of the form), but only if there is a POST request |
||
54 | $form->handleRequest($request); |
||
55 | // if is true only if there is a request submitted and it is valid |
||
56 | if ($form->isSubmitted() && $form->isValid()) { |
||
57 | // read content of form input field |
||
58 | $inputData = $form->getData(); |
||
59 | |||
60 | // send request to DB |
||
61 | $fetchedCaches = $this->getCachesForSearchField($inputData['content_caches_searchfield']); |
||
62 | } |
||
63 | |||
64 | if ($fetchedCaches === '') { |
||
65 | return $this->render( |
||
66 | 'backend/caches/index.html.twig', [ |
||
67 | 'cachesForm' => $form->createView(), |
||
68 | ] |
||
69 | ); |
||
70 | } else { |
||
71 | return $this->render( |
||
72 | 'backend/caches/basicview.html.twig', [ |
||
73 | 'cachesForm' => $form->createView(), |
||
74 | 'caches_by_searchfield' => $fetchedCaches |
||
75 | ] |
||
76 | ); |
||
77 | } |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * @param string $wpID |
||
82 | * |
||
83 | * @return Response |
||
84 | * @Route("/cache/{wpID}", name="cache_by_wp_oc_gc") |
||
85 | */ |
||
86 | public function search_by_cache_wp(string $wpID) |
||
98 | |||
99 | /** |
||
100 | * @param string $searchtext |
||
101 | * |
||
102 | * @return array |
||
103 | */ |
||
104 | public function getCachesForSearchField(string $searchtext) |
||
127 | |||
128 | /** |
||
129 | * @param int $id |
||
130 | * |
||
131 | * @return array |
||
132 | * @throws RecordNotFoundException |
||
133 | */ |
||
134 | public function getCacheDetailsById(int $id) : array { |
||
139 | |||
140 | /** |
||
141 | * @param string $wayPoint |
||
142 | * |
||
143 | * @return array |
||
144 | * @throws RecordNotFoundException |
||
145 | */ |
||
146 | public function getCacheDetailsByWayPoint(string $wayPoint) : array { |
||
151 | } |
||
152 |
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.