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 |
||
9 | class Decorator extends EncryptingDecorator |
||
10 | { |
||
11 | /** @var resource */ |
||
12 | private $publicKey; |
||
13 | /** @var resource */ |
||
14 | private $privateKey; |
||
15 | /** @var string */ |
||
16 | private $cipher; |
||
17 | |||
18 | /** |
||
19 | * @param Cache $decorated |
||
20 | * @param mixed $cert |
||
21 | * @param mixed $key |
||
22 | * @param string|null $passphrase |
||
23 | * @param string $cipher |
||
24 | * |
||
25 | * @throws IAE If OpenSSL keys cannot be extracted from $cert and $key. |
||
26 | 93 | */ |
|
27 | public function __construct( |
||
39 | 57 | ||
40 | public function __destruct() |
||
45 | 57 | ||
46 | protected function isDataDecryptable($data, string $id): bool |
||
54 | 36 | ||
55 | protected function encrypt($data, string $id): EncryptedValue |
||
69 | 18 | ||
70 | View Code Duplication | protected function decrypt($data) |
|
81 | 93 | ||
82 | private function setPublicKey($cert) |
||
92 | 93 | ||
93 | private function setPrivateKey($key, $passphrase) |
||
102 | 93 | ||
103 | private function validateOpenSslKey($key) |
||
107 | 36 | ||
108 | private function signString($string) |
||
114 | 18 | ||
115 | private function validateSignature($signed, $signature) |
||
119 | 36 | ||
120 | private function encryptEnvelopeKey($key) |
||
126 | 18 | ||
127 | private function decryptEnvelopeKey($sealedKey) |
||
133 | } |
||
134 |
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.