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 |
||
15 | class DefaultController extends AbstractController |
||
16 | { |
||
17 | /** |
||
18 | * @var GroupDefinitionAwareRollout |
||
19 | */ |
||
20 | private $rollout; |
||
21 | |||
22 | /** |
||
23 | * @var UserProviderInterface |
||
24 | */ |
||
25 | private $userProvider; |
||
26 | |||
27 | /** |
||
28 | * @param GroupDefinitionAwareRollout $rollout |
||
29 | * @param UserProviderInterface $userProvider |
||
30 | */ |
||
31 | public function __construct(GroupDefinitionAwareRollout $rollout, UserProviderInterface $userProvider) |
||
36 | |||
37 | /** |
||
38 | * @return Response |
||
39 | */ |
||
40 | public function indexAction() |
||
44 | |||
45 | /** |
||
46 | * @param string $feature |
||
47 | * |
||
48 | * @return RedirectResponse |
||
49 | */ |
||
50 | public function activateAction($feature) |
||
58 | |||
59 | /** |
||
60 | * @param string $feature |
||
61 | * |
||
62 | * @return RedirectResponse |
||
63 | */ |
||
64 | public function deactivateAction($feature) |
||
72 | |||
73 | /** |
||
74 | * @param string $feature |
||
75 | * |
||
76 | * @return RedirectResponse |
||
77 | */ |
||
78 | public function incrementPercentageAction($feature) |
||
82 | |||
83 | /** |
||
84 | * @param string $feature |
||
85 | * |
||
86 | * @return RedirectResponse |
||
87 | */ |
||
88 | public function decrementPercentageAction($feature) |
||
92 | |||
93 | /** |
||
94 | * @param string $feature |
||
95 | * @param string $group |
||
96 | * |
||
97 | * @return RedirectResponse |
||
98 | */ |
||
99 | public function activateGroupAction($feature, $group) |
||
107 | |||
108 | /** |
||
109 | * @param string $feature |
||
110 | * @param string $group |
||
111 | * |
||
112 | * @return RedirectResponse |
||
113 | */ |
||
114 | public function deactivateGroupAction($feature, $group) |
||
122 | |||
123 | /** |
||
124 | * @param Request $request |
||
125 | * @param string $feature |
||
126 | * |
||
127 | * @return RedirectResponse |
||
128 | */ |
||
129 | View Code Duplication | public function activateUserAction(Request $request, $feature) |
|
151 | |||
152 | /** |
||
153 | * @param string $feature |
||
154 | * @param string $id |
||
155 | * |
||
156 | * @return RedirectResponse |
||
157 | */ |
||
158 | View Code Duplication | public function deactivateUserAction($feature, $id) |
|
179 | |||
180 | /** |
||
181 | * @param string $feature |
||
182 | * |
||
183 | * @return RedirectResponse |
||
184 | */ |
||
185 | public function removeAction($feature) |
||
193 | |||
194 | /** |
||
195 | * @param Request $request |
||
196 | * @param string $feature |
||
197 | * |
||
198 | * @return RedirectResponse |
||
199 | */ |
||
200 | public function setRequestParamAction(Request $request, $feature) |
||
216 | |||
217 | /** |
||
218 | * Abstract out common functionality |
||
219 | * |
||
220 | * @param string $feature |
||
221 | * @param int $increment |
||
222 | * |
||
223 | * @return RedirectResponse |
||
224 | */ |
||
225 | private function changePercentage($feature, $increment) |
||
234 | } |
||
235 |
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.