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 |
||
| 18 | abstract class KeyManagementAlgorithm implements |
||
| 19 | AlgorithmParameterValue, |
||
|
|
|||
| 20 | HeaderParameters |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * ID of the key used by the algorithm. |
||
| 24 | * |
||
| 25 | * If set, KeyID parameter shall be automatically inserted into JWE's |
||
| 26 | * header. |
||
| 27 | * |
||
| 28 | * @var string|null $_keyID |
||
| 29 | */ |
||
| 30 | protected $_keyID; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Encrypt a key. |
||
| 34 | * |
||
| 35 | * @param string $key Key to be encrypted |
||
| 36 | * @param Header $header Reference to the Header variable, that shall |
||
| 37 | * be updated to contain parameters specific to the encryption |
||
| 38 | * @return string Ciphertext |
||
| 39 | */ |
||
| 40 | abstract protected function _encryptKey($key, Header &$header); |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Decrypt a key. |
||
| 44 | * |
||
| 45 | * @param string $ciphertext Ciphertext of the encrypted key |
||
| 46 | * @param Header $header Header possibly containing encoding specific |
||
| 47 | * parameters |
||
| 48 | * @return string Plaintext key |
||
| 49 | */ |
||
| 50 | abstract protected function _decryptKey($ciphertext, Header $header); |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Encrypt a key to be inserted into JWE header. |
||
| 54 | * |
||
| 55 | * @param string $cek Content encryption key |
||
| 56 | * @param Header|null $header Optional reference to the Header variable, |
||
| 57 | * which may be updated to contain parameters specific to this |
||
| 58 | * encrypt invocation. If the variable is referenced, but is a null, |
||
| 59 | * it shall be initialized to an empty Header. |
||
| 60 | * @throws \RuntimeException For generic errors |
||
| 61 | * @return string Encrypted key |
||
| 62 | */ |
||
| 63 | 49 | final public function encrypt($cek, Header &$header = null) { |
|
| 69 | |||
| 70 | /** |
||
| 71 | * Decrypt a CEK from the encrypted data. |
||
| 72 | * |
||
| 73 | * @param string $data Encrypted key |
||
| 74 | * @param Header|null Optional header containing parameters required to |
||
| 75 | * decrypt the key. |
||
| 76 | * @throws \RuntimeException For generic errors |
||
| 77 | * @return string Content encryption key |
||
| 78 | */ |
||
| 79 | 44 | final public function decrypt($data, Header $header = null) { |
|
| 85 | |||
| 86 | /** |
||
| 87 | * Get content encryption key for the encryption. |
||
| 88 | * |
||
| 89 | * Returned key may be random depending on the key management algorithm. |
||
| 90 | * |
||
| 91 | * @param int $length Required key size in bytes |
||
| 92 | * @return string |
||
| 93 | */ |
||
| 94 | abstract public function cekForEncryption($length); |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Initialize key management algorithm from a JWK and a header. |
||
| 98 | * |
||
| 99 | * @param JWK $jwk |
||
| 100 | * @param Header $header |
||
| 101 | * @return KeyManagementAlgorithm |
||
| 102 | */ |
||
| 103 | 3 | public static function fromJWK(JWK $jwk, Header $header) { |
|
| 107 | |||
| 108 | /** |
||
| 109 | * Get self with key ID. |
||
| 110 | * |
||
| 111 | * @param string|null $id Key ID or null to remove |
||
| 112 | * @return self |
||
| 113 | */ |
||
| 114 | 3 | public function withKeyID($id) { |
|
| 119 | |||
| 120 | /** |
||
| 121 | * |
||
| 122 | * @see \JWX\JWT\Header\HeaderParameters::headerParameters() |
||
| 123 | * @return JWTParameter[] |
||
| 124 | */ |
||
| 125 | 19 | View Code Duplication | public function headerParameters() { |
| 132 | } |
||
| 133 |