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 |
||
27 | class CreateController extends Controller |
||
28 | { |
||
29 | /** |
||
30 | * Main controller action |
||
31 | * |
||
32 | * @param Request $request |
||
33 | * |
||
34 | * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response |
||
35 | */ |
||
36 | 6 | View Code Duplication | public function indexAction(Request $request) |
52 | |||
53 | /** |
||
54 | * Handle form submission. |
||
55 | * |
||
56 | * @param Form $form |
||
57 | * @param Request $request |
||
58 | * |
||
59 | * @return bool TRUE if successful |
||
60 | */ |
||
61 | 5 | protected function handleForm(Form $form, Request $request) |
|
85 | |||
86 | /** |
||
87 | * Get FQCN of FormType form. |
||
88 | * |
||
89 | * @return string |
||
90 | */ |
||
91 | 5 | protected function getFormType() |
|
95 | |||
96 | /** |
||
97 | * Get form. |
||
98 | * |
||
99 | * @return Form |
||
100 | */ |
||
101 | 5 | protected function getForm() |
|
105 | |||
106 | /** |
||
107 | * Save rate. |
||
108 | * |
||
109 | * @param RateInterface $rate |
||
110 | * |
||
111 | * @return TRUE if successful. |
||
112 | */ |
||
113 | 2 | View Code Duplication | protected function save(RateInterface $rate) |
124 | |||
125 | /** |
||
126 | * Redirect after success. |
||
127 | * |
||
128 | * @return \Symfony\Component\HttpFoundation\RedirectResponse |
||
129 | */ |
||
130 | 1 | protected function redirectAfterSuccess() |
|
134 | |||
135 | /** |
||
136 | * Get Twig template path. |
||
137 | * |
||
138 | * @return string |
||
139 | */ |
||
140 | 5 | protected function getTwigTemplate() |
|
144 | } |
||
145 |
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.