Complex classes like JWELoader 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 JWELoader, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 36 | final class JWELoader |
||
| 37 | { |
||
| 38 | /** |
||
| 39 | * @var HeaderCheckerManager |
||
| 40 | */ |
||
| 41 | private $headerCheckerManager; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var AlgorithmManager |
||
| 45 | */ |
||
| 46 | private $keyEncryptionAlgorithmManager; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var AlgorithmManager |
||
| 50 | */ |
||
| 51 | private $contentEncryptionAlgorithmManager; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var CompressionMethodManager |
||
| 55 | */ |
||
| 56 | private $compressionMethodManager; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var JWESerializerManager |
||
| 60 | */ |
||
| 61 | private $serializerManager; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * JWELoader constructor. |
||
| 65 | * |
||
| 66 | * @param AlgorithmManager $keyEncryptionAlgorithmManager |
||
| 67 | * @param AlgorithmManager $contentEncryptionAlgorithmManager |
||
| 68 | * @param CompressionMethodManager $compressionMethodManager |
||
| 69 | * @param HeaderCheckerManager $headerCheckerManager |
||
| 70 | * @param JWESerializerManager $serializerManager |
||
| 71 | */ |
||
| 72 | public function __construct(AlgorithmManager $keyEncryptionAlgorithmManager, AlgorithmManager $contentEncryptionAlgorithmManager, CompressionMethodManager $compressionMethodManager, HeaderCheckerManager $headerCheckerManager, JWESerializerManager $serializerManager) |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @param string $input |
||
| 83 | * @param string|null $serializer |
||
| 84 | * |
||
| 85 | * @return JWE |
||
| 86 | */ |
||
| 87 | public function load(string $input, ?string &$serializer = null): JWE |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @return AlgorithmManager |
||
| 94 | */ |
||
| 95 | public function getKeyEncryptionAlgorithmManager(): AlgorithmManager |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @return AlgorithmManager |
||
| 102 | */ |
||
| 103 | public function getContentEncryptionAlgorithmManager(): AlgorithmManager |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @return CompressionMethodManager |
||
| 110 | */ |
||
| 111 | public function getCompressionMethodManager(): CompressionMethodManager |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @param JWE $jwe A JWE object to decrypt |
||
| 118 | * @param JWK $jwk The key used to decrypt the input |
||
| 119 | * @param null|int $recipientIndex If the JWE has been decrypted, an integer that represents the ID of the recipient is set |
||
| 120 | * |
||
| 121 | * @return JWE |
||
| 122 | */ |
||
| 123 | public function decryptUsingKey(JWE $jwe, JWK $jwk, ?int &$recipientIndex = null): JWE |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @param JWE $jwe A JWE object to decrypt |
||
| 133 | * @param JWKSet $jwkset The key set used to decrypt the input |
||
| 134 | * @param null|int $recipientIndex If the JWE has been decrypted, an integer that represents the ID of the recipient is set |
||
| 135 | * |
||
| 136 | * @return JWE |
||
| 137 | */ |
||
| 138 | public function decryptUsingKeySet(JWE $jwe, JWKSet $jwkset, ?int &$recipientIndex = null): JWE |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @param JWE $jwe |
||
| 165 | * @param JWKSet $jwkset |
||
| 166 | * @param int $i |
||
| 167 | * |
||
| 168 | * @return string|null |
||
| 169 | */ |
||
| 170 | private function decryptRecipientKey(JWE $jwe, JWKSet $jwkset, int $i): ?string |
||
| 199 | |||
| 200 | /** |
||
| 201 | * @param JWE $jwe |
||
| 202 | */ |
||
| 203 | private function checkRecipients(JWE $jwe) |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @param JWE $jwe |
||
| 212 | */ |
||
| 213 | private function checkPayload(JWE $jwe) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @param JWKSet $jwkset |
||
| 222 | */ |
||
| 223 | private function checkJWKSet(JWKSet $jwkset) |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @param AlgorithmInterface $key_encryption_algorithm |
||
| 232 | * @param ContentEncryptionAlgorithmInterface $content_encryption_algorithm |
||
| 233 | * @param JWK $key |
||
| 234 | * @param Recipient $recipient |
||
| 235 | * @param array $complete_headers |
||
| 236 | * |
||
| 237 | * @return null|string |
||
| 238 | */ |
||
| 239 | private function decryptCEK(AlgorithmInterface $key_encryption_algorithm, ContentEncryptionAlgorithmInterface $content_encryption_algorithm, JWK $key, Recipient $recipient, array $complete_headers): ?string |
||
| 255 | |||
| 256 | /** |
||
| 257 | * @param JWE $jwe |
||
| 258 | * @param string $cek |
||
| 259 | * @param ContentEncryptionAlgorithmInterface $content_encryption_algorithm |
||
| 260 | * @param array $complete_headers |
||
| 261 | * |
||
| 262 | * @return string |
||
| 263 | */ |
||
| 264 | private function decryptPayload(JWE $jwe, string $cek, ContentEncryptionAlgorithmInterface $content_encryption_algorithm, array $complete_headers): string |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @param string $payload |
||
| 276 | * @param array $complete_headers |
||
| 277 | * |
||
| 278 | * @return string |
||
| 279 | */ |
||
| 280 | private function decompressIfNeeded(string $payload, array $complete_headers): string |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @param array $complete_headers |
||
| 295 | * |
||
| 296 | * @throws \InvalidArgumentException |
||
| 297 | */ |
||
| 298 | private function checkCompleteHeader(array $complete_headers) |
||
| 306 | |||
| 307 | /** |
||
| 308 | * @param array $complete_headers |
||
| 309 | * |
||
| 310 | * @return KeyEncryptionAlgorithmInterface |
||
| 311 | */ |
||
| 312 | private function getKeyEncryptionAlgorithm(array $complete_headers): KeyEncryptionAlgorithmInterface |
||
| 321 | |||
| 322 | /** |
||
| 323 | * @param array $complete_headers |
||
| 324 | * |
||
| 325 | * @return ContentEncryptionAlgorithmInterface |
||
| 326 | */ |
||
| 327 | private function getContentEncryptionAlgorithm(array $complete_headers): ContentEncryptionAlgorithmInterface |
||
| 336 | } |
||
| 337 |
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: