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 |
||
50 | class ThemingController extends Controller { |
||
51 | /** @var Template */ |
||
52 | private $template; |
||
53 | /** @var Util */ |
||
54 | private $util; |
||
55 | /** @var ITimeFactory */ |
||
56 | private $timeFactory; |
||
57 | /** @var IL10N */ |
||
58 | private $l; |
||
59 | /** @var IConfig */ |
||
60 | private $config; |
||
61 | /** @var IRootFolder */ |
||
62 | private $rootFolder; |
||
63 | |||
64 | /** |
||
65 | * ThemingController constructor. |
||
66 | * |
||
67 | * @param string $appName |
||
68 | * @param IRequest $request |
||
69 | * @param IConfig $config |
||
70 | * @param Template $template |
||
71 | * @param Util $util |
||
72 | * @param ITimeFactory $timeFactory |
||
73 | * @param IL10N $l |
||
74 | * @param IRootFolder $rootFolder |
||
75 | */ |
||
76 | View Code Duplication | public function __construct( |
|
95 | |||
96 | /** |
||
97 | * @param string $setting |
||
98 | * @param string $value |
||
99 | * @return DataResponse |
||
100 | * @internal param string $color |
||
101 | */ |
||
102 | public function updateStylesheet($setting, $value) { |
||
158 | |||
159 | /** |
||
160 | * Update the logos and background image |
||
161 | * |
||
162 | * @return DataResponse |
||
163 | */ |
||
164 | public function updateLogo() { |
||
165 | $newLogo = $this->request->getUploadedFile('uploadlogo'); |
||
166 | $newBackgroundLogo = $this->request->getUploadedFile('upload-login-background'); |
||
167 | if (empty($newLogo) && empty($newBackgroundLogo)) { |
||
168 | return new DataResponse( |
||
169 | [ |
||
170 | 'data' => [ |
||
171 | 'message' => $this->l->t('No file uploaded') |
||
172 | ] |
||
173 | ], |
||
174 | Http::STATUS_UNPROCESSABLE_ENTITY); |
||
175 | } |
||
176 | $name = ''; |
||
177 | View Code Duplication | if(!empty($newLogo)) { |
|
178 | $target = $this->rootFolder->newFile('themedinstancelogo'); |
||
179 | stream_copy_to_stream(fopen($newLogo['tmp_name'], 'r'), $target->fopen('w')); |
||
180 | $this->template->set('logoMime', $newLogo['type']); |
||
181 | $name = $newLogo['name']; |
||
182 | } |
||
183 | View Code Duplication | if(!empty($newBackgroundLogo)) { |
|
184 | $target = $this->rootFolder->newFile('themedbackgroundlogo'); |
||
185 | stream_copy_to_stream(fopen($newBackgroundLogo['tmp_name'], 'r'), $target->fopen('w')); |
||
186 | $this->template->set('backgroundMime', $newBackgroundLogo['type']); |
||
187 | $name = $newBackgroundLogo['name']; |
||
188 | } |
||
189 | |||
190 | return new DataResponse( |
||
191 | [ |
||
192 | 'data' => |
||
193 | [ |
||
194 | 'name' => $name, |
||
195 | 'message' => $this->l->t('Saved') |
||
196 | ], |
||
197 | 'status' => 'success' |
||
198 | ] |
||
199 | ); |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * Revert setting to default value |
||
204 | * |
||
205 | * @param string $setting setting which should be reverted |
||
206 | * @return DataResponse |
||
207 | */ |
||
208 | public function undo($setting) { |
||
221 | |||
222 | /** |
||
223 | * @PublicPage |
||
224 | * @NoCSRFRequired |
||
225 | * |
||
226 | * @return StreamResponse|DataResponse |
||
227 | */ |
||
228 | View Code Duplication | public function getLogo() { |
|
241 | |||
242 | /** |
||
243 | * @PublicPage |
||
244 | * @NoCSRFRequired |
||
245 | * |
||
246 | * @return StreamResponse|DataResponse |
||
247 | */ |
||
248 | View Code Duplication | public function getLoginBackground() { |
|
261 | |||
262 | /** |
||
263 | * @NoCSRFRequired |
||
264 | * @PublicPage |
||
265 | * |
||
266 | * @return DataDownloadResponse |
||
267 | */ |
||
268 | public function getStylesheet() { |
||
269 | $cacheBusterValue = $this->config->getAppValue('theming', 'cachebuster', '0'); |
||
270 | $responseCss = ''; |
||
271 | $color = $this->config->getAppValue($this->appName, 'color'); |
||
272 | $elementColor = $this->util->elementColor($color); |
||
273 | if($color !== '') { |
||
274 | $responseCss .= sprintf( |
||
275 | '#body-user #header,#body-settings #header,#body-public #header,#body-login,.searchbox input[type="search"]:focus,.searchbox input[type="search"]:active,.searchbox input[type="search"]:valid {background-color: %s}' . "\n", |
||
276 | $color |
||
277 | ); |
||
278 | $responseCss .= sprintf('input[type="checkbox"].checkbox:checked:enabled:not(.checkbox--white) + label:before {' . |
||
279 | 'background-image:url(\'%s/core/img/actions/checkmark-white.svg\');' . |
||
280 | 'background-color: %s; background-position: center center; background-size:contain;' . |
||
281 | 'width:12px; height:12px; padding:0; margin:2px 6px 6px 2px; border-radius:1px;' . |
||
282 | "}\n", |
||
283 | \OC::$WEBROOT, |
||
284 | $elementColor |
||
285 | ); |
||
286 | $responseCss .= 'input[type="radio"].radio:checked:not(.radio--white):not(:disabled) + label:before {' . |
||
287 | 'background-image: url(\'data:image/svg+xml;base64,'.$this->util->generateRadioButton($elementColor).'\');' . |
||
288 | "}\n"; |
||
289 | $responseCss .= ' |
||
290 | #firstrunwizard .firstrunwizard-header { |
||
291 | background-color: ' . $color . '; |
||
292 | } |
||
293 | #firstrunwizard p a { |
||
294 | color: ' . $color . '; |
||
295 | } |
||
296 | '; |
||
297 | $responseCss .= sprintf('.nc-theming-main-background {background-color: %s}' . "\n", $color); |
||
298 | $responseCss .= sprintf('.nc-theming-main-text {color: %s}' . "\n", $color); |
||
299 | |||
300 | } |
||
301 | $logo = $this->config->getAppValue($this->appName, 'logoMime'); |
||
302 | if($logo !== '') { |
||
303 | $responseCss .= sprintf( |
||
304 | '#header .logo {' . |
||
305 | 'background-image: url(\'./logo?v='.$cacheBusterValue.'\');' . |
||
306 | 'background-size: contain;' . |
||
307 | '}' . "\n" . |
||
308 | '#header .logo-icon {' . |
||
309 | 'background-image: url(\'./logo?v='.$cacheBusterValue.'\');' . |
||
310 | 'background-size: contain;' . |
||
311 | '}' . "\n" . |
||
312 | '#firstrunwizard .firstrunwizard-header .logo {' . |
||
313 | 'background-image: url(\'./logo?v='.$cacheBusterValue.'\');' . |
||
314 | 'background-size: contain;' . |
||
315 | '}' . "\n" |
||
316 | ); |
||
317 | } |
||
318 | $backgroundLogo = $this->config->getAppValue($this->appName, 'backgroundMime'); |
||
319 | if($backgroundLogo !== '') { |
||
320 | $responseCss .= '#body-login {background-image: url(\'./loginbackground?v='.$cacheBusterValue.'\');}' . "\n"; |
||
321 | $responseCss .= '#firstrunwizard .firstrunwizard-header {' . |
||
322 | 'background-image: url(\'./loginbackground?v='.$cacheBusterValue.'\');' . |
||
323 | '}' . "\n"; |
||
324 | } |
||
325 | if($this->util->invertTextColor($color)) { |
||
326 | $responseCss .= '#header .header-appname, #expandDisplayName { color: #000000; }' . "\n"; |
||
327 | $responseCss .= '#header .icon-caret { background-image: url(\'' . \OC::$WEBROOT . '/core/img/actions/caret-dark.svg\'); }' . "\n"; |
||
328 | $responseCss .= '.searchbox input[type="search"] { background: transparent url(\'' . \OC::$WEBROOT . '/core/img/actions/search.svg\') no-repeat 6px center; color: #000; }' . "\n"; |
||
329 | $responseCss .= '.searchbox input[type="search"]:focus,.searchbox input[type="search"]:active,.searchbox input[type="search"]:valid { color: #000; border: 1px solid rgba(0, 0, 0, .5); }' . "\n"; |
||
330 | $responseCss .= '.nc-theming-contrast {color: #000000}' . "\n"; |
||
331 | } else { |
||
332 | $responseCss .= '.nc-theming-contrast {color: #ffffff}' . "\n"; |
||
333 | } |
||
334 | |||
335 | $response = new DataDownloadResponse($responseCss, 'style', 'text/css'); |
||
336 | $response->addHeader('Expires', date(\DateTime::RFC2822, $this->timeFactory->getTime())); |
||
337 | $response->cacheFor(3600); |
||
338 | return $response; |
||
339 | } |
||
340 | /** |
||
341 | * @NoCSRFRequired |
||
342 | * @PublicPage |
||
343 | * |
||
344 | * @return DataDownloadResponse |
||
345 | */ |
||
346 | public function getJavascript() { |
||
362 | } |
||
363 |
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.