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 | 15 | public function __construct($appName, IRequest $request, IUserSession $userSession, |
|
55 | IConfig $config) { |
||
56 | 15 | parent::__construct($appName, $request); |
|
57 | 15 | $this->config = $config; |
|
58 | 15 | $this->userSession = $userSession; |
|
59 | 15 | $this->userId = $userSession->getUser()->getUID(); |
|
60 | 15 | } |
|
61 | |||
62 | /** |
||
63 | * get a configuration item |
||
64 | * |
||
65 | * @NoAdminRequired |
||
66 | * |
||
67 | * @param string $key |
||
68 | * @return JSONResponse |
||
69 | */ |
||
70 | 5 | public function getConfig($key) { |
|
71 | switch ($key) { |
||
72 | 5 | case 'view': |
|
73 | 2 | return $this->getView(); |
|
74 | 3 | case 'skipPopover': |
|
75 | 2 | return $this->getSkipPopover(); |
|
76 | 1 | case 'showWeekNr': |
|
77 | return $this->getShowWeekNr(); |
||
78 | default: |
||
79 | 1 | return new JSONResponse([], Http::STATUS_BAD_REQUEST); |
|
80 | } |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * set a configuration item |
||
85 | * |
||
86 | * @NoAdminRequired |
||
87 | * |
||
88 | * @param string $key |
||
89 | * @param mixed $value |
||
90 | * @return JSONResponse |
||
91 | */ |
||
92 | 10 | public function setConfig($key, $value) { |
|
93 | switch ($key) { |
||
94 | 10 | case 'view': |
|
95 | 5 | return $this->setView($value); |
|
96 | 5 | case 'skipPopover': |
|
97 | 4 | return $this->setSkipPopover($value); |
|
98 | 1 | case 'showWeekNr': |
|
99 | return $this->setShowWeekNr($value); |
||
100 | default: |
||
101 | 1 | return new JSONResponse([], Http::STATUS_BAD_REQUEST); |
|
102 | } |
||
103 | } |
||
104 | |||
105 | |||
106 | /** |
||
107 | * set a new view |
||
108 | * |
||
109 | * @param string $view |
||
110 | * @return JSONResponse |
||
111 | */ |
||
112 | 5 | View Code Duplication | private function setView($view) { |
|
|||
113 | 5 | if (!$this->isViewAllowed($view)) { |
|
114 | 1 | return new JSONResponse([], Http::STATUS_UNPROCESSABLE_ENTITY); |
|
115 | } |
||
116 | |||
117 | try { |
||
118 | 4 | $this->config->setUserValue( |
|
119 | 4 | $this->userId, |
|
120 | 4 | $this->appName, |
|
121 | 4 | 'currentView', |
|
122 | $view |
||
123 | ); |
||
124 | 1 | } catch(\Exception $e) { |
|
125 | 1 | return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR); |
|
126 | } |
||
127 | |||
128 | 3 | return new JSONResponse(); |
|
129 | } |
||
130 | |||
131 | |||
132 | /** |
||
133 | * get a config value |
||
134 | * |
||
135 | * @return JSONResponse |
||
136 | */ |
||
137 | 2 | View Code Duplication | private function getView() { |
138 | try { |
||
139 | 2 | $view = $this->config->getUserValue( |
|
140 | 2 | $this->userId, |
|
141 | 2 | $this->appName, |
|
142 | 2 | 'currentView', |
|
143 | 2 | 'month' |
|
144 | ); |
||
145 | 1 | } catch(\Exception $e) { |
|
146 | 1 | return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR); |
|
147 | } |
||
148 | |||
149 | 1 | return new JSONResponse([ |
|
150 | 1 | 'value' => $view, |
|
151 | ]); |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * check if view is allowed |
||
156 | * |
||
157 | * @param $view |
||
158 | * @return bool |
||
159 | */ |
||
160 | 5 | private function isViewAllowed($view) { |
|
161 | $allowedViews = [ |
||
162 | 5 | 'agendaDay', |
|
163 | 'agendaWeek', |
||
164 | 'month', |
||
165 | ]; |
||
166 | |||
167 | 5 | return in_array($view, $allowedViews); |
|
168 | } |
||
169 | |||
170 | /** |
||
171 | * set if popover shall be skipped |
||
172 | * |
||
173 | * @param $value |
||
174 | * @return JSONResponse |
||
175 | */ |
||
176 | 4 | View Code Duplication | private function setSkipPopover($value) { |
177 | 4 | if (!$this->isSkipPopoverValueAllowed($value)) { |
|
178 | 1 | return new JSONResponse([], Http::STATUS_UNPROCESSABLE_ENTITY); |
|
179 | } |
||
180 | |||
181 | try { |
||
182 | 3 | $this->config->setUserValue( |
|
183 | 3 | $this->userId, |
|
184 | 3 | $this->appName, |
|
185 | 3 | 'skipPopover', |
|
186 | $value |
||
187 | ); |
||
188 | 1 | } catch(\Exception $e) { |
|
189 | 1 | return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR); |
|
190 | } |
||
191 | |||
192 | 2 | return new JSONResponse(); |
|
193 | } |
||
194 | |||
195 | /** |
||
196 | * get if popover shall be skipped |
||
197 | * |
||
198 | * @return JSONResponse |
||
199 | */ |
||
200 | 2 | View Code Duplication | private function getSkipPopover() { |
201 | try { |
||
202 | 2 | $view = $this->config->getUserValue( |
|
203 | 2 | $this->userId, |
|
204 | 2 | $this->appName, |
|
205 | 2 | 'skipPopover', |
|
206 | 2 | 'no' |
|
207 | ); |
||
208 | 1 | } catch(\Exception $e) { |
|
209 | 1 | return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR); |
|
210 | } |
||
211 | |||
212 | 1 | return new JSONResponse([ |
|
213 | 1 | 'value' => $view, |
|
214 | ]); |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * check if value for skipPopover is allowed |
||
219 | * |
||
220 | * @param $value |
||
221 | * @return bool |
||
222 | */ |
||
223 | 4 | private function isSkipPopoverValueAllowed($value) { |
|
224 | $allowedValues = [ |
||
225 | 4 | 'yes', |
|
226 | 'no' |
||
227 | ]; |
||
228 | |||
229 | 4 | return in_array($value, $allowedValues); |
|
230 | } |
||
231 | |||
232 | /** |
||
233 | * set config value for showing week numbers |
||
234 | * |
||
235 | * @param $value |
||
236 | * @return JSONResponse |
||
237 | */ |
||
238 | private function setShowWeekNr($value) { |
||
256 | |||
257 | /** |
||
258 | * get config value for showing week numbers |
||
259 | * |
||
260 | * @return JSONResponse |
||
261 | */ |
||
262 | View Code Duplication | private function getShowWeekNr() { |
|
278 | |||
279 | /** |
||
280 | * check if value for showWeekNr is allowed |
||
281 | * |
||
282 | * @param $value |
||
283 | * @return bool |
||
284 | */ |
||
285 | private function isShowWeekNrValueAllowed($value) { |
||
293 | } |
||
294 |
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.