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 JWKAnalyzerManager|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 JWKAnalyzerManager|null $jwkAnalyzerManager |
||
70 | */ |
||
71 | public function __construct(AlgorithmManagerFactory $algorithmManagerFactory, ?CompressionMethodManagerFactory $compressionMethodManagerFactory = null, ?JWSSerializerManagerFactory $jwsSerializerManagerFactory = null, ?JWESerializerManagerFactory $jweSerializerManagerFactory = null, ?JWKAnalyzerManager $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 | * {@inheritdoc} |
||
156 | */ |
||
157 | public function getName() |
||
161 | |||
162 | private function collectSupportedAlgorithms() |
||
185 | |||
186 | /** |
||
187 | * @param AlgorithmInterface $algorithm |
||
188 | * @param int $signatureAlgorithms |
||
189 | * @param int $keyEncryptionAlgorithms |
||
190 | * @param int $contentEncryptionAlgorithms |
||
191 | * |
||
192 | * @return string |
||
193 | */ |
||
194 | private function getAlgorithmType(AlgorithmInterface $algorithm, int &$signatureAlgorithms, int &$keyEncryptionAlgorithms, int &$contentEncryptionAlgorithms): string |
||
213 | |||
214 | private function collectSupportedCompressionMethods() |
||
225 | |||
226 | private function collectSupportedJWSSerializations() |
||
237 | |||
238 | private function collectSupportedJWESerializations() |
||
249 | |||
250 | private function collectSupportedJWSBuilders() |
||
259 | |||
260 | private function collectSupportedJWSVerifiers() |
||
270 | |||
271 | private function collectSupportedJWEBuilders() |
||
283 | |||
284 | private function collectSupportedJWEDecrypters() |
||
296 | |||
297 | private function collectJWK() |
||
307 | |||
308 | private function collectJWKSet() |
||
325 | |||
326 | /** |
||
327 | * @var JWSBuilder[] |
||
328 | */ |
||
329 | private $jwsBuilders = []; |
||
330 | |||
331 | /** |
||
332 | * @param string $id |
||
333 | * @param JWSBuilder $jwsBuilder |
||
334 | */ |
||
335 | public function addJWSBuilder(string $id, JWSBuilder $jwsBuilder) |
||
339 | |||
340 | /** |
||
341 | * @var JWSVerifier[] |
||
342 | */ |
||
343 | private $jwsVerifiers = []; |
||
344 | |||
345 | /** |
||
346 | * @param string $id |
||
347 | * @param JWSVerifier $jwsVerifier |
||
348 | */ |
||
349 | public function addJWSVerifier(string $id, JWSVerifier $jwsVerifier) |
||
353 | |||
354 | /** |
||
355 | * @var JWEBuilder[] |
||
356 | */ |
||
357 | private $jweBuilders = []; |
||
358 | |||
359 | /** |
||
360 | * @param string $id |
||
361 | * @param JWEBuilder $jweBuilder |
||
362 | */ |
||
363 | public function addJWEBuilder(string $id, JWEBuilder $jweBuilder) |
||
367 | |||
368 | /** |
||
369 | * @var JWEDecrypter[] |
||
370 | */ |
||
371 | private $jweDecrypters = []; |
||
372 | |||
373 | /** |
||
374 | * @param string $id |
||
375 | * @param JWEDecrypter $jweDecrypter |
||
376 | */ |
||
377 | public function addJWEDecrypter(string $id, JWEDecrypter $jweDecrypter) |
||
381 | |||
382 | /** |
||
383 | * @var JWK[] |
||
384 | */ |
||
385 | private $jwks = []; |
||
386 | |||
387 | /** |
||
388 | * @param string $id |
||
389 | * @param JWK $jwk |
||
390 | */ |
||
391 | public function addJWK(string $id, JWK $jwk) |
||
395 | |||
396 | /** |
||
397 | * @var JWKSet[] |
||
398 | */ |
||
399 | private $jwksets = []; |
||
400 | |||
401 | /** |
||
402 | * @param string $id |
||
403 | * @param JWKSet $jwkset |
||
404 | */ |
||
405 | public function addJWKSet(string $id, JWKSet $jwkset) |
||
409 | } |
||
410 |
This check looks for the bodies of
if
statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.These
if
bodies can be removed. If you have an empty if but statements in theelse
branch, consider inverting the condition.could be turned into
This is much more concise to read.