Complex classes like Encrypter 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 Encrypter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | final class Encrypter implements EncrypterInterface |
||
| 34 | { |
||
| 35 | use HasKeyChecker; |
||
| 36 | use HasJWAManager; |
||
| 37 | use HasCompressionManager; |
||
| 38 | use CommonCipheringMethods; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * {@inheritdoc} |
||
| 42 | */ |
||
| 43 | public static function createEncrypter(array $key_encryption_algorithms, array $content_encryption_algorithms, array $compression_methods = ['DEF', 'ZLIB', 'GZ']) |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Decrypter constructor. |
||
| 52 | * |
||
| 53 | * @param string[]|\Jose\Algorithm\KeyEncryptionAlgorithmInterface[] $key_encryption_algorithms |
||
| 54 | * @param string[]|\Jose\Algorithm\ContentEncryptionAlgorithmInterface[] $content_encryption_algorithms |
||
| 55 | * @param string[]|\Jose\Compression\CompressionInterface[] $compression_methods |
||
| 56 | */ |
||
| 57 | public function __construct(array $key_encryption_algorithms, array $content_encryption_algorithms, array $compression_methods) |
||
| 65 | |||
| 66 | /** |
||
| 67 | * {@inheritdoc} |
||
| 68 | */ |
||
| 69 | public function encrypt(JWEInterface &$jwe) |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @param \Jose\Object\JWEInterface $jwe |
||
| 98 | * @param \Jose\Object\RecipientInterface $recipient |
||
| 99 | * @param string $cek |
||
| 100 | * @param \Jose\Algorithm\ContentEncryptionAlgorithmInterface $content_encryption_algorithm |
||
| 101 | * @param array $additional_headers |
||
| 102 | */ |
||
| 103 | private function processRecipient(JWEInterface $jwe, RecipientInterface &$recipient, $cek, ContentEncryptionAlgorithmInterface $content_encryption_algorithm, array &$additional_headers) |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @param \Jose\Object\JWEInterface $jwe |
||
| 123 | * @param \Jose\Algorithm\ContentEncryptionAlgorithmInterface $content_encryption_algorithm |
||
| 124 | * @param string $key_management_mode |
||
| 125 | * @param array $additional_headers |
||
| 126 | * |
||
| 127 | * @return string |
||
| 128 | */ |
||
| 129 | private function determineCEK(JWEInterface $jwe, ContentEncryptionAlgorithmInterface $content_encryption_algorithm, $key_management_mode, array &$additional_headers) |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @param \Jose\Object\JWEInterface $jwe |
||
| 154 | * |
||
| 155 | * @return string |
||
| 156 | */ |
||
| 157 | private function getKeyManagementMode(JWEInterface $jwe) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @param \Jose\Object\JWEInterface $jwe |
||
| 181 | * |
||
| 182 | * @return \Jose\Compression\CompressionInterface|null |
||
| 183 | */ |
||
| 184 | private function getCompressionMethod(JWEInterface $jwe) |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @param \Jose\Object\JWEInterface $jwe |
||
| 218 | * |
||
| 219 | * @return \Jose\Algorithm\ContentEncryptionAlgorithmInterface |
||
| 220 | */ |
||
| 221 | private function getContentEncryptionAlgorithm(JWEInterface $jwe) |
||
| 240 | |||
| 241 | /** |
||
| 242 | * @param \Jose\Object\JWEInterface $jwe |
||
| 243 | * @param \Jose\Algorithm\ContentEncryptionAlgorithmInterface $content_encryption_algorithm |
||
| 244 | * @param string $cek |
||
| 245 | * @param string $iv |
||
| 246 | * @param \Jose\Compression\CompressionInterface|null $compression_method |
||
| 247 | */ |
||
| 248 | private function encryptJWE(JWEInterface &$jwe, ContentEncryptionAlgorithmInterface $content_encryption_algorithm, $cek, $iv, CompressionInterface $compression_method = null) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @param \Jose\Algorithm\KeyEncryptionAlgorithmInterface $key_encryption_algorithm |
||
| 268 | * @param \Jose\Algorithm\ContentEncryptionAlgorithmInterface $content_encryption_algorithm |
||
| 269 | * @param \Jose\Object\JWKInterface $recipient_key |
||
| 270 | */ |
||
| 271 | private function checkKeys(KeyEncryptionAlgorithmInterface $key_encryption_algorithm, ContentEncryptionAlgorithmInterface $content_encryption_algorithm, JWKInterface $recipient_key) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * @param string $current |
||
| 283 | * @param string $new |
||
| 284 | * |
||
| 285 | * @return bool |
||
| 286 | */ |
||
| 287 | private function areKeyManagementModesCompatible($current, $new) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * @param string $payload |
||
| 304 | * @param \Jose\Compression\CompressionInterface|null $compression_method |
||
| 305 | * |
||
| 306 | * @return string |
||
| 307 | */ |
||
| 308 | private function preparePayload($payload, CompressionInterface $compression_method = null) |
||
| 321 | |||
| 322 | /** |
||
| 323 | * @param array $complete_headers |
||
| 324 | * @param string $cek |
||
| 325 | * @param \Jose\Algorithm\KeyEncryptionAlgorithmInterface $key_encryption_algorithm |
||
| 326 | * @param \Jose\Algorithm\ContentEncryptionAlgorithmInterface $content_encryption_algorithm |
||
| 327 | * @param \Jose\Object\JWKInterface $recipient_key |
||
| 328 | * @param array $additional_headers |
||
| 329 | * |
||
| 330 | * @return string|null |
||
| 331 | */ |
||
| 332 | private function getEncryptedKey(array $complete_headers, $cek, KeyEncryptionAlgorithmInterface $key_encryption_algorithm, ContentEncryptionAlgorithmInterface $content_encryption_algorithm, array &$additional_headers, JWKInterface $recipient_key) |
||
| 342 | |||
| 343 | /** |
||
| 344 | * @param array $complete_headers |
||
| 345 | * @param string $cek |
||
| 346 | * @param \Jose\Algorithm\KeyEncryption\KeyAgreementWrappingInterface $key_encryption_algorithm |
||
| 347 | * @param \Jose\Algorithm\ContentEncryptionAlgorithmInterface $content_encryption_algorithm |
||
| 348 | * @param array $additional_headers |
||
| 349 | * @param \Jose\Object\JWKInterface $recipient_key |
||
| 350 | * |
||
| 351 | * @return string |
||
| 352 | */ |
||
| 353 | private function getEncryptedKeyFromKeyAgreementAndKeyWrappingAlgorithm(array $complete_headers, $cek, KeyAgreementWrappingInterface $key_encryption_algorithm, ContentEncryptionAlgorithmInterface $content_encryption_algorithm, array &$additional_headers, JWKInterface $recipient_key) |
||
| 359 | |||
| 360 | /** |
||
| 361 | * @param array $complete_headers |
||
| 362 | * @param string $cek |
||
| 363 | * @param \Jose\Algorithm\KeyEncryption\KeyEncryptionInterface $key_encryption_algorithm |
||
| 364 | * @param \Jose\Object\JWKInterface $recipient_key |
||
| 365 | * @param array $additional_headers |
||
| 366 | * |
||
| 367 | * @return string |
||
| 368 | */ |
||
| 369 | private function getEncryptedKeyFromKeyEncryptionAlgorithm(array $complete_headers, $cek, KeyEncryptionInterface $key_encryption_algorithm, JWKInterface $recipient_key, array &$additional_headers) |
||
| 373 | |||
| 374 | /** |
||
| 375 | * @param array $complete_headers |
||
| 376 | * @param string $cek |
||
| 377 | * @param \Jose\Algorithm\KeyEncryption\KeyWrappingInterface $key_encryption_algorithm |
||
| 378 | * @param \Jose\Object\JWKInterface $recipient_key |
||
| 379 | * @param array $additional_headers |
||
| 380 | * |
||
| 381 | * @return string |
||
| 382 | */ |
||
| 383 | private function getEncryptedKeyFromKeyWrappingAlgorithm(array $complete_headers, $cek, KeyWrappingInterface $key_encryption_algorithm, JWKInterface $recipient_key, &$additional_headers) |
||
| 387 | |||
| 388 | /** |
||
| 389 | * @param array $complete_headers |
||
| 390 | * |
||
| 391 | * @return \Jose\Algorithm\KeyEncryptionAlgorithmInterface |
||
| 392 | */ |
||
| 393 | private function findKeyEncryptionAlgorithm(array $complete_headers) |
||
| 401 | |||
| 402 | /** |
||
| 403 | * @param int $size |
||
| 404 | * |
||
| 405 | * @return string |
||
| 406 | */ |
||
| 407 | private function createCEK($size) |
||
| 411 | |||
| 412 | /** |
||
| 413 | * @param int $size |
||
| 414 | * |
||
| 415 | * @return string |
||
| 416 | */ |
||
| 417 | private function createIV($size) |
||
| 421 | } |
||
| 422 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: