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 |
||
| 17 | final class Encrypter implements EncrypterInterface |
||
| 18 | { |
||
| 19 | use Behaviour\HasKeyChecker; |
||
| 20 | use Behaviour\HasJWAManager; |
||
| 21 | use Behaviour\HasCompressionManager; |
||
| 22 | use Behaviour\CommonCipheringMethods; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * {@inheritdoc} |
||
| 26 | */ |
||
| 27 | public static function createEncrypter(array $key_encryption_algorithms, array $content_encryption_algorithms, array $compression_methods = ['DEF', 'ZLIB', 'GZ']) |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Decrypter constructor. |
||
| 36 | * |
||
| 37 | * @param string[]|\Jose\Algorithm\KeyEncryptionAlgorithmInterface[] $key_encryption_algorithms |
||
| 38 | * @param string[]|\Jose\Algorithm\ContentEncryptionAlgorithmInterface[] $content_encryption_algorithms |
||
| 39 | * @param string[]|\Jose\Compression\CompressionInterface[] $compression_methods |
||
| 40 | */ |
||
| 41 | public function __construct(array $key_encryption_algorithms, array $content_encryption_algorithms, array $compression_methods) |
||
| 49 | |||
| 50 | /** |
||
| 51 | * {@inheritdoc} |
||
| 52 | */ |
||
| 53 | public function encrypt(Object\JWEInterface &$jwe) |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @param \Jose\Object\JWEInterface $jwe |
||
| 82 | * @param \Jose\Object\RecipientInterface $recipient |
||
| 83 | * @param string $cek |
||
| 84 | * @param \Jose\Algorithm\ContentEncryptionAlgorithmInterface $content_encryption_algorithm |
||
| 85 | * @param array $additional_headers |
||
| 86 | */ |
||
| 87 | private function processRecipient(Object\JWEInterface $jwe, Object\RecipientInterface &$recipient, $cek, Algorithm\ContentEncryptionAlgorithmInterface $content_encryption_algorithm, array &$additional_headers) |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @param \Jose\Object\JWEInterface $jwe |
||
| 107 | * @param \Jose\Algorithm\ContentEncryptionAlgorithmInterface $content_encryption_algorithm |
||
| 108 | * @param string $key_management_mode |
||
| 109 | * @param array $additional_headers |
||
| 110 | * |
||
| 111 | * @return string |
||
| 112 | */ |
||
| 113 | private function determineCEK(Object\JWEInterface $jwe, Algorithm\ContentEncryptionAlgorithmInterface $content_encryption_algorithm, $key_management_mode, array &$additional_headers) |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @param \Jose\Object\JWEInterface $jwe |
||
| 138 | * |
||
| 139 | * @return string |
||
| 140 | */ |
||
| 141 | private function getKeyManagementMode(Object\JWEInterface $jwe) |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @param \Jose\Object\JWEInterface $jwe |
||
| 165 | * |
||
| 166 | * @return \Jose\Compression\CompressionInterface|null |
||
| 167 | */ |
||
| 168 | private function getCompressionMethod(Object\JWEInterface $jwe) |
||
| 199 | |||
| 200 | /** |
||
| 201 | * @param \Jose\Object\JWEInterface $jwe |
||
| 202 | * |
||
| 203 | * @return \Jose\Algorithm\ContentEncryptionAlgorithmInterface |
||
| 204 | */ |
||
| 205 | private function getContentEncryptionAlgorithm(Object\JWEInterface $jwe) |
||
| 224 | |||
| 225 | /** |
||
| 226 | * @param \Jose\Object\JWEInterface $jwe |
||
| 227 | * @param \Jose\Algorithm\ContentEncryptionAlgorithmInterface $content_encryption_algorithm |
||
| 228 | * @param string $cek |
||
| 229 | * @param string $iv |
||
| 230 | * @param \Jose\Compression\CompressionInterface|null $compression_method |
||
| 231 | */ |
||
| 232 | private function encryptJWE(Object\JWEInterface &$jwe, Algorithm\ContentEncryptionAlgorithmInterface $content_encryption_algorithm, $cek, $iv, Compression\CompressionInterface $compression_method = null) |
||
| 249 | |||
| 250 | /** |
||
| 251 | * @param \Jose\Algorithm\KeyEncryptionAlgorithmInterface $key_encryption_algorithm |
||
| 252 | * @param \Jose\Algorithm\ContentEncryptionAlgorithmInterface $content_encryption_algorithm |
||
| 253 | * @param \Jose\Object\JWKInterface $recipient_key |
||
| 254 | */ |
||
| 255 | private function checkKeys(Algorithm\KeyEncryptionAlgorithmInterface $key_encryption_algorithm, Algorithm\ContentEncryptionAlgorithmInterface $content_encryption_algorithm, Object\JWKInterface $recipient_key) |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @param string $current |
||
| 267 | * @param string $new |
||
| 268 | * |
||
| 269 | * @return bool |
||
| 270 | */ |
||
| 271 | private function areKeyManagementModesCompatible($current, $new) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @param string $payload |
||
| 288 | * @param \Jose\Compression\CompressionInterface|null $compression_method |
||
| 289 | * |
||
| 290 | * @return string |
||
| 291 | */ |
||
| 292 | private function preparePayload($payload, Compression\CompressionInterface $compression_method = null) |
||
| 305 | |||
| 306 | /** |
||
| 307 | * @param array $complete_headers |
||
| 308 | * @param string $cek |
||
| 309 | * @param \Jose\Algorithm\KeyEncryptionAlgorithmInterface $key_encryption_algorithm |
||
| 310 | * @param \Jose\Algorithm\ContentEncryptionAlgorithmInterface $content_encryption_algorithm |
||
| 311 | * @param \Jose\Object\JWKInterface $recipient_key |
||
| 312 | * @param array $additional_headers |
||
| 313 | * |
||
| 314 | * @return string|null |
||
| 315 | */ |
||
| 316 | private function getEncryptedKey(array $complete_headers, $cek, Algorithm\KeyEncryptionAlgorithmInterface $key_encryption_algorithm, Algorithm\ContentEncryptionAlgorithmInterface $content_encryption_algorithm, array &$additional_headers, Object\JWKInterface $recipient_key) |
||
| 326 | |||
| 327 | /** |
||
| 328 | * @param array $complete_headers |
||
| 329 | * @param string $cek |
||
| 330 | * @param \Jose\Algorithm\KeyEncryption\KeyAgreementWrappingInterface $key_encryption_algorithm |
||
| 331 | * @param \Jose\Algorithm\ContentEncryptionAlgorithmInterface $content_encryption_algorithm |
||
| 332 | * @param array $additional_headers |
||
| 333 | * @param \Jose\Object\JWKInterface $recipient_key |
||
| 334 | * |
||
| 335 | * @return string |
||
| 336 | */ |
||
| 337 | private function getEncryptedKeyFromKeyAgreementAndKeyWrappingAlgorithm(array $complete_headers, $cek, Algorithm\KeyEncryption\KeyAgreementWrappingInterface $key_encryption_algorithm, Algorithm\ContentEncryptionAlgorithmInterface $content_encryption_algorithm, array &$additional_headers, Object\JWKInterface $recipient_key) |
||
| 343 | |||
| 344 | /** |
||
| 345 | * @param array $complete_headers |
||
| 346 | * @param string $cek |
||
| 347 | * @param \Jose\Algorithm\KeyEncryption\KeyEncryptionInterface $key_encryption_algorithm |
||
| 348 | * @param \Jose\Object\JWKInterface $recipient_key |
||
| 349 | * @param array $additional_headers |
||
| 350 | * |
||
| 351 | * @return string |
||
| 352 | */ |
||
| 353 | private function getEncryptedKeyFromKeyEncryptionAlgorithm(array $complete_headers, $cek, Algorithm\KeyEncryption\KeyEncryptionInterface $key_encryption_algorithm, Object\JWKInterface $recipient_key, array &$additional_headers) |
||
| 357 | |||
| 358 | /** |
||
| 359 | * @param array $complete_headers |
||
| 360 | * @param string $cek |
||
| 361 | * @param \Jose\Algorithm\KeyEncryption\KeyWrappingInterface $key_encryption_algorithm |
||
| 362 | * @param \Jose\Object\JWKInterface $recipient_key |
||
| 363 | * @param array $additional_headers |
||
| 364 | * |
||
| 365 | * @return string |
||
| 366 | */ |
||
| 367 | private function getEncryptedKeyFromKeyWrappingAlgorithm(array $complete_headers, $cek, Algorithm\KeyEncryption\KeyWrappingInterface $key_encryption_algorithm, Object\JWKInterface $recipient_key, &$additional_headers) |
||
| 371 | |||
| 372 | /** |
||
| 373 | * @param array $complete_headers |
||
| 374 | * |
||
| 375 | * @return \Jose\Algorithm\KeyEncryptionAlgorithmInterface |
||
| 376 | */ |
||
| 377 | private function findKeyEncryptionAlgorithm(array $complete_headers) |
||
| 385 | |||
| 386 | /** |
||
| 387 | * @param int $size |
||
| 388 | * |
||
| 389 | * @return string |
||
| 390 | */ |
||
| 391 | private function createCEK($size) |
||
| 395 | |||
| 396 | /** |
||
| 397 | * @param int $size |
||
| 398 | * |
||
| 399 | * @return string |
||
| 400 | */ |
||
| 401 | private function createIV($size) |
||
| 405 | } |
||
| 406 |
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: