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 |
||
| 21 | abstract class RSAESKeyAlgorithm extends KeyManagementAlgorithm |
||
| 22 | { |
||
| 23 | use RandomCEK; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Public key. |
||
| 27 | * |
||
| 28 | * @var RSAPublicKeyJWK $_publicKey |
||
| 29 | */ |
||
| 30 | protected $_publicKey; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Private key. |
||
| 34 | * |
||
| 35 | * @var RSAPrivateKeyJWK|null $_privateKey |
||
| 36 | */ |
||
| 37 | protected $_privateKey; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Mapping from algorithm name to class name. |
||
| 41 | * |
||
| 42 | * @internal |
||
| 43 | * |
||
| 44 | * @var array |
||
| 45 | */ |
||
| 46 | const MAP_ALGO_TO_CLASS = array( |
||
| 47 | /* @formatter:off */ |
||
| 48 | JWA::ALGO_RSA1_5 => RSAESPKCS1Algorithm::class, |
||
| 49 | JWA::ALGO_RSA_OAEP => RSAESOAEPAlgorithm::class |
||
| 50 | /* @formatter:on */ |
||
| 51 | ); |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Get the padding scheme. |
||
| 55 | * |
||
| 56 | * @return int |
||
| 57 | */ |
||
| 58 | abstract protected function _paddingScheme(); |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Constructor. |
||
| 62 | * |
||
| 63 | * Use <code>fromPublicKey</code> or <code>fromPrivateKey</code> instead! |
||
| 64 | * |
||
| 65 | * @param RSAPublicKeyJWK $pub_key RSA public key |
||
| 66 | * @param RSAPrivateKeyJWK $priv_key Optional RSA private key |
||
| 67 | */ |
||
| 68 | 18 | protected function __construct(RSAPublicKeyJWK $pub_key, |
|
| 73 | |||
| 74 | /** |
||
| 75 | * |
||
| 76 | * @param JWK $jwk |
||
| 77 | * @param Header $header |
||
| 78 | * @throws \UnexpectedValueException |
||
| 79 | * @return RSAESKeyAlgorithm |
||
| 80 | */ |
||
| 81 | 3 | public static function fromJWK(JWK $jwk, Header $header) { |
|
| 92 | |||
| 93 | /** |
||
| 94 | * Initialize from a public key. |
||
| 95 | * |
||
| 96 | * @param RSAPublicKeyJWK $jwk |
||
| 97 | * @return self |
||
| 98 | */ |
||
| 99 | 3 | public static function fromPublicKey(RSAPublicKeyJWK $jwk) { |
|
| 102 | |||
| 103 | /** |
||
| 104 | * Initialize from a private key. |
||
| 105 | * |
||
| 106 | * @param RSAPrivateKeyJWK $jwk |
||
| 107 | * @return self |
||
| 108 | */ |
||
| 109 | 15 | public static function fromPrivateKey(RSAPrivateKeyJWK $jwk) { |
|
| 112 | |||
| 113 | /** |
||
| 114 | * Get the public key. |
||
| 115 | * |
||
| 116 | * @return RSAPublicKeyJWK |
||
| 117 | */ |
||
| 118 | 11 | public function publicKey() { |
|
| 121 | |||
| 122 | /** |
||
| 123 | * Check whether the private key is present. |
||
| 124 | * |
||
| 125 | * @return bool |
||
| 126 | */ |
||
| 127 | 13 | public function hasPrivateKey() { |
|
| 130 | |||
| 131 | /** |
||
| 132 | * Get the private key. |
||
| 133 | * |
||
| 134 | * @throws \LogicException |
||
| 135 | * @return RSAPrivateKeyJWK |
||
| 136 | */ |
||
| 137 | 12 | public function privateKey() { |
|
| 143 | |||
| 144 | 10 | View Code Duplication | protected function _encryptKey($key, Header &$header) { |
| 163 | |||
| 164 | 10 | View Code Duplication | protected function _decryptKey($ciphertext, Header $header) { |
| 183 | |||
| 184 | /** |
||
| 185 | * Get last OpenSSL error message. |
||
| 186 | * |
||
| 187 | * @return string|null |
||
| 188 | */ |
||
| 189 | 4 | View Code Duplication | protected function _getLastOpenSSLError() { |
| 196 | |||
| 197 | 3 | public function headerParameters() { |
|
| 200 | } |
||
| 201 |
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.