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, |
|
66 | |||
67 | /** |
||
68 | * Validate certification path. |
||
69 | * |
||
70 | * @throws PathValidationException |
||
71 | * @return PathValidationResult |
||
72 | */ |
||
73 | 23 | public function validate() { |
|
98 | |||
99 | /** |
||
100 | * Apply basic certificate processing according to RFC 5280 section 6.1.3. |
||
101 | * |
||
102 | * @link https://tools.ietf.org/html/rfc5280#section-6.1.3 |
||
103 | * @param ValidatorState $state |
||
104 | * @param Certificate $cert |
||
105 | * @throws PathValidationException |
||
106 | * @return ValidatorState |
||
107 | */ |
||
108 | 23 | protected function _processCertificate(ValidatorState $state, |
|
143 | |||
144 | /** |
||
145 | * Apply preparation for certificate i+1 according to rfc5280 section 6.1.4. |
||
146 | * |
||
147 | * @link https://tools.ietf.org/html/rfc5280#section-6.1.4 |
||
148 | * @param ValidatorState $state |
||
149 | * @param Certificate $cert |
||
150 | * @return ValidatorState |
||
151 | */ |
||
152 | 22 | protected function _prepareNext(ValidatorState $state, Certificate $cert) { |
|
234 | |||
235 | /** |
||
236 | * Apply wrap-up procedure according to RFC 5280 section 6.1.5. |
||
237 | * |
||
238 | * @link https://tools.ietf.org/html/rfc5280#section-6.1.5 |
||
239 | * @param ValidatorState $state |
||
240 | * @param Certificate $cert |
||
241 | * @throws PathValidationException |
||
242 | */ |
||
243 | 12 | protected function _wrapUp(ValidatorState $state, Certificate $cert) { |
|
270 | |||
271 | /** |
||
272 | * Update working_public_key, working_public_key_parameters and |
||
273 | * working_public_key_algorithm state variables from certificate. |
||
274 | * |
||
275 | * @param ValidatorState $state |
||
276 | * @param Certificate $cert |
||
277 | * @return ValidatorState |
||
278 | */ |
||
279 | 21 | protected function _setPublicKeyState(ValidatorState $state, |
|
301 | |||
302 | /** |
||
303 | * Verify certificate signature. |
||
304 | * |
||
305 | * @param ValidatorState $state |
||
306 | * @param Certificate $cert |
||
307 | * @throws PathValidationException |
||
308 | */ |
||
309 | 23 | protected function _verifySignature(ValidatorState $state, Certificate $cert) { |
|
315 | |||
316 | /** |
||
317 | * Check certificate validity. |
||
318 | * |
||
319 | * @param Certificate $cert |
||
320 | * @throws PathValidationException |
||
321 | */ |
||
322 | 23 | protected function _checkValidity(Certificate $cert) { |
|
336 | |||
337 | /** |
||
338 | * Check certificate revocation. |
||
339 | * |
||
340 | * @param Certificate $cert |
||
341 | */ |
||
342 | 22 | protected function _checkRevocation(Certificate $cert) { |
|
345 | |||
346 | /** |
||
347 | * Check certificate issuer. |
||
348 | * |
||
349 | * @param ValidatorState $state |
||
350 | * @param Certificate $cert |
||
351 | * @throws PathValidationException |
||
352 | */ |
||
353 | 22 | protected function _checkIssuer(ValidatorState $state, Certificate $cert) { |
|
360 | |||
361 | /** |
||
362 | * |
||
363 | * @param ValidatorState $state |
||
364 | * @param Certificate $cert |
||
365 | */ |
||
366 | 15 | protected function _checkPermittedSubtrees(ValidatorState $state, |
|
371 | |||
372 | /** |
||
373 | * |
||
374 | * @param ValidatorState $state |
||
375 | * @param Certificate $cert |
||
376 | */ |
||
377 | 15 | protected function _checkExcludedSubtrees(ValidatorState $state, |
|
382 | |||
383 | /** |
||
384 | * |
||
385 | * @param ValidatorState $state |
||
386 | * @param Certificate $cert |
||
387 | * @return ValidatorState |
||
388 | */ |
||
389 | 6 | protected function _processPolicyInformation(ValidatorState $state, |
|
394 | |||
395 | /** |
||
396 | * |
||
397 | * @param ValidatorState $state |
||
398 | * @param Certificate $cert |
||
399 | * @return ValidatorState |
||
400 | */ |
||
401 | 1 | protected function _processNameConstraints(ValidatorState $state, |
|
406 | |||
407 | /** |
||
408 | * Process basic constraints extension. |
||
409 | * |
||
410 | * @param Certificate $cert |
||
411 | * @throws PathValidationException |
||
412 | */ |
||
413 | 21 | protected function _processBasicContraints(Certificate $cert) { |
|
427 | |||
428 | /** |
||
429 | * Process pathLenConstraint. |
||
430 | * |
||
431 | * @param ValidatorState $state |
||
432 | * @param Certificate $cert |
||
433 | * @return ValidatorState |
||
434 | */ |
||
435 | 19 | protected function _processPathLengthContraint(ValidatorState $state, |
|
448 | |||
449 | /** |
||
450 | * Process policy mappings extension. |
||
451 | * |
||
452 | * @param ValidatorState $state |
||
453 | * @param Certificate $cert |
||
454 | * @return ValidatorState |
||
455 | */ |
||
456 | 2 | protected function _processPolicyMappings(ValidatorState $state, |
|
461 | |||
462 | /** |
||
463 | * |
||
464 | * @param ValidatorState $state |
||
465 | * @param Certificate $cert |
||
466 | * @return ValidatorState |
||
467 | */ |
||
468 | 18 | protected function _processExtensions(ValidatorState $state, |
|
473 | |||
474 | /** |
||
475 | * |
||
476 | * @param ValidatorState $state |
||
477 | * @return ValidatorState |
||
478 | */ |
||
479 | 12 | protected function _calculatePolicyIntersection(ValidatorState $state) { |
|
483 | } |
||
484 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.