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 |
||
| 51 | class AvatarController extends Controller { |
||
| 52 | |||
| 53 | /** @var IAvatarManager */ |
||
| 54 | protected $avatarManager; |
||
| 55 | |||
| 56 | /** @var ICache */ |
||
| 57 | protected $cache; |
||
| 58 | |||
| 59 | /** @var IL10N */ |
||
| 60 | protected $l; |
||
| 61 | |||
| 62 | /** @var IUserManager */ |
||
| 63 | protected $userManager; |
||
| 64 | |||
| 65 | /** @var IUserSession */ |
||
| 66 | protected $userSession; |
||
| 67 | |||
| 68 | /** @var IRootFolder */ |
||
| 69 | protected $rootFolder; |
||
| 70 | |||
| 71 | /** @var ILogger */ |
||
| 72 | protected $logger; |
||
| 73 | |||
| 74 | /** @var string */ |
||
| 75 | protected $userId; |
||
| 76 | |||
| 77 | /** @var TimeFactory */ |
||
| 78 | protected $timeFactory; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @param string $appName |
||
| 82 | * @param IRequest $request |
||
| 83 | * @param IAvatarManager $avatarManager |
||
| 84 | * @param ICache $cache |
||
| 85 | * @param IL10N $l10n |
||
| 86 | * @param IUserManager $userManager |
||
| 87 | * @param IRootFolder $rootFolder |
||
| 88 | * @param ILogger $logger |
||
| 89 | * @param string $userId |
||
| 90 | * @param TimeFactory $timeFactory |
||
| 91 | */ |
||
| 92 | View Code Duplication | public function __construct($appName, |
|
|
|
|||
| 93 | IRequest $request, |
||
| 94 | IAvatarManager $avatarManager, |
||
| 95 | ICache $cache, |
||
| 96 | IL10N $l10n, |
||
| 97 | IUserManager $userManager, |
||
| 98 | IRootFolder $rootFolder, |
||
| 99 | ILogger $logger, |
||
| 100 | $userId, |
||
| 101 | TimeFactory $timeFactory) { |
||
| 102 | parent::__construct($appName, $request); |
||
| 103 | |||
| 104 | $this->avatarManager = $avatarManager; |
||
| 105 | $this->cache = $cache; |
||
| 106 | $this->l = $l10n; |
||
| 107 | $this->userManager = $userManager; |
||
| 108 | $this->rootFolder = $rootFolder; |
||
| 109 | $this->logger = $logger; |
||
| 110 | $this->userId = $userId; |
||
| 111 | $this->timeFactory = $timeFactory; |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @NoAdminRequired |
||
| 116 | * @NoCSRFRequired |
||
| 117 | * @PublicPage |
||
| 118 | * |
||
| 119 | * @param string $userId |
||
| 120 | * @param int $size |
||
| 121 | * @return JSONResponse|DataDisplayResponse |
||
| 122 | */ |
||
| 123 | public function getAvatar($userId, $size) { |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @NoAdminRequired |
||
| 177 | * |
||
| 178 | * @param string $path |
||
| 179 | * @return JSONResponse |
||
| 180 | */ |
||
| 181 | public function postAvatar($path) { |
||
| 182 | $files = $this->request->getUploadedFile('files'); |
||
| 183 | |||
| 184 | if (isset($path)) { |
||
| 185 | $path = stripslashes($path); |
||
| 186 | $userFolder = $this->rootFolder->getUserFolder($this->userId); |
||
| 187 | $node = $userFolder->get($path); |
||
| 188 | if (!($node instanceof File)) { |
||
| 189 | return new JSONResponse(['data' => ['message' => $this->l->t('Please select a file.')]]); |
||
| 190 | } |
||
| 191 | View Code Duplication | if ($node->getSize() > 20*1024*1024) { |
|
| 192 | return new JSONResponse( |
||
| 193 | ['data' => ['message' => $this->l->t('File is too big')]], |
||
| 194 | Http::STATUS_BAD_REQUEST |
||
| 195 | ); |
||
| 196 | } |
||
| 197 | $content = $node->getContent(); |
||
| 198 | } elseif (!is_null($files)) { |
||
| 199 | if ( |
||
| 200 | $files['error'][0] === 0 && |
||
| 201 | is_uploaded_file($files['tmp_name'][0]) && |
||
| 202 | !\OC\Files\Filesystem::isFileBlacklisted($files['tmp_name'][0]) |
||
| 203 | ) { |
||
| 204 | View Code Duplication | if ($files['size'][0] > 20*1024*1024) { |
|
| 205 | return new JSONResponse( |
||
| 206 | ['data' => ['message' => $this->l->t('File is too big')]], |
||
| 207 | Http::STATUS_BAD_REQUEST |
||
| 208 | ); |
||
| 209 | } |
||
| 210 | $this->cache->set('avatar_upload', file_get_contents($files['tmp_name'][0]), 7200); |
||
| 211 | $content = $this->cache->get('avatar_upload'); |
||
| 212 | unlink($files['tmp_name'][0]); |
||
| 213 | View Code Duplication | } else { |
|
| 214 | return new JSONResponse( |
||
| 215 | ['data' => ['message' => $this->l->t('Invalid file provided')]], |
||
| 216 | Http::STATUS_BAD_REQUEST |
||
| 217 | ); |
||
| 218 | } |
||
| 219 | View Code Duplication | } else { |
|
| 220 | //Add imgfile |
||
| 221 | return new JSONResponse( |
||
| 222 | ['data' => ['message' => $this->l->t('No image or file provided')]], |
||
| 223 | Http::STATUS_BAD_REQUEST |
||
| 224 | ); |
||
| 225 | } |
||
| 226 | |||
| 227 | try { |
||
| 228 | $image = new \OC_Image(); |
||
| 229 | $image->loadFromData($content); |
||
| 230 | $image->fixOrientation(); |
||
| 231 | |||
| 232 | if ($image->valid()) { |
||
| 233 | $mimeType = $image->mimeType(); |
||
| 234 | if ($mimeType !== 'image/jpeg' && $mimeType !== 'image/png') { |
||
| 235 | return new JSONResponse( |
||
| 236 | ['data' => ['message' => $this->l->t('Unknown filetype')]], |
||
| 237 | Http::STATUS_OK |
||
| 238 | ); |
||
| 239 | } |
||
| 240 | |||
| 241 | $this->cache->set('tmpAvatar', $image->data(), 7200); |
||
| 242 | return new JSONResponse( |
||
| 243 | ['data' => 'notsquare'], |
||
| 244 | Http::STATUS_OK |
||
| 245 | ); |
||
| 246 | View Code Duplication | } else { |
|
| 247 | return new JSONResponse( |
||
| 248 | ['data' => ['message' => $this->l->t('Invalid image')]], |
||
| 249 | Http::STATUS_OK |
||
| 250 | ); |
||
| 251 | } |
||
| 252 | } catch (\Exception $e) { |
||
| 253 | $this->logger->logException($e, ['app' => 'core']); |
||
| 254 | return new JSONResponse(['data' => ['message' => $this->l->t('An error occurred. Please contact your admin.')]], Http::STATUS_OK); |
||
| 255 | } |
||
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * @NoAdminRequired |
||
| 260 | * |
||
| 261 | * @return JSONResponse |
||
| 262 | */ |
||
| 263 | public function deleteAvatar() { |
||
| 264 | try { |
||
| 265 | $avatar = $this->avatarManager->getAvatar($this->userId); |
||
| 266 | $avatar->remove(); |
||
| 267 | return new JSONResponse(); |
||
| 268 | } catch (\Exception $e) { |
||
| 269 | $this->logger->logException($e, ['app' => 'core']); |
||
| 270 | return new JSONResponse(['data' => ['message' => $this->l->t('An error occurred. Please contact your admin.')]], Http::STATUS_BAD_REQUEST); |
||
| 271 | } |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @NoAdminRequired |
||
| 276 | * |
||
| 277 | * @return JSONResponse|DataDisplayResponse |
||
| 278 | */ |
||
| 279 | public function getTmpAvatar() { |
||
| 299 | |||
| 300 | /** |
||
| 301 | * @NoAdminRequired |
||
| 302 | * |
||
| 303 | * @param array $crop |
||
| 304 | * @return JSONResponse |
||
| 305 | */ |
||
| 306 | public function postCroppedAvatar($crop) { |
||
| 341 | } |
||
| 342 |
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.