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 | protected function _processCertificate(ValidatorState $state, |
|
| 146 | |||
| 147 | /** |
||
| 148 | * Apply preparation for certificate i+1 according to rfc5280 section 6.1.4. |
||
| 149 | * |
||
| 150 | * @link https://tools.ietf.org/html/rfc5280#section-6.1.4 |
||
| 151 | * @param ValidatorState $state |
||
| 152 | * @param Certificate $cert |
||
| 153 | * @return ValidatorState |
||
| 154 | */ |
||
| 155 | 22 | protected function _prepareNext(ValidatorState $state, Certificate $cert) { |
|
| 237 | |||
| 238 | /** |
||
| 239 | * Apply wrap-up procedure according to RFC 5280 section 6.1.5. |
||
| 240 | * |
||
| 241 | * @link https://tools.ietf.org/html/rfc5280#section-6.1.5 |
||
| 242 | * @param ValidatorState $state |
||
| 243 | * @param Certificate $cert |
||
| 244 | * @throws PathValidationException |
||
| 245 | */ |
||
| 246 | 12 | protected function _wrapUp(ValidatorState $state, Certificate $cert) { |
|
| 273 | |||
| 274 | /** |
||
| 275 | * Update working_public_key, working_public_key_parameters and |
||
| 276 | * working_public_key_algorithm state variables from certificate. |
||
| 277 | * |
||
| 278 | * @param ValidatorState $state |
||
| 279 | * @param Certificate $cert |
||
| 280 | * @return ValidatorState |
||
| 281 | */ |
||
| 282 | 21 | protected function _setPublicKeyState(ValidatorState $state, |
|
| 304 | |||
| 305 | /** |
||
| 306 | * Verify certificate signature. |
||
| 307 | * |
||
| 308 | * @param ValidatorState $state |
||
| 309 | * @param Certificate $cert |
||
| 310 | * @throws PathValidationException |
||
| 311 | */ |
||
| 312 | 23 | protected function _verifySignature(ValidatorState $state, Certificate $cert) { |
|
| 318 | |||
| 319 | /** |
||
| 320 | * Check certificate validity. |
||
| 321 | * |
||
| 322 | * @param Certificate $cert |
||
| 323 | * @throws PathValidationException |
||
| 324 | */ |
||
| 325 | 23 | protected function _checkValidity(Certificate $cert) { |
|
| 339 | |||
| 340 | /** |
||
| 341 | * Check certificate revocation. |
||
| 342 | * |
||
| 343 | * @param Certificate $cert |
||
| 344 | */ |
||
| 345 | 22 | protected function _checkRevocation(Certificate $cert) { |
|
| 348 | |||
| 349 | /** |
||
| 350 | * Check certificate issuer. |
||
| 351 | * |
||
| 352 | * @param ValidatorState $state |
||
| 353 | * @param Certificate $cert |
||
| 354 | * @throws PathValidationException |
||
| 355 | */ |
||
| 356 | 22 | protected function _checkIssuer(ValidatorState $state, Certificate $cert) { |
|
| 363 | |||
| 364 | /** |
||
| 365 | * |
||
| 366 | * @param ValidatorState $state |
||
| 367 | * @param Certificate $cert |
||
| 368 | */ |
||
| 369 | 15 | protected function _checkPermittedSubtrees(ValidatorState $state, |
|
| 374 | |||
| 375 | /** |
||
| 376 | * |
||
| 377 | * @param ValidatorState $state |
||
| 378 | * @param Certificate $cert |
||
| 379 | */ |
||
| 380 | 15 | protected function _checkExcludedSubtrees(ValidatorState $state, |
|
| 385 | |||
| 386 | /** |
||
| 387 | * |
||
| 388 | * @param ValidatorState $state |
||
| 389 | * @param Certificate $cert |
||
| 390 | * @return ValidatorState |
||
| 391 | */ |
||
| 392 | 6 | protected function _processPolicyInformation(ValidatorState $state, |
|
| 397 | |||
| 398 | /** |
||
| 399 | * |
||
| 400 | * @param ValidatorState $state |
||
| 401 | * @param Certificate $cert |
||
| 402 | * @return ValidatorState |
||
| 403 | */ |
||
| 404 | 1 | protected function _processNameConstraints(ValidatorState $state, |
|
| 409 | |||
| 410 | /** |
||
| 411 | * Process basic constraints extension. |
||
| 412 | * |
||
| 413 | * @param Certificate $cert |
||
| 414 | * @throws PathValidationException |
||
| 415 | */ |
||
| 416 | 21 | protected function _processBasicContraints(Certificate $cert) { |
|
| 430 | |||
| 431 | /** |
||
| 432 | * Process pathLenConstraint. |
||
| 433 | * |
||
| 434 | * @param ValidatorState $state |
||
| 435 | * @param Certificate $cert |
||
| 436 | * @return ValidatorState |
||
| 437 | */ |
||
| 438 | 19 | protected function _processPathLengthContraint(ValidatorState $state, |
|
| 451 | |||
| 452 | /** |
||
| 453 | * Process policy mappings extension. |
||
| 454 | * |
||
| 455 | * @param ValidatorState $state |
||
| 456 | * @param Certificate $cert |
||
| 457 | * @return ValidatorState |
||
| 458 | */ |
||
| 459 | 2 | protected function _processPolicyMappings(ValidatorState $state, |
|
| 464 | |||
| 465 | /** |
||
| 466 | * |
||
| 467 | * @param ValidatorState $state |
||
| 468 | * @param Certificate $cert |
||
| 469 | * @return ValidatorState |
||
| 470 | */ |
||
| 471 | 18 | protected function _processExtensions(ValidatorState $state, |
|
| 476 | |||
| 477 | /** |
||
| 478 | * |
||
| 479 | * @param ValidatorState $state |
||
| 480 | * @return ValidatorState |
||
| 481 | */ |
||
| 482 | 12 | protected function _calculatePolicyIntersection(ValidatorState $state) { |
|
| 486 | } |
||
| 487 |
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: