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 |
||
31 | class SettingsController extends Controller { |
||
32 | |||
33 | /** |
||
34 | * @var IConfig |
||
35 | */ |
||
36 | private $config; |
||
37 | |||
38 | /** |
||
39 | * @var IUserSession |
||
40 | */ |
||
41 | private $userSession; |
||
42 | |||
43 | /** |
||
44 | * @var string |
||
45 | */ |
||
46 | private $userId; |
||
47 | |||
48 | /** |
||
49 | * @param string $appName |
||
50 | * @param IRequest $request an instance of the request |
||
51 | * @param IUserSession $userSession |
||
52 | * @param IConfig $config |
||
53 | */ |
||
54 | 19 | public function __construct($appName, IRequest $request, IUserSession $userSession, |
|
61 | |||
62 | /** |
||
63 | * get a configuration item |
||
64 | * |
||
65 | * @NoAdminRequired |
||
66 | * |
||
67 | * @param string $key |
||
68 | * @return JSONResponse |
||
69 | */ |
||
70 | 7 | public function getConfig($key) { |
|
84 | |||
85 | /** |
||
86 | * set a configuration item |
||
87 | * |
||
88 | * @NoAdminRequired |
||
89 | * |
||
90 | * @param string $key |
||
91 | * @param mixed $value |
||
92 | * @return JSONResponse |
||
93 | */ |
||
94 | 12 | public function setConfig($key, $value) { |
|
108 | |||
109 | |||
110 | /** |
||
111 | * set a new view |
||
112 | * |
||
113 | * @param string $view |
||
114 | * @return JSONResponse |
||
115 | */ |
||
116 | 5 | View Code Duplication | private function setView($view) { |
134 | |||
135 | |||
136 | /** |
||
137 | * get a config value |
||
138 | * |
||
139 | * @return JSONResponse |
||
140 | */ |
||
141 | 2 | View Code Duplication | private function getView() { |
157 | |||
158 | /** |
||
159 | * check if view is allowed |
||
160 | * |
||
161 | * @param $view |
||
162 | * @return bool |
||
163 | */ |
||
164 | 5 | private function isViewAllowed($view) { |
|
173 | |||
174 | /** |
||
175 | * set if popover shall be skipped |
||
176 | * |
||
177 | * @param $value |
||
178 | * @return JSONResponse |
||
179 | */ |
||
180 | 4 | View Code Duplication | private function setSkipPopover($value) { |
198 | |||
199 | /** |
||
200 | * get if popover shall be skipped |
||
201 | * |
||
202 | * @return JSONResponse |
||
203 | */ |
||
204 | 2 | View Code Duplication | private function getSkipPopover() { |
220 | |||
221 | /** |
||
222 | * check if value for skipPopover is allowed |
||
223 | * |
||
224 | * @param $value |
||
225 | * @return bool |
||
226 | */ |
||
227 | 4 | private function isSkipPopoverValueAllowed($value) { |
|
235 | |||
236 | /** |
||
237 | * set config value for showing week numbers |
||
238 | * |
||
239 | * @param $value |
||
240 | * @return JSONResponse |
||
241 | */ |
||
242 | View Code Duplication | private function setShowWeekNr($value) { |
|
260 | |||
261 | /** |
||
262 | * get config value for showing week numbers |
||
263 | * |
||
264 | * @return JSONResponse |
||
265 | */ |
||
266 | View Code Duplication | private function getShowWeekNr() { |
|
282 | |||
283 | /** |
||
284 | * check if value for showWeekNr is allowed |
||
285 | * |
||
286 | * @param $value |
||
287 | * @return bool |
||
288 | */ |
||
289 | private function isShowWeekNrValueAllowed($value) { |
||
297 | |||
298 | /** |
||
299 | * remember that first run routines executed |
||
300 | * |
||
301 | * @return JSONResponse |
||
302 | */ |
||
303 | 2 | View Code Duplication | private function setFirstRun() { |
317 | |||
318 | /** |
||
319 | * get stored value for first run |
||
320 | * |
||
321 | * @return JSONResponse |
||
322 | */ |
||
323 | 2 | View Code Duplication | private function getFirstRun() { |
339 | } |
||
340 |
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.