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 |
||
| 64 | class ThemingController extends Controller { |
||
| 65 | /** @var ThemingDefaults */ |
||
| 66 | private $themingDefaults; |
||
| 67 | /** @var Util */ |
||
| 68 | private $util; |
||
| 69 | /** @var ITimeFactory */ |
||
| 70 | private $timeFactory; |
||
| 71 | /** @var IL10N */ |
||
| 72 | private $l10n; |
||
| 73 | /** @var IConfig */ |
||
| 74 | private $config; |
||
| 75 | /** @var ITempManager */ |
||
| 76 | private $tempManager; |
||
| 77 | /** @var IAppData */ |
||
| 78 | private $appData; |
||
| 79 | /** @var SCSSCacher */ |
||
| 80 | private $scssCacher; |
||
| 81 | /** @var IURLGenerator */ |
||
| 82 | private $urlGenerator; |
||
| 83 | /** @var IAppManager */ |
||
| 84 | private $appManager; |
||
| 85 | /** @var ImageManager */ |
||
| 86 | private $imageManager; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * ThemingController constructor. |
||
| 90 | * |
||
| 91 | * @param string $appName |
||
| 92 | * @param IRequest $request |
||
| 93 | * @param IConfig $config |
||
| 94 | * @param ThemingDefaults $themingDefaults |
||
| 95 | * @param Util $util |
||
| 96 | * @param ITimeFactory $timeFactory |
||
| 97 | * @param IL10N $l |
||
| 98 | * @param ITempManager $tempManager |
||
| 99 | * @param IAppData $appData |
||
| 100 | * @param SCSSCacher $scssCacher |
||
| 101 | * @param IURLGenerator $urlGenerator |
||
| 102 | * @param IAppManager $appManager |
||
| 103 | * @param ImageManager $imageManager |
||
| 104 | */ |
||
| 105 | public function __construct( |
||
| 106 | $appName, |
||
| 107 | IRequest $request, |
||
| 108 | IConfig $config, |
||
| 109 | ThemingDefaults $themingDefaults, |
||
| 110 | Util $util, |
||
| 111 | ITimeFactory $timeFactory, |
||
| 112 | IL10N $l, |
||
| 113 | ITempManager $tempManager, |
||
| 114 | IAppData $appData, |
||
| 115 | SCSSCacher $scssCacher, |
||
| 116 | IURLGenerator $urlGenerator, |
||
| 117 | IAppManager $appManager, |
||
| 118 | ImageManager $imageManager |
||
| 119 | ) { |
||
| 120 | parent::__construct($appName, $request); |
||
| 121 | |||
| 122 | $this->themingDefaults = $themingDefaults; |
||
| 123 | $this->util = $util; |
||
| 124 | $this->timeFactory = $timeFactory; |
||
| 125 | $this->l10n = $l; |
||
| 126 | $this->config = $config; |
||
| 127 | $this->tempManager = $tempManager; |
||
| 128 | $this->appData = $appData; |
||
| 129 | $this->scssCacher = $scssCacher; |
||
| 130 | $this->urlGenerator = $urlGenerator; |
||
| 131 | $this->appManager = $appManager; |
||
| 132 | $this->imageManager = $imageManager; |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @param string $setting |
||
| 137 | * @param string $value |
||
| 138 | * @return DataResponse |
||
| 139 | * @throws NotPermittedException |
||
| 140 | */ |
||
| 141 | public function updateStylesheet($setting, $value) { |
||
| 142 | $value = trim($value); |
||
| 143 | switch ($setting) { |
||
| 144 | View Code Duplication | case 'name': |
|
| 145 | if (strlen($value) > 250) { |
||
| 146 | return new DataResponse([ |
||
| 147 | 'data' => [ |
||
| 148 | 'message' => $this->l10n->t('The given name is too long'), |
||
| 149 | ], |
||
| 150 | 'status' => 'error' |
||
| 151 | ]); |
||
| 152 | } |
||
| 153 | break; |
||
| 154 | View Code Duplication | case 'url': |
|
| 155 | if (strlen($value) > 500) { |
||
| 156 | return new DataResponse([ |
||
| 157 | 'data' => [ |
||
| 158 | 'message' => $this->l10n->t('The given web address is too long'), |
||
| 159 | ], |
||
| 160 | 'status' => 'error' |
||
| 161 | ]); |
||
| 162 | } |
||
| 163 | break; |
||
| 164 | View Code Duplication | case 'slogan': |
|
| 165 | if (strlen($value) > 500) { |
||
| 166 | return new DataResponse([ |
||
| 167 | 'data' => [ |
||
| 168 | 'message' => $this->l10n->t('The given slogan is too long'), |
||
| 169 | ], |
||
| 170 | 'status' => 'error' |
||
| 171 | ]); |
||
| 172 | } |
||
| 173 | break; |
||
| 174 | View Code Duplication | case 'color': |
|
| 175 | if (!preg_match('/^\#([0-9a-f]{3}|[0-9a-f]{6})$/i', $value)) { |
||
| 176 | return new DataResponse([ |
||
| 177 | 'data' => [ |
||
| 178 | 'message' => $this->l10n->t('The given color is invalid'), |
||
| 179 | ], |
||
| 180 | 'status' => 'error' |
||
| 181 | ]); |
||
| 182 | } |
||
| 183 | break; |
||
| 184 | } |
||
| 185 | |||
| 186 | $this->themingDefaults->set($setting, $value); |
||
| 187 | |||
| 188 | // reprocess server scss for preview |
||
| 189 | $cssCached = $this->scssCacher->process(\OC::$SERVERROOT, 'core/css/server.scss', 'core'); |
||
|
|
|||
| 190 | |||
| 191 | return new DataResponse( |
||
| 192 | [ |
||
| 193 | 'data' => |
||
| 194 | [ |
||
| 195 | 'message' => $this->l10n->t('Saved'), |
||
| 196 | 'serverCssUrl' => $this->urlGenerator->linkTo('', $this->scssCacher->getCachedSCSS('core', '/core/css/server.scss')) |
||
| 197 | ], |
||
| 198 | 'status' => 'success' |
||
| 199 | ] |
||
| 200 | ); |
||
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @return DataResponse |
||
| 205 | * @throws NotPermittedException |
||
| 206 | */ |
||
| 207 | public function uploadImage(): DataResponse { |
||
| 208 | // logo / background |
||
| 209 | // new: favicon logo-header |
||
| 210 | // |
||
| 211 | $key = $this->request->getParam('key'); |
||
| 212 | $image = $this->request->getUploadedFile('image'); |
||
| 213 | $error = null; |
||
| 214 | $phpFileUploadErrors = [ |
||
| 215 | UPLOAD_ERR_OK => $this->l10n->t('The file was uploaded'), |
||
| 216 | UPLOAD_ERR_INI_SIZE => $this->l10n->t('The uploaded file exceeds the upload_max_filesize directive in php.ini'), |
||
| 217 | UPLOAD_ERR_FORM_SIZE => $this->l10n->t('The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form'), |
||
| 218 | UPLOAD_ERR_PARTIAL => $this->l10n->t('The file was only partially uploaded'), |
||
| 219 | UPLOAD_ERR_NO_FILE => $this->l10n->t('No file was uploaded'), |
||
| 220 | UPLOAD_ERR_NO_TMP_DIR => $this->l10n->t('Missing a temporary folder'), |
||
| 221 | UPLOAD_ERR_CANT_WRITE => $this->l10n->t('Could not write file to disk'), |
||
| 222 | UPLOAD_ERR_EXTENSION => $this->l10n->t('A PHP extension stopped the file upload'), |
||
| 223 | ]; |
||
| 224 | if (empty($image)) { |
||
| 225 | $error = $this->l10n->t('No file uploaded'); |
||
| 226 | } |
||
| 227 | if (!empty($image) && array_key_exists('error', $image) && $image['error'] !== UPLOAD_ERR_OK) { |
||
| 228 | $error = $phpFileUploadErrors[$image['error']]; |
||
| 229 | } |
||
| 230 | |||
| 231 | if ($error !== null) { |
||
| 232 | return new DataResponse( |
||
| 233 | [ |
||
| 234 | 'data' => [ |
||
| 235 | 'message' => $error |
||
| 236 | ], |
||
| 237 | 'status' => 'failure', |
||
| 238 | ], |
||
| 239 | Http::STATUS_UNPROCESSABLE_ENTITY |
||
| 240 | ); |
||
| 241 | } |
||
| 242 | |||
| 243 | $name = ''; |
||
| 244 | try { |
||
| 245 | $folder = $this->appData->getFolder('images'); |
||
| 246 | } catch (NotFoundException $e) { |
||
| 247 | $folder = $this->appData->newFolder('images'); |
||
| 248 | } |
||
| 249 | |||
| 250 | $target = $folder->newFile($key); |
||
| 251 | $supportedFormats = ['image/jpeg', 'image/png', 'image/gif', 'image/svg+xml', 'image/svg']; |
||
| 252 | $detectedMimeType = mime_content_type($image['tmp_name']); |
||
| 253 | if (!in_array($image['type'], $supportedFormats) || !in_array($detectedMimeType, $supportedFormats)) { |
||
| 254 | return new DataResponse( |
||
| 255 | [ |
||
| 256 | 'data' => [ |
||
| 257 | 'message' => $this->l10n->t('Unsupported image type'), |
||
| 258 | ], |
||
| 259 | 'status' => 'failure', |
||
| 260 | ], |
||
| 261 | Http::STATUS_UNPROCESSABLE_ENTITY |
||
| 262 | ); |
||
| 263 | } |
||
| 264 | |||
| 265 | $resizeKeys = ['background']; |
||
| 266 | if (in_array($key, $resizeKeys, true)) { |
||
| 267 | // Optimize the image since some people may upload images that will be |
||
| 268 | // either to big or are not progressive rendering. |
||
| 269 | $newImage = @imagecreatefromstring(file_get_contents($image['tmp_name'], 'r')); |
||
| 270 | |||
| 271 | $tmpFile = $this->tempManager->getTemporaryFile(); |
||
| 272 | $newWidth = imagesx($newImage) < 4096 ? imagesx($newImage) : 4096; |
||
| 273 | $newHeight = imagesy($newImage) / (imagesx($newImage) / $newWidth); |
||
| 274 | $outputImage = imagescale($newImage, $newWidth, $newHeight); |
||
| 275 | |||
| 276 | imageinterlace($outputImage, 1); |
||
| 277 | imagejpeg($outputImage, $tmpFile, 75); |
||
| 278 | imagedestroy($outputImage); |
||
| 279 | |||
| 280 | $target->putContent(file_get_contents($tmpFile, 'r')); |
||
| 281 | } else { |
||
| 282 | $target->putContent(file_get_contents($image['tmp_name'], 'r')); |
||
| 283 | } |
||
| 284 | $name = $image['name']; |
||
| 285 | |||
| 286 | $this->themingDefaults->set($key.'Mime', $image['type']); |
||
| 287 | |||
| 288 | $cssCached = $this->scssCacher->process(\OC::$SERVERROOT, 'core/css/server.scss', 'core'); |
||
| 289 | |||
| 290 | return new DataResponse( |
||
| 291 | [ |
||
| 292 | 'data' => |
||
| 293 | [ |
||
| 294 | 'name' => $name, |
||
| 295 | 'url' => $this->imageManager->getImageUrl($key), |
||
| 296 | 'message' => $this->l10n->t('Saved'), |
||
| 297 | 'serverCssUrl' => $this->urlGenerator->linkTo('', $this->scssCacher->getCachedSCSS('core', '/core/css/server.scss')) |
||
| 298 | ], |
||
| 299 | 'status' => 'success' |
||
| 300 | ] |
||
| 301 | ); |
||
| 302 | } |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Revert setting to default value |
||
| 306 | * |
||
| 307 | * @param string $setting setting which should be reverted |
||
| 308 | * @return DataResponse |
||
| 309 | * @throws NotPermittedException |
||
| 310 | */ |
||
| 311 | public function undo(string $setting): DataResponse { |
||
| 312 | $value = $this->themingDefaults->undo($setting); |
||
| 313 | // reprocess server scss for preview |
||
| 314 | $cssCached = $this->scssCacher->process(\OC::$SERVERROOT, 'core/css/server.scss', 'core'); |
||
| 315 | |||
| 316 | if (strpos($setting, 'Mime') !== -1) { |
||
| 317 | $imageKey = str_replace('Mime', '', $setting); |
||
| 318 | $this->imageManager->delete($imageKey); |
||
| 319 | } |
||
| 320 | |||
| 321 | return new DataResponse( |
||
| 322 | [ |
||
| 323 | 'data' => |
||
| 324 | [ |
||
| 325 | 'value' => $value, |
||
| 326 | 'message' => $this->l10n->t('Saved'), |
||
| 327 | 'serverCssUrl' => $this->urlGenerator->linkTo('', $this->scssCacher->getCachedSCSS('core', '/core/css/server.scss')) |
||
| 328 | ], |
||
| 329 | 'status' => 'success' |
||
| 330 | ] |
||
| 331 | ); |
||
| 332 | } |
||
| 333 | |||
| 334 | /** |
||
| 335 | * @PublicPage |
||
| 336 | * @NoCSRFRequired |
||
| 337 | * |
||
| 338 | * @param string $key |
||
| 339 | * @return FileDisplayResponse|NotFoundResponse |
||
| 340 | * @throws \Exception |
||
| 341 | */ |
||
| 342 | public function getImage(string $key) { |
||
| 343 | try { |
||
| 344 | $file = $this->imageManager->getImage($key); |
||
| 345 | } catch (NotFoundException $e) { |
||
| 346 | return new NotFoundResponse(); |
||
| 347 | } |
||
| 348 | |||
| 349 | $response = new FileDisplayResponse($file); |
||
| 350 | $response->cacheFor(3600); |
||
| 351 | $expires = new \DateTime(); |
||
| 352 | $expires->setTimestamp($this->timeFactory->getTime()); |
||
| 353 | $expires->add(new \DateInterval('PT24H')); |
||
| 354 | $response->addHeader('Expires', $expires->format(\DateTime::RFC2822)); |
||
| 355 | $response->addHeader('Pragma', 'cache'); |
||
| 356 | $response->addHeader('Content-Type', $this->config->getAppValue($this->appName, $key . 'Mime', '')); |
||
| 357 | $response->addHeader('Content-Disposition', 'attachment; filename="' . $key . '"'); |
||
| 358 | return $response; |
||
| 359 | } |
||
| 360 | |||
| 361 | /** |
||
| 362 | * @NoCSRFRequired |
||
| 363 | * @PublicPage |
||
| 364 | * |
||
| 365 | * @return FileDisplayResponse|NotFoundResponse |
||
| 366 | * @throws NotPermittedException |
||
| 367 | * @throws \Exception |
||
| 368 | * @throws \OCP\App\AppPathNotFoundException |
||
| 369 | */ |
||
| 370 | public function getStylesheet() { |
||
| 371 | $appPath = $this->appManager->getAppPath('theming'); |
||
| 372 | |||
| 373 | /* SCSSCacher is required here |
||
| 374 | * We cannot rely on automatic caching done by \OC_Util::addStyle, |
||
| 375 | * since we need to add the cacheBuster value to the url |
||
| 376 | */ |
||
| 377 | $cssCached = $this->scssCacher->process($appPath, 'css/theming.scss', 'theming'); |
||
| 378 | if(!$cssCached) { |
||
| 379 | return new NotFoundResponse(); |
||
| 380 | } |
||
| 381 | |||
| 382 | try { |
||
| 383 | $cssFile = $this->scssCacher->getCachedCSS('theming', 'theming.css'); |
||
| 384 | $response = new FileDisplayResponse($cssFile, Http::STATUS_OK, ['Content-Type' => 'text/css']); |
||
| 385 | $response->cacheFor(86400); |
||
| 386 | $expires = new \DateTime(); |
||
| 387 | $expires->setTimestamp($this->timeFactory->getTime()); |
||
| 388 | $expires->add(new \DateInterval('PT24H')); |
||
| 389 | $response->addHeader('Expires', $expires->format(\DateTime::RFC1123)); |
||
| 390 | $response->addHeader('Pragma', 'cache'); |
||
| 391 | return $response; |
||
| 392 | } catch (NotFoundException $e) { |
||
| 393 | return new NotFoundResponse(); |
||
| 394 | } |
||
| 395 | } |
||
| 396 | |||
| 397 | /** |
||
| 398 | * @NoCSRFRequired |
||
| 399 | * @PublicPage |
||
| 400 | * |
||
| 401 | * @return DataDownloadResponse |
||
| 402 | */ |
||
| 403 | public function getJavascript() { |
||
| 421 | |||
| 422 | /** |
||
| 423 | * @NoCSRFRequired |
||
| 424 | * @PublicPage |
||
| 425 | * |
||
| 426 | * @return Http\JSONResponse |
||
| 427 | */ |
||
| 428 | public function getManifest($app) { |
||
| 456 | } |
||
| 457 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.