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 |
||
| 18 | class PathValidator |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * Crypto engine. |
||
| 22 | * |
||
| 23 | * @var Crypto $_crypto |
||
| 24 | */ |
||
| 25 | protected $_crypto; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Path validation configuration. |
||
| 29 | * |
||
| 30 | * @var PathValidationConfig $_config |
||
| 31 | */ |
||
| 32 | protected $_config; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Certification path. |
||
| 36 | * |
||
| 37 | * @var Certificate[] $_certificates |
||
| 38 | */ |
||
| 39 | protected $_certificates; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Certification path trust anchor. |
||
| 43 | * |
||
| 44 | * @var Certificate $_trustAnchor |
||
| 45 | */ |
||
| 46 | protected $_trustAnchor; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Constructor. |
||
| 50 | * |
||
| 51 | * @param Crypto $crypto Crypto engine |
||
| 52 | * @param PathValidationConfig $config Validation config |
||
| 53 | * @param Certificate ...$certificates Certificates from the trust anchor to |
||
| 54 | * the end-entity certificate |
||
| 55 | */ |
||
| 56 | 46 | public function __construct(Crypto $crypto, PathValidationConfig $config, |
|
| 72 | |||
| 73 | /** |
||
| 74 | * Validate certification path. |
||
| 75 | * |
||
| 76 | * @throws PathValidationException |
||
| 77 | * @return PathValidationResult |
||
| 78 | */ |
||
| 79 | 45 | public function validate(): PathValidationResult |
|
| 102 | |||
| 103 | /** |
||
| 104 | * Apply basic certificate processing according to RFC 5280 section 6.1.3. |
||
| 105 | * |
||
| 106 | * @link https://tools.ietf.org/html/rfc5280#section-6.1.3 |
||
| 107 | * @param ValidatorState $state |
||
| 108 | * @param Certificate $cert |
||
| 109 | * @throws PathValidationException |
||
| 110 | * @return ValidatorState |
||
| 111 | */ |
||
| 112 | 44 | private function _processCertificate(ValidatorState $state, Certificate $cert): ValidatorState |
|
| 148 | |||
| 149 | /** |
||
| 150 | * Apply preparation for the certificate i+1 according to rfc5280 section |
||
| 151 | * 6.1.4. |
||
| 152 | * |
||
| 153 | * @link https://tools.ietf.org/html/rfc5280#section-6.1.4 |
||
| 154 | * @param ValidatorState $state |
||
| 155 | * @param Certificate $cert |
||
| 156 | * @return ValidatorState |
||
| 157 | */ |
||
| 158 | 43 | private function _prepareNext(ValidatorState $state, Certificate $cert): ValidatorState |
|
| 190 | |||
| 191 | /** |
||
| 192 | * Apply wrap-up procedure according to RFC 5280 section 6.1.5. |
||
| 193 | * |
||
| 194 | * @link https://tools.ietf.org/html/rfc5280#section-6.1.5 |
||
| 195 | * @param ValidatorState $state |
||
| 196 | * @param Certificate $cert |
||
| 197 | * @throws PathValidationException |
||
| 198 | * @return ValidatorState |
||
| 199 | */ |
||
| 200 | 30 | private function _wrapUp(ValidatorState $state, Certificate $cert): ValidatorState |
|
| 229 | |||
| 230 | /** |
||
| 231 | * Update working_public_key, working_public_key_parameters and |
||
| 232 | * working_public_key_algorithm state variables from certificate. |
||
| 233 | * |
||
| 234 | * @param ValidatorState $state |
||
| 235 | * @param Certificate $cert |
||
| 236 | * @return ValidatorState |
||
| 237 | */ |
||
| 238 | 42 | private function _setPublicKeyState(ValidatorState $state, Certificate $cert): ValidatorState |
|
| 260 | |||
| 261 | /** |
||
| 262 | * Verify certificate signature. |
||
| 263 | * |
||
| 264 | * @param ValidatorState $state |
||
| 265 | * @param Certificate $cert |
||
| 266 | * @throws PathValidationException |
||
| 267 | */ |
||
| 268 | 44 | private function _verifySignature(ValidatorState $state, Certificate $cert) |
|
| 281 | |||
| 282 | /** |
||
| 283 | * Check certificate validity. |
||
| 284 | * |
||
| 285 | * @param Certificate $cert |
||
| 286 | * @throws PathValidationException |
||
| 287 | */ |
||
| 288 | 44 | private function _checkValidity(Certificate $cert) |
|
| 303 | |||
| 304 | /** |
||
| 305 | * Check certificate revocation. |
||
| 306 | * |
||
| 307 | * @param Certificate $cert |
||
| 308 | */ |
||
| 309 | 43 | private function _checkRevocation(Certificate $cert) |
|
| 313 | |||
| 314 | /** |
||
| 315 | * Check certificate issuer. |
||
| 316 | * |
||
| 317 | * @param ValidatorState $state |
||
| 318 | * @param Certificate $cert |
||
| 319 | * @throws PathValidationException |
||
| 320 | */ |
||
| 321 | 43 | private function _checkIssuer(ValidatorState $state, Certificate $cert) |
|
| 329 | |||
| 330 | /** |
||
| 331 | * |
||
| 332 | * @param ValidatorState $state |
||
| 333 | * @param Certificate $cert |
||
| 334 | */ |
||
| 335 | 35 | private function _checkPermittedSubtrees(ValidatorState $state, |
|
| 341 | |||
| 342 | /** |
||
| 343 | * |
||
| 344 | * @param ValidatorState $state |
||
| 345 | * @param Certificate $cert |
||
| 346 | */ |
||
| 347 | 35 | private function _checkExcludedSubtrees(ValidatorState $state, |
|
| 353 | |||
| 354 | /** |
||
| 355 | * Apply policy mappings handling for the preparation step. |
||
| 356 | * |
||
| 357 | * @param ValidatorState $state |
||
| 358 | * @param Certificate $cert |
||
| 359 | * @throws PathValidationException |
||
| 360 | * @return ValidatorState |
||
| 361 | */ |
||
| 362 | 43 | private function _preparePolicyMappings(ValidatorState $state, |
|
| 379 | |||
| 380 | /** |
||
| 381 | * Apply name constraints handling for the preparation step. |
||
| 382 | * |
||
| 383 | * @param ValidatorState $state |
||
| 384 | * @param Certificate $cert |
||
| 385 | * @return ValidatorState |
||
| 386 | */ |
||
| 387 | 42 | private function _prepareNameConstraints(ValidatorState $state, |
|
| 396 | |||
| 397 | /** |
||
| 398 | * Apply preparation for a non-self-signed certificate. |
||
| 399 | * |
||
| 400 | * @param ValidatorState $state |
||
| 401 | * @return ValidatorState |
||
| 402 | */ |
||
| 403 | 21 | private function _prepareNonSelfIssued(ValidatorState $state): ValidatorState |
|
| 420 | |||
| 421 | /** |
||
| 422 | * Apply policy constraints handling for the preparation step. |
||
| 423 | * |
||
| 424 | * @param ValidatorState $state |
||
| 425 | * @param Certificate $cert |
||
| 426 | * @return ValidatorState |
||
| 427 | */ |
||
| 428 | 42 | private function _preparePolicyConstraints(ValidatorState $state, |
|
| 448 | |||
| 449 | /** |
||
| 450 | * Apply inhibit any-policy handling for the preparation step. |
||
| 451 | * |
||
| 452 | * @param ValidatorState $state |
||
| 453 | * @param Certificate $cert |
||
| 454 | * @return ValidatorState |
||
| 455 | */ |
||
| 456 | 42 | private function _prepareInhibitAnyPolicy(ValidatorState $state, |
|
| 468 | |||
| 469 | /** |
||
| 470 | * Verify maximum certification path length for the preparation step. |
||
| 471 | * |
||
| 472 | * @param ValidatorState $state |
||
| 473 | * @param Certificate $cert |
||
| 474 | * @throws PathValidationException |
||
| 475 | * @return ValidatorState |
||
| 476 | */ |
||
| 477 | 40 | private function _verifyMaxPathLength(ValidatorState $state, |
|
| 489 | |||
| 490 | /** |
||
| 491 | * Check key usage extension for the preparation step. |
||
| 492 | * |
||
| 493 | * @param Certificate $cert |
||
| 494 | * @throws PathValidationException |
||
| 495 | */ |
||
| 496 | 40 | private function _checkKeyUsage(Certificate $cert) |
|
| 506 | |||
| 507 | /** |
||
| 508 | * |
||
| 509 | * @param ValidatorState $state |
||
| 510 | * @param Certificate $cert |
||
| 511 | * @return ValidatorState |
||
| 512 | */ |
||
| 513 | 1 | private function _processNameConstraints(ValidatorState $state, |
|
| 519 | |||
| 520 | /** |
||
| 521 | * Process basic constraints extension. |
||
| 522 | * |
||
| 523 | * @param Certificate $cert |
||
| 524 | * @throws PathValidationException |
||
| 525 | */ |
||
| 526 | 42 | private function _processBasicContraints(Certificate $cert) |
|
| 541 | |||
| 542 | /** |
||
| 543 | * Process pathLenConstraint. |
||
| 544 | * |
||
| 545 | * @param ValidatorState $state |
||
| 546 | * @param Certificate $cert |
||
| 547 | * @return ValidatorState |
||
| 548 | */ |
||
| 549 | 40 | private function _processPathLengthContraint(ValidatorState $state, |
|
| 563 | |||
| 564 | /** |
||
| 565 | * |
||
| 566 | * @param ValidatorState $state |
||
| 567 | * @param Certificate $cert |
||
| 568 | * @return ValidatorState |
||
| 569 | */ |
||
| 570 | 39 | private function _processExtensions(ValidatorState $state, Certificate $cert): ValidatorState |
|
| 575 | |||
| 576 | /** |
||
| 577 | * |
||
| 578 | * @param ValidatorState $state |
||
| 579 | * @return ValidatorState |
||
| 580 | */ |
||
| 581 | 30 | private function _calculatePolicyIntersection(ValidatorState $state): ValidatorState |
|
| 601 | } |
||
| 602 |
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: