Complex classes like JoseCollector 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 JoseCollector, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 35 | final class JoseCollector extends DataCollector |
||
| 36 | { |
||
| 37 | /** |
||
| 38 | * @var AlgorithmManagerFactory |
||
| 39 | */ |
||
| 40 | private $algorithmManagerFactory; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var CompressionMethodManagerFactory|null |
||
| 44 | */ |
||
| 45 | private $compressionMethodManagerFactory; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var JWSSerializerManagerFactory|null |
||
| 49 | */ |
||
| 50 | private $jwsSerializerManagerFactory; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var JWESerializerManagerFactory|null |
||
| 54 | */ |
||
| 55 | private $jweSerializerManagerFactory; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var KeyAnalyzerManager|null |
||
| 59 | */ |
||
| 60 | private $jwkAnalyzerManager; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * JoseCollector constructor. |
||
| 64 | * |
||
| 65 | * @param AlgorithmManagerFactory $algorithmManagerFactory |
||
| 66 | * @param CompressionMethodManagerFactory|null $compressionMethodManagerFactory |
||
| 67 | * @param JWSSerializerManagerFactory|null $jwsSerializerManagerFactory |
||
| 68 | * @param JWESerializerManagerFactory|null $jweSerializerManagerFactory |
||
| 69 | * @param KeyAnalyzerManager|null $jwkAnalyzerManager |
||
| 70 | */ |
||
| 71 | public function __construct(AlgorithmManagerFactory $algorithmManagerFactory, ?CompressionMethodManagerFactory $compressionMethodManagerFactory = null, ?JWSSerializerManagerFactory $jwsSerializerManagerFactory = null, ?JWESerializerManagerFactory $jweSerializerManagerFactory = null, ?KeyAnalyzerManager $jwkAnalyzerManager) |
||
| 80 | |||
| 81 | /** |
||
| 82 | * {@inheritdoc} |
||
| 83 | */ |
||
| 84 | public function collect(Request $request, Response $response, \Exception $exception = null) |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @return array |
||
| 100 | */ |
||
| 101 | public function getAlgorithmDetails(): array |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @return int |
||
| 108 | */ |
||
| 109 | public function countSignatureAlgorithms(): int |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @return int |
||
| 116 | */ |
||
| 117 | public function countKeyEncryptionAlgorithms(): int |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @return int |
||
| 124 | */ |
||
| 125 | public function countContentEncryptionAlgorithms(): int |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @return array |
||
| 132 | */ |
||
| 133 | public function getCompressionMethodDetails(): array |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @return array |
||
| 140 | */ |
||
| 141 | public function getJWSSerializationDetails(): array |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @return array |
||
| 148 | */ |
||
| 149 | public function getJWESerializationDetails(): array |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @return array |
||
| 156 | */ |
||
| 157 | public function getJWKs(): array |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @return array |
||
| 164 | */ |
||
| 165 | public function getJWKSets(): array |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @return array |
||
| 172 | */ |
||
| 173 | public function getJWSBuilders(): array |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @return array |
||
| 180 | */ |
||
| 181 | public function getJWSVerifiers(): array |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @return array |
||
| 188 | */ |
||
| 189 | public function getJWEBuilders(): array |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @return array |
||
| 196 | */ |
||
| 197 | public function getJWEDecrypters(): array |
||
| 201 | |||
| 202 | /** |
||
| 203 | * {@inheritdoc} |
||
| 204 | */ |
||
| 205 | public function getName() |
||
| 209 | |||
| 210 | private function collectSupportedAlgorithms() |
||
| 233 | |||
| 234 | /** |
||
| 235 | * @param Algorithm $algorithm |
||
| 236 | * @param int $signatureAlgorithms |
||
| 237 | * @param int $keyEncryptionAlgorithms |
||
| 238 | * @param int $contentEncryptionAlgorithms |
||
| 239 | * |
||
| 240 | * @return string |
||
| 241 | */ |
||
| 242 | private function getAlgorithmType(Algorithm $algorithm, int &$signatureAlgorithms, int &$keyEncryptionAlgorithms, int &$contentEncryptionAlgorithms): string |
||
| 261 | |||
| 262 | private function collectSupportedCompressionMethods() |
||
| 273 | |||
| 274 | private function collectSupportedJWSSerializations() |
||
| 285 | |||
| 286 | private function collectSupportedJWESerializations() |
||
| 297 | |||
| 298 | private function collectSupportedJWSBuilders() |
||
| 307 | |||
| 308 | private function collectSupportedJWSVerifiers() |
||
| 317 | |||
| 318 | private function collectSupportedJWEBuilders() |
||
| 329 | |||
| 330 | private function collectSupportedJWEDecrypters() |
||
| 341 | |||
| 342 | private function collectJWK() |
||
| 352 | |||
| 353 | private function collectJWKSet() |
||
| 369 | |||
| 370 | /** |
||
| 371 | * @var JWSBuilder[] |
||
| 372 | */ |
||
| 373 | private $jwsBuilders = []; |
||
| 374 | |||
| 375 | /** |
||
| 376 | * @param string $id |
||
| 377 | * @param JWSBuilder $jwsBuilder |
||
| 378 | */ |
||
| 379 | public function addJWSBuilder(string $id, JWSBuilder $jwsBuilder) |
||
| 383 | |||
| 384 | /** |
||
| 385 | * @var JWSVerifier[] |
||
| 386 | */ |
||
| 387 | private $jwsVerifiers = []; |
||
| 388 | |||
| 389 | /** |
||
| 390 | * @param string $id |
||
| 391 | * @param JWSVerifier $jwsVerifier |
||
| 392 | */ |
||
| 393 | public function addJWSVerifier(string $id, JWSVerifier $jwsVerifier) |
||
| 397 | |||
| 398 | /** |
||
| 399 | * @var JWEBuilder[] |
||
| 400 | */ |
||
| 401 | private $jweBuilders = []; |
||
| 402 | |||
| 403 | /** |
||
| 404 | * @param string $id |
||
| 405 | * @param JWEBuilder $jweBuilder |
||
| 406 | */ |
||
| 407 | public function addJWEBuilder(string $id, JWEBuilder $jweBuilder) |
||
| 411 | |||
| 412 | /** |
||
| 413 | * @var JWEDecrypter[] |
||
| 414 | */ |
||
| 415 | private $jweDecrypters = []; |
||
| 416 | |||
| 417 | /** |
||
| 418 | * @param string $id |
||
| 419 | * @param JWEDecrypter $jweDecrypter |
||
| 420 | */ |
||
| 421 | public function addJWEDecrypter(string $id, JWEDecrypter $jweDecrypter) |
||
| 425 | |||
| 426 | /** |
||
| 427 | * @var JWK[] |
||
| 428 | */ |
||
| 429 | private $jwks = []; |
||
| 430 | |||
| 431 | /** |
||
| 432 | * @param string $id |
||
| 433 | * @param JWK $jwk |
||
| 434 | */ |
||
| 435 | public function addJWK(string $id, JWK $jwk) |
||
| 439 | |||
| 440 | /** |
||
| 441 | * @var JWKSet[] |
||
| 442 | */ |
||
| 443 | private $jwksets = []; |
||
| 444 | |||
| 445 | /** |
||
| 446 | * @param string $id |
||
| 447 | * @param JWKSet $jwkset |
||
| 448 | */ |
||
| 449 | public function addJWKSet(string $id, JWKSet $jwkset) |
||
| 453 | } |
||
| 454 |
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: