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 | * @param string $appName |
||
| 45 | * @param IRequest $request an instance of the request |
||
| 46 | * @param IUserSession $userSession |
||
| 47 | * @param IConfig $config |
||
| 48 | */ |
||
| 49 | 7 | public function __construct($appName, IRequest $request, IUserSession $userSession, |
|
| 55 | |||
| 56 | |||
| 57 | /** |
||
| 58 | * set a new view |
||
| 59 | * |
||
| 60 | * @param string $view |
||
| 61 | * @return JSONResponse |
||
| 62 | * |
||
| 63 | * @NoAdminRequired |
||
| 64 | */ |
||
| 65 | 5 | View Code Duplication | public function setView($view) { |
|
|
|||
| 66 | 5 | if (!$this->isViewAllowed($view)) { |
|
| 67 | 1 | return new JSONResponse([], Http::STATUS_UNPROCESSABLE_ENTITY); |
|
| 68 | } |
||
| 69 | |||
| 70 | 4 | $userId = $this->userSession->getUser()->getUID(); |
|
| 71 | 4 | $app = $this->appName; |
|
| 72 | |||
| 73 | try { |
||
| 74 | 4 | $this->config->setUserValue( |
|
| 75 | 4 | $userId, |
|
| 76 | 4 | $app, |
|
| 77 | 4 | 'currentView', |
|
| 78 | $view |
||
| 79 | 4 | ); |
|
| 80 | 4 | } catch(\Exception $e) { |
|
| 81 | 1 | return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR); |
|
| 82 | } |
||
| 83 | |||
| 84 | 3 | return new JSONResponse(); |
|
| 85 | } |
||
| 86 | |||
| 87 | |||
| 88 | /** |
||
| 89 | * get a config value |
||
| 90 | * |
||
| 91 | * @return JSONResponse |
||
| 92 | * |
||
| 93 | * @NoAdminRequired |
||
| 94 | */ |
||
| 95 | 2 | View Code Duplication | public function getView() { |
| 96 | 2 | $userId = $this->userSession->getUser()->getUID(); |
|
| 97 | 2 | $app = $this->appName; |
|
| 98 | |||
| 99 | try { |
||
| 100 | 2 | $view = $this->config->getUserValue( |
|
| 101 | 2 | $userId, |
|
| 102 | 2 | $app, |
|
| 103 | 2 | 'currentView', |
|
| 104 | 'month' |
||
| 105 | 2 | ); |
|
| 106 | 2 | } catch(\Exception $e) { |
|
| 107 | 1 | return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR); |
|
| 108 | } |
||
| 109 | |||
| 110 | 1 | return new JSONResponse([ |
|
| 111 | 1 | 'value' => $view, |
|
| 112 | 1 | ]); |
|
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * check if view is allowed |
||
| 117 | * |
||
| 118 | * @param $view |
||
| 119 | * @return bool |
||
| 120 | */ |
||
| 121 | 5 | private function isViewAllowed($view) { |
|
| 130 | |||
| 131 | /** |
||
| 132 | * set config value for showing week numbers |
||
| 133 | * |
||
| 134 | * @param bool $showWeekNr |
||
| 135 | * @return JSONResponse |
||
| 136 | * |
||
| 137 | * @NoAdminRequired |
||
| 138 | */ |
||
| 139 | View Code Duplication | public function setShowWeekNr($showWeekNr) { |
|
| 160 | |||
| 161 | /** |
||
| 162 | * get config value for showing week numbers |
||
| 163 | * |
||
| 164 | * @return JSONResponse |
||
| 165 | * |
||
| 166 | * @NoAdminRequired |
||
| 167 | */ |
||
| 168 | View Code Duplication | public function getShowWeekNr() { |
|
| 187 | } |
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.