Complex classes like PathValidator 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 PathValidator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class PathValidator |
||
18 | { |
||
19 | /** |
||
20 | * Crypto engine. |
||
21 | * |
||
22 | * @var Crypto $_crypto |
||
23 | */ |
||
24 | protected $_crypto; |
||
25 | |||
26 | /** |
||
27 | * Path validation configuration. |
||
28 | * |
||
29 | * @var PathValidationConfig $_config |
||
30 | */ |
||
31 | protected $_config; |
||
32 | |||
33 | /** |
||
34 | * Certification path. |
||
35 | * |
||
36 | * @var Certificate[] $_certificates |
||
37 | */ |
||
38 | protected $_certificates; |
||
39 | |||
40 | /** |
||
41 | * Certification path trust anchor. |
||
42 | * |
||
43 | * @var Certificate $_trustAnchor |
||
44 | */ |
||
45 | protected $_trustAnchor; |
||
46 | |||
47 | /** |
||
48 | * Constructor |
||
49 | * |
||
50 | * @param Crypto $crypto |
||
51 | * @param PathValidationConfig $config |
||
52 | * @param Certificate ...$certificates |
||
53 | */ |
||
54 | 23 | public function __construct(Crypto $crypto, PathValidationConfig $config, |
|
69 | |||
70 | /** |
||
71 | * Validate certification path. |
||
72 | * |
||
73 | * @throws PathValidationException |
||
74 | * @return PathValidationResult |
||
75 | */ |
||
76 | 23 | public function validate() { |
|
101 | |||
102 | /** |
||
103 | * Apply basic certificate processing according to RFC 5280 section 6.1.3. |
||
104 | * |
||
105 | * @link https://tools.ietf.org/html/rfc5280#section-6.1.3 |
||
106 | * @param ValidatorState $state |
||
107 | * @param Certificate $cert |
||
108 | * @throws PathValidationException |
||
109 | * @return ValidatorState |
||
110 | */ |
||
111 | 23 | private function _processCertificate(ValidatorState $state, |
|
146 | |||
147 | /** |
||
148 | * Apply preparation for the certificate i+1 according to rfc5280 section |
||
149 | * 6.1.4. |
||
150 | * |
||
151 | * @link https://tools.ietf.org/html/rfc5280#section-6.1.4 |
||
152 | * @param ValidatorState $state |
||
153 | * @param Certificate $cert |
||
154 | * @return ValidatorState |
||
155 | */ |
||
156 | 22 | private function _prepareNext(ValidatorState $state, Certificate $cert) { |
|
187 | |||
188 | /** |
||
189 | * Apply wrap-up procedure according to RFC 5280 section 6.1.5. |
||
190 | * |
||
191 | * @link https://tools.ietf.org/html/rfc5280#section-6.1.5 |
||
192 | * @param ValidatorState $state |
||
193 | * @param Certificate $cert |
||
194 | * @throws PathValidationException |
||
195 | */ |
||
196 | 12 | private function _wrapUp(ValidatorState $state, Certificate $cert) { |
|
223 | |||
224 | /** |
||
225 | * Update working_public_key, working_public_key_parameters and |
||
226 | * working_public_key_algorithm state variables from certificate. |
||
227 | * |
||
228 | * @param ValidatorState $state |
||
229 | * @param Certificate $cert |
||
230 | * @return ValidatorState |
||
231 | */ |
||
232 | 21 | private function _setPublicKeyState(ValidatorState $state, Certificate $cert) { |
|
253 | |||
254 | /** |
||
255 | * Verify certificate signature. |
||
256 | * |
||
257 | * @param ValidatorState $state |
||
258 | * @param Certificate $cert |
||
259 | * @throws PathValidationException |
||
260 | */ |
||
261 | 23 | private function _verifySignature(ValidatorState $state, Certificate $cert) { |
|
267 | |||
268 | /** |
||
269 | * Check certificate validity. |
||
270 | * |
||
271 | * @param Certificate $cert |
||
272 | * @throws PathValidationException |
||
273 | */ |
||
274 | 23 | private function _checkValidity(Certificate $cert) { |
|
288 | |||
289 | /** |
||
290 | * Check certificate revocation. |
||
291 | * |
||
292 | * @param Certificate $cert |
||
293 | */ |
||
294 | 22 | private function _checkRevocation(Certificate $cert) { |
|
297 | |||
298 | /** |
||
299 | * Check certificate issuer. |
||
300 | * |
||
301 | * @param ValidatorState $state |
||
302 | * @param Certificate $cert |
||
303 | * @throws PathValidationException |
||
304 | */ |
||
305 | 22 | private function _checkIssuer(ValidatorState $state, Certificate $cert) { |
|
312 | |||
313 | /** |
||
314 | * |
||
315 | * @param ValidatorState $state |
||
316 | * @param Certificate $cert |
||
317 | */ |
||
318 | 15 | private function _checkPermittedSubtrees(ValidatorState $state, |
|
323 | |||
324 | /** |
||
325 | * |
||
326 | * @param ValidatorState $state |
||
327 | * @param Certificate $cert |
||
328 | */ |
||
329 | 15 | private function _checkExcludedSubtrees(ValidatorState $state, |
|
334 | |||
335 | /** |
||
336 | * |
||
337 | * @param ValidatorState $state |
||
338 | * @param Certificate $cert |
||
339 | * @return ValidatorState |
||
340 | */ |
||
341 | 6 | private function _processPolicyInformation(ValidatorState $state, |
|
346 | |||
347 | /** |
||
348 | * Apply policy mappings handling for the preparation step. |
||
349 | * |
||
350 | * @param ValidatorState $state |
||
351 | * @param Certificate $cert |
||
352 | * @throws PathValidationException |
||
353 | * @return ValidatorState |
||
354 | */ |
||
355 | 22 | private function _preparePolicyMappings(ValidatorState $state, |
|
368 | |||
369 | /** |
||
370 | * Apply name constraints handling for the preparation step. |
||
371 | * |
||
372 | * @param ValidatorState $state |
||
373 | * @param Certificate $cert |
||
374 | * @return ValidatorState |
||
375 | */ |
||
376 | 21 | private function _prepareNameConstraints(ValidatorState $state, |
|
384 | |||
385 | /** |
||
386 | * Apply preparation for a non-self-signed certificate. |
||
387 | * |
||
388 | * @param ValidatorState $state |
||
389 | * @return ValidatorState |
||
390 | */ |
||
391 | 6 | private function _prepareNonSelfIssued(ValidatorState $state) { |
|
407 | |||
408 | /** |
||
409 | * Apply policy constraints handling for the preparation step. |
||
410 | * |
||
411 | * @param ValidatorState $state |
||
412 | * @param Certificate $cert |
||
413 | * @return ValidatorState |
||
414 | */ |
||
415 | 21 | private function _preparePolicyConstraints(ValidatorState $state, |
|
434 | |||
435 | /** |
||
436 | * Apply inhibit any-policy handling for the preparation step. |
||
437 | * |
||
438 | * @param ValidatorState $state |
||
439 | * @param Certificate $cert |
||
440 | * @return ValidatorState |
||
441 | */ |
||
442 | 21 | private function _prepareInhibitAnyPolicy(ValidatorState $state, |
|
453 | |||
454 | /** |
||
455 | * Verify maximum certification path length for the preparation step. |
||
456 | * |
||
457 | * @param ValidatorState $state |
||
458 | * @param Certificate $cert |
||
459 | * @throws PathValidationException |
||
460 | * @return ValidatorState |
||
461 | */ |
||
462 | 19 | private function _verifyMaxPathLength(ValidatorState $state, |
|
473 | |||
474 | /** |
||
475 | * Check key usage extension for the preparation step. |
||
476 | * |
||
477 | * @param Certificate $cert |
||
478 | * @throws PathValidationException |
||
479 | */ |
||
480 | 19 | private function _checkKeyUsage(Certificate $cert) { |
|
489 | |||
490 | /** |
||
491 | * |
||
492 | * @param ValidatorState $state |
||
493 | * @param Certificate $cert |
||
494 | * @return ValidatorState |
||
495 | */ |
||
496 | 1 | private function _processNameConstraints(ValidatorState $state, |
|
501 | |||
502 | /** |
||
503 | * Process basic constraints extension. |
||
504 | * |
||
505 | * @param Certificate $cert |
||
506 | * @throws PathValidationException |
||
507 | */ |
||
508 | 21 | private function _processBasicContraints(Certificate $cert) { |
|
522 | |||
523 | /** |
||
524 | * Process pathLenConstraint. |
||
525 | * |
||
526 | * @param ValidatorState $state |
||
527 | * @param Certificate $cert |
||
528 | * @return ValidatorState |
||
529 | */ |
||
530 | 19 | private function _processPathLengthContraint(ValidatorState $state, |
|
543 | |||
544 | /** |
||
545 | * Process policy mappings extension. |
||
546 | * |
||
547 | * @param ValidatorState $state |
||
548 | * @param Certificate $cert |
||
549 | * @return ValidatorState |
||
550 | */ |
||
551 | 2 | private function _processPolicyMappings(ValidatorState $state, |
|
556 | |||
557 | /** |
||
558 | * |
||
559 | * @param ValidatorState $state |
||
560 | * @param Certificate $cert |
||
561 | * @return ValidatorState |
||
562 | */ |
||
563 | 18 | private function _processExtensions(ValidatorState $state, Certificate $cert) { |
|
567 | |||
568 | /** |
||
569 | * |
||
570 | * @param ValidatorState $state |
||
571 | * @return ValidatorState |
||
572 | */ |
||
573 | 12 | private function _calculatePolicyIntersection(ValidatorState $state) { |
|
577 | } |
||
578 |
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()
method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail()
, this method _has_ side-effects. In the following case, we could not remove the method call: