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 GroupControllerProvider implements ControllerProviderInterface |
||
17 | { |
||
18 | /** |
||
19 | * @var Application Reference to the application container |
||
20 | */ |
||
21 | private $app; |
||
22 | |||
23 | /** |
||
24 | * @var string The DN of the group we deal with in this request |
||
25 | */ |
||
26 | private $group_dn; |
||
27 | |||
28 | /** |
||
29 | * @var string The DN of the user we deal with in this request |
||
30 | */ |
||
31 | private $user_dn; |
||
32 | |||
33 | /** |
||
34 | * @var string The ou value of the group |
||
35 | */ |
||
36 | private $ou; |
||
37 | |||
38 | /** |
||
39 | * @var string The uid value of the user |
||
40 | */ |
||
41 | private $uid; |
||
42 | |||
43 | /** |
||
44 | * {@inheritdoc} |
||
45 | */ |
||
46 | public function connect(Application $app) |
||
86 | |||
87 | /** |
||
88 | * Ensure you're not editing your own position in the group, such as demoting yourself to regular user. |
||
89 | * To be used as before middleware. |
||
90 | * |
||
91 | * @param Request $request |
||
92 | * @return null|RefererRedirectResponse |
||
93 | */ |
||
94 | public function ensureNotOwn(Request $request) |
||
106 | |||
107 | /** |
||
108 | * Ensure you are indeed an admin of the group you are about to modify. |
||
109 | * To be used as before middleware. |
||
110 | * |
||
111 | * @param Request $request |
||
112 | * @return null|RefererRedirectResponse |
||
113 | */ |
||
114 | public function ensureGroupAdmin(Request $request) |
||
126 | |||
127 | /** |
||
128 | * Adds the current user to the current group as owner. |
||
129 | * |
||
130 | * @param Request $request |
||
131 | * @return RefererRedirectResponse |
||
132 | */ |
||
133 | View Code Duplication | public function ownerAdd(Request $request) |
|
140 | |||
141 | /** |
||
142 | * Removes the current user from the current group as owner. |
||
143 | * |
||
144 | * @param Request $request |
||
145 | * @return RefererRedirectResponse |
||
146 | */ |
||
147 | View Code Duplication | public function ownerRemove(Request $request) |
|
154 | |||
155 | /** |
||
156 | * Sets the full DNs from the given Request object on the controller instance. |
||
157 | * |
||
158 | * @param Request $request |
||
159 | * @return array Full DNs for the owner and group |
||
160 | */ |
||
161 | public function setDNs(Request $request) |
||
182 | |||
183 | } |
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.