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 |
||
| 37 | class DecryptAll { |
||
| 38 | |||
| 39 | /** @var OutputInterface */ |
||
| 40 | protected $output; |
||
| 41 | |||
| 42 | /** @var InputInterface */ |
||
| 43 | protected $input; |
||
| 44 | |||
| 45 | /** @var Manager */ |
||
| 46 | protected $encryptionManager; |
||
| 47 | |||
| 48 | /** @var IUserManager */ |
||
| 49 | protected $userManager; |
||
| 50 | |||
| 51 | /** @var View */ |
||
| 52 | protected $rootView; |
||
| 53 | |||
| 54 | /** @var array files which couldn't be decrypted */ |
||
| 55 | protected $failed; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @param Manager $encryptionManager |
||
| 59 | * @param IUserManager $userManager |
||
| 60 | * @param View $rootView |
||
| 61 | */ |
||
| 62 | public function __construct( |
||
| 72 | |||
| 73 | /** |
||
| 74 | * start to decrypt all files |
||
| 75 | * |
||
| 76 | * @param InputInterface $input |
||
| 77 | * @param OutputInterface $output |
||
| 78 | * @param string $user which users data folder should be decrypted, default = all users |
||
| 79 | * @return bool |
||
| 80 | * @throws \Exception |
||
| 81 | */ |
||
| 82 | public function decryptAll(InputInterface $input, OutputInterface $output, $user = '') { |
||
| 113 | |||
| 114 | /** |
||
| 115 | * prepare encryption modules to perform the decrypt all function |
||
| 116 | * |
||
| 117 | * @param $user |
||
| 118 | * @return bool |
||
| 119 | */ |
||
| 120 | protected function prepareEncryptionModules($user) { |
||
| 137 | |||
| 138 | /** |
||
| 139 | * iterate over all user and encrypt their files |
||
| 140 | * |
||
| 141 | * @param string $user which users files should be decrypted, default = all users |
||
| 142 | */ |
||
| 143 | protected function decryptAllUsersFiles($user = '') { |
||
| 196 | |||
| 197 | /** |
||
| 198 | * encrypt files from the given user |
||
| 199 | * |
||
| 200 | * @param string $uid |
||
| 201 | * @param ProgressBar $progress |
||
| 202 | * @param string $userCount |
||
| 203 | */ |
||
| 204 | protected function decryptUsersFiles($uid, ProgressBar $progress, $userCount) { |
||
| 205 | |||
| 206 | $this->setupUserFS($uid); |
||
| 207 | $directories = array(); |
||
| 208 | $directories[] = '/' . $uid . '/files'; |
||
| 209 | |||
| 210 | while ($root = array_pop($directories)) { |
||
| 211 | $content = $this->rootView->getDirectoryContent($root); |
||
| 212 | foreach ($content as $file) { |
||
| 213 | // only decrypt files owned by the user |
||
| 214 | if($file->getStorage()->instanceOfStorage('OC\Files\Storage\Shared')) { |
||
| 215 | continue; |
||
| 216 | } |
||
| 217 | $path = $root . '/' . $file['name']; |
||
| 218 | if ($this->rootView->is_dir($path)) { |
||
| 219 | $directories[] = $path; |
||
| 220 | continue; |
||
| 221 | } else { |
||
| 222 | try { |
||
| 223 | $progress->setMessage("decrypt files for user $userCount: $path"); |
||
| 224 | $progress->advance(); |
||
| 225 | View Code Duplication | if ($file->isEncrypted() === false) { |
|
| 226 | $progress->setMessage("decrypt files for user $userCount: $path (already decrypted)"); |
||
| 227 | $progress->advance(); |
||
| 228 | } else { |
||
| 229 | if ($this->decryptFile($path) === false) { |
||
| 230 | $progress->setMessage("decrypt files for user $userCount: $path (already decrypted)"); |
||
| 231 | $progress->advance(); |
||
| 232 | } |
||
| 233 | } |
||
| 234 | } catch (\Exception $e) { |
||
| 235 | if (isset($this->failed[$uid])) { |
||
| 236 | $this->failed[$uid][] = $path; |
||
| 237 | } else { |
||
| 238 | $this->failed[$uid] = [$path]; |
||
| 239 | } |
||
| 240 | } |
||
| 241 | } |
||
| 242 | } |
||
| 243 | } |
||
| 244 | } |
||
| 245 | |||
| 246 | /** |
||
| 247 | * encrypt file |
||
| 248 | * |
||
| 249 | * @param string $path |
||
| 250 | * @return bool |
||
| 251 | */ |
||
| 252 | View Code Duplication | protected function decryptFile($path) { |
|
| 269 | |||
| 270 | /** |
||
| 271 | * get current timestamp |
||
| 272 | * |
||
| 273 | * @return int |
||
| 274 | */ |
||
| 275 | protected function getTimestamp() { |
||
| 278 | |||
| 279 | |||
| 280 | /** |
||
| 281 | * setup user file system |
||
| 282 | * |
||
| 283 | * @param string $uid |
||
| 284 | */ |
||
| 285 | protected function setupUserFS($uid) { |
||
| 289 | |||
| 290 | } |
||
| 291 |
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.