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:
Complex classes like KeyManager often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use KeyManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 35 | class KeyManager { |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var Session |
||
| 39 | */ |
||
| 40 | protected $session; |
||
| 41 | /** |
||
| 42 | * @var IStorage |
||
| 43 | */ |
||
| 44 | private $keyStorage; |
||
| 45 | /** |
||
| 46 | * @var Crypt |
||
| 47 | */ |
||
| 48 | private $crypt; |
||
| 49 | /** |
||
| 50 | * @var string |
||
| 51 | */ |
||
| 52 | private $recoveryKeyId; |
||
| 53 | /** |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | private $publicShareKeyId; |
||
| 57 | /** |
||
| 58 | * @var string |
||
| 59 | */ |
||
| 60 | private $masterKeyId; |
||
| 61 | /** |
||
| 62 | * @var string UserID |
||
| 63 | */ |
||
| 64 | private $keyId; |
||
| 65 | /** |
||
| 66 | * @var string |
||
| 67 | */ |
||
| 68 | private $publicKeyId = 'publicKey'; |
||
| 69 | /** |
||
| 70 | * @var string |
||
| 71 | */ |
||
| 72 | private $privateKeyId = 'privateKey'; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var string |
||
| 76 | */ |
||
| 77 | private $shareKeyId = 'shareKey'; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var string |
||
| 81 | */ |
||
| 82 | private $fileKeyId = 'fileKey'; |
||
| 83 | /** |
||
| 84 | * @var IConfig |
||
| 85 | */ |
||
| 86 | private $config; |
||
| 87 | /** |
||
| 88 | * @var ILogger |
||
| 89 | */ |
||
| 90 | private $log; |
||
| 91 | /** |
||
| 92 | * @var Util |
||
| 93 | */ |
||
| 94 | private $util; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @param IStorage $keyStorage |
||
| 98 | * @param Crypt $crypt |
||
| 99 | * @param IConfig $config |
||
| 100 | * @param IUserSession $userSession |
||
| 101 | * @param Session $session |
||
| 102 | * @param ILogger $log |
||
| 103 | * @param Util $util |
||
| 104 | */ |
||
| 105 | 42 | public function __construct( |
|
| 106 | IStorage $keyStorage, |
||
| 107 | Crypt $crypt, |
||
| 108 | IConfig $config, |
||
| 109 | IUserSession $userSession, |
||
| 110 | Session $session, |
||
| 111 | ILogger $log, |
||
| 112 | Util $util |
||
| 113 | ) { |
||
| 114 | |||
| 115 | 42 | $this->util = $util; |
|
| 116 | 42 | $this->session = $session; |
|
| 117 | 42 | $this->keyStorage = $keyStorage; |
|
| 118 | 42 | $this->crypt = $crypt; |
|
| 119 | 42 | $this->config = $config; |
|
| 120 | 42 | $this->log = $log; |
|
| 121 | |||
| 122 | 42 | $this->recoveryKeyId = $this->config->getAppValue('encryption', |
|
| 123 | 42 | 'recoveryKeyId'); |
|
| 124 | 42 | if (empty($this->recoveryKeyId)) { |
|
| 125 | $this->recoveryKeyId = 'recoveryKey_' . substr(md5(time()), 0, 8); |
||
| 126 | $this->config->setAppValue('encryption', |
||
| 127 | 'recoveryKeyId', |
||
| 128 | $this->recoveryKeyId); |
||
| 129 | } |
||
| 130 | |||
| 131 | 42 | $this->publicShareKeyId = $this->config->getAppValue('encryption', |
|
| 132 | 42 | 'publicShareKeyId'); |
|
| 133 | 42 | View Code Duplication | if (empty($this->publicShareKeyId)) { |
|
|
|||
| 134 | $this->publicShareKeyId = 'pubShare_' . substr(md5(time()), 0, 8); |
||
| 135 | $this->config->setAppValue('encryption', 'publicShareKeyId', $this->publicShareKeyId); |
||
| 136 | } |
||
| 137 | |||
| 138 | 42 | $this->masterKeyId = $this->config->getAppValue('encryption', |
|
| 139 | 42 | 'masterKeyId'); |
|
| 140 | 42 | View Code Duplication | if (empty($this->masterKeyId)) { |
| 141 | $this->masterKeyId = 'master_' . substr(md5(time()), 0, 8); |
||
| 142 | $this->config->setAppValue('encryption', 'masterKeyId', $this->masterKeyId); |
||
| 143 | } |
||
| 144 | |||
| 145 | 42 | $this->keyId = $userSession && $userSession->isLoggedIn() ? $userSession->getUser()->getUID() : false; |
|
| 146 | 42 | $this->log = $log; |
|
| 147 | 42 | } |
|
| 148 | |||
| 149 | /** |
||
| 150 | * check if key pair for public link shares exists, if not we create one |
||
| 151 | */ |
||
| 152 | 5 | View Code Duplication | public function validateShareKey() { |
| 168 | |||
| 169 | /** |
||
| 170 | * check if a key pair for the master key exists, if not we create one |
||
| 171 | */ |
||
| 172 | 7 | View Code Duplication | public function validateMasterKey() { |
| 188 | |||
| 189 | /** |
||
| 190 | * @return bool |
||
| 191 | */ |
||
| 192 | 10 | public function recoveryKeyExists() { |
|
| 196 | |||
| 197 | /** |
||
| 198 | * get recovery key |
||
| 199 | * |
||
| 200 | * @return string |
||
| 201 | */ |
||
| 202 | 10 | public function getRecoveryKey() { |
|
| 205 | |||
| 206 | /** |
||
| 207 | * get recovery key ID |
||
| 208 | * |
||
| 209 | * @return string |
||
| 210 | */ |
||
| 211 | 3 | public function getRecoveryKeyId() { |
|
| 214 | |||
| 215 | /** |
||
| 216 | * @param $password |
||
| 217 | * @return bool |
||
| 218 | */ |
||
| 219 | 1 | public function checkRecoveryPassword($password) { |
|
| 228 | |||
| 229 | /** |
||
| 230 | * @param string $uid |
||
| 231 | * @param string $password |
||
| 232 | * @param string $keyPair |
||
| 233 | * @return bool |
||
| 234 | */ |
||
| 235 | 5 | public function storeKeyPair($uid, $password, $keyPair) { |
|
| 249 | |||
| 250 | /** |
||
| 251 | * @param string $password |
||
| 252 | * @param array $keyPair |
||
| 253 | * @return bool |
||
| 254 | */ |
||
| 255 | 1 | public function setRecoveryKey($password, $keyPair) { |
|
| 271 | |||
| 272 | /** |
||
| 273 | * @param $userId |
||
| 274 | * @param $key |
||
| 275 | * @return bool |
||
| 276 | */ |
||
| 277 | 6 | public function setPublicKey($userId, $key) { |
|
| 280 | |||
| 281 | /** |
||
| 282 | * @param $userId |
||
| 283 | * @param $key |
||
| 284 | * @return bool |
||
| 285 | */ |
||
| 286 | 6 | public function setPrivateKey($userId, $key) { |
|
| 292 | |||
| 293 | /** |
||
| 294 | * write file key to key storage |
||
| 295 | * |
||
| 296 | * @param string $path |
||
| 297 | * @param string $key |
||
| 298 | * @return boolean |
||
| 299 | */ |
||
| 300 | 5 | public function setFileKey($path, $key) { |
|
| 303 | |||
| 304 | /** |
||
| 305 | * set all file keys (the file key and the corresponding share keys) |
||
| 306 | * |
||
| 307 | * @param string $path |
||
| 308 | * @param array $keys |
||
| 309 | */ |
||
| 310 | 5 | public function setAllFileKeys($path, $keys) { |
|
| 316 | |||
| 317 | /** |
||
| 318 | * write share key to the key storage |
||
| 319 | * |
||
| 320 | * @param string $path |
||
| 321 | * @param string $uid |
||
| 322 | * @param string $key |
||
| 323 | * @return boolean |
||
| 324 | */ |
||
| 325 | 5 | public function setShareKey($path, $uid, $key) { |
|
| 329 | |||
| 330 | /** |
||
| 331 | * Decrypt private key and store it |
||
| 332 | * |
||
| 333 | * @param string $uid userid |
||
| 334 | * @param string $passPhrase users password |
||
| 335 | * @return boolean |
||
| 336 | */ |
||
| 337 | 7 | public function init($uid, $passPhrase) { |
|
| 365 | |||
| 366 | /** |
||
| 367 | * @param $userId |
||
| 368 | * @return mixed |
||
| 369 | * @throws PrivateKeyMissingException |
||
| 370 | */ |
||
| 371 | 10 | View Code Duplication | public function getPrivateKey($userId) { |
| 380 | |||
| 381 | /** |
||
| 382 | * @param $path |
||
| 383 | * @param $uid |
||
| 384 | * @return string |
||
| 385 | */ |
||
| 386 | 13 | public function getFileKey($path, $uid) { |
|
| 412 | |||
| 413 | /** |
||
| 414 | * get the encrypted file key |
||
| 415 | * |
||
| 416 | * @param $path |
||
| 417 | * @return string |
||
| 418 | */ |
||
| 419 | 1 | public function getEncryptedFileKey($path) { |
|
| 425 | |||
| 426 | /** |
||
| 427 | * delete share key |
||
| 428 | * |
||
| 429 | * @param string $path |
||
| 430 | * @param string $keyId |
||
| 431 | * @return boolean |
||
| 432 | */ |
||
| 433 | 1 | public function deleteShareKey($path, $keyId) { |
|
| 439 | |||
| 440 | |||
| 441 | /** |
||
| 442 | * @param $path |
||
| 443 | * @param $uid |
||
| 444 | * @return mixed |
||
| 445 | */ |
||
| 446 | 13 | public function getShareKey($path, $uid) { |
|
| 450 | |||
| 451 | /** |
||
| 452 | * check if user has a private and a public key |
||
| 453 | * |
||
| 454 | * @param string $userId |
||
| 455 | * @return bool |
||
| 456 | * @throws PrivateKeyMissingException |
||
| 457 | * @throws PublicKeyMissingException |
||
| 458 | */ |
||
| 459 | 9 | public function userHasKeys($userId) { |
|
| 483 | |||
| 484 | /** |
||
| 485 | * @param $userId |
||
| 486 | * @return mixed |
||
| 487 | * @throws PublicKeyMissingException |
||
| 488 | */ |
||
| 489 | 10 | View Code Duplication | public function getPublicKey($userId) { |
| 497 | |||
| 498 | 2 | public function getPublicShareKeyId() { |
|
| 501 | |||
| 502 | /** |
||
| 503 | * get public key for public link shares |
||
| 504 | * |
||
| 505 | * @return string |
||
| 506 | */ |
||
| 507 | 7 | public function getPublicShareKey() { |
|
| 510 | |||
| 511 | /** |
||
| 512 | * @param $purpose |
||
| 513 | * @param bool $timestamp |
||
| 514 | * @param bool $includeUserKeys |
||
| 515 | */ |
||
| 516 | public function backupAllKeys($purpose, $timestamp = true, $includeUserKeys = true) { |
||
| 519 | |||
| 520 | /** |
||
| 521 | * @param string $uid |
||
| 522 | */ |
||
| 523 | public function replaceUserKeys($uid) { |
||
| 528 | |||
| 529 | /** |
||
| 530 | * @param $uid |
||
| 531 | * @return bool |
||
| 532 | */ |
||
| 533 | public function deletePublicKey($uid) { |
||
| 536 | |||
| 537 | /** |
||
| 538 | * @param $uid |
||
| 539 | * @return bool |
||
| 540 | */ |
||
| 541 | 1 | private function deletePrivateKey($uid) { |
|
| 544 | |||
| 545 | 1 | public function deleteAllFileKeys($path) { |
|
| 548 | |||
| 549 | /** |
||
| 550 | * @param array $userIds |
||
| 551 | * @return array |
||
| 552 | * @throws PublicKeyMissingException |
||
| 553 | */ |
||
| 554 | public function getPublicKeys(array $userIds) { |
||
| 568 | |||
| 569 | /** |
||
| 570 | * @param string $keyId |
||
| 571 | * @return string returns openssl key |
||
| 572 | */ |
||
| 573 | 1 | public function getSystemPrivateKey($keyId) { |
|
| 576 | |||
| 577 | /** |
||
| 578 | * @param string $keyId |
||
| 579 | * @param string $key |
||
| 580 | * @return string returns openssl key |
||
| 581 | */ |
||
| 582 | 3 | public function setSystemPrivateKey($keyId, $key) { |
|
| 588 | |||
| 589 | /** |
||
| 590 | * add system keys such as the public share key and the recovery key |
||
| 591 | * |
||
| 592 | * @param array $accessList |
||
| 593 | * @param array $publicKeys |
||
| 594 | * @param string $uid |
||
| 595 | * @return array |
||
| 596 | * @throws PublicKeyMissingException |
||
| 597 | */ |
||
| 598 | 9 | public function addSystemKeys(array $accessList, array $publicKeys, $uid) { |
|
| 615 | |||
| 616 | /** |
||
| 617 | * get master key password |
||
| 618 | * |
||
| 619 | * @return string |
||
| 620 | * @throws \Exception |
||
| 621 | */ |
||
| 622 | 3 | protected function getMasterKeyPassword() { |
|
| 630 | |||
| 631 | /** |
||
| 632 | * return master key id |
||
| 633 | * |
||
| 634 | * @return string |
||
| 635 | */ |
||
| 636 | 5 | public function getMasterKeyId() { |
|
| 639 | |||
| 640 | /** |
||
| 641 | * get public master key |
||
| 642 | * |
||
| 643 | * @return string |
||
| 644 | */ |
||
| 645 | 6 | public function getPublicMasterKey() { |
|
| 648 | } |
||
| 649 |
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.