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:
Complex classes like SettingsController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use SettingsController, and based on these observations, apply Extract Interface, too.
| 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 | 23 | 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 | 9 | public function getConfig($key) { |
|
| 86 | |||
| 87 | /** |
||
| 88 | * set a configuration item |
||
| 89 | * |
||
| 90 | * @NoAdminRequired |
||
| 91 | * |
||
| 92 | * @param string $key |
||
| 93 | * @param mixed $value |
||
| 94 | * @return JSONResponse |
||
| 95 | */ |
||
| 96 | 14 | public function setConfig($key, $value) { |
|
| 112 | |||
| 113 | |||
| 114 | /** |
||
| 115 | * set a new view |
||
| 116 | * |
||
| 117 | * @param string $view |
||
| 118 | * @return JSONResponse |
||
| 119 | */ |
||
| 120 | 5 | View Code Duplication | private function setView($view) { |
| 138 | |||
| 139 | |||
| 140 | /** |
||
| 141 | * get a config value |
||
| 142 | * |
||
| 143 | * @return JSONResponse |
||
| 144 | */ |
||
| 145 | 2 | View Code Duplication | private function getView() { |
| 161 | |||
| 162 | /** |
||
| 163 | * check if view is allowed |
||
| 164 | * |
||
| 165 | * @param $view |
||
| 166 | * @return bool |
||
| 167 | */ |
||
| 168 | 5 | private function isViewAllowed($view) { |
|
| 177 | |||
| 178 | /** |
||
| 179 | * set if popover shall be skipped |
||
| 180 | * |
||
| 181 | * @param $value |
||
| 182 | * @return JSONResponse |
||
| 183 | */ |
||
| 184 | 4 | View Code Duplication | private function setSkipPopover($value) { |
| 202 | |||
| 203 | /** |
||
| 204 | * get if popover shall be skipped |
||
| 205 | * |
||
| 206 | * @return JSONResponse |
||
| 207 | */ |
||
| 208 | 2 | View Code Duplication | private function getSkipPopover() { |
| 224 | |||
| 225 | /** |
||
| 226 | * check if value for skipPopover is allowed |
||
| 227 | * |
||
| 228 | * @param $value |
||
| 229 | * @return bool |
||
| 230 | */ |
||
| 231 | 4 | private function isSkipPopoverValueAllowed($value) { |
|
| 239 | |||
| 240 | /** |
||
| 241 | * set config value for showing week numbers |
||
| 242 | * |
||
| 243 | * @param $value |
||
| 244 | * @return JSONResponse |
||
| 245 | */ |
||
| 246 | View Code Duplication | private function setShowWeekNr($value) { |
|
| 264 | |||
| 265 | /** |
||
| 266 | * get config value for showing week numbers |
||
| 267 | * |
||
| 268 | * @return JSONResponse |
||
| 269 | */ |
||
| 270 | View Code Duplication | private function getShowWeekNr() { |
|
| 286 | |||
| 287 | /** |
||
| 288 | * check if value for showWeekNr is allowed |
||
| 289 | * |
||
| 290 | * @param $value |
||
| 291 | * @return bool |
||
| 292 | */ |
||
| 293 | private function isShowWeekNrValueAllowed($value) { |
||
| 301 | |||
| 302 | /** |
||
| 303 | * remember that first run routines executed |
||
| 304 | * |
||
| 305 | * @return JSONResponse |
||
| 306 | */ |
||
| 307 | 2 | View Code Duplication | private function setFirstRun() { |
| 321 | |||
| 322 | /** |
||
| 323 | * get stored value for first run |
||
| 324 | * |
||
| 325 | * @return JSONResponse |
||
| 326 | */ |
||
| 327 | 2 | View Code Duplication | private function getFirstRun() { |
| 343 | |||
| 344 | /** |
||
| 345 | * sets display timezone for user |
||
| 346 | * |
||
| 347 | * @param string $value |
||
| 348 | * @return JSONResponse |
||
| 349 | */ |
||
| 350 | 2 | View Code Duplication | private function setTimezone($value) { |
| 364 | |||
| 365 | /** |
||
| 366 | * gets display timezone for user |
||
| 367 | * |
||
| 368 | * @return JSONResponse |
||
| 369 | */ |
||
| 370 | 2 | View Code Duplication | private function getTimezone() { |
| 386 | |||
| 387 | /** |
||
| 388 | * get a configuration item |
||
| 389 | * |
||
| 390 | * @NoAdminRequired |
||
| 391 | * |
||
| 392 | * @param string $state |
||
| 393 | * @return String |
||
| 394 | */ |
||
| 395 | public function setLastChoosenDateState($state) { |
||
| 401 | |||
| 402 | public function getLastChoosenDateState() { |
||
| 409 | |||
| 410 | } |
||
| 411 |
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.