Complex classes like HmacValidator 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 HmacValidator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 55 | class HmacValidator extends AbstractValidator |
||
| 56 | { |
||
| 57 | /** |
||
| 58 | * Request method vector |
||
| 59 | * |
||
| 60 | * @var null|array |
||
| 61 | */ |
||
| 62 | protected $methodVector = null; |
||
| 63 | /** |
||
| 64 | * Request submission times |
||
| 65 | * |
||
| 66 | * @var null|array |
||
| 67 | */ |
||
| 68 | protected $submissionTimes = null; |
||
| 69 | /** |
||
| 70 | * Previous request method extracted from valid HMAC |
||
| 71 | * |
||
| 72 | * @var string|null |
||
| 73 | */ |
||
| 74 | protected $previousMethod = null; |
||
| 75 | /** |
||
| 76 | * Previous validation result |
||
| 77 | * |
||
| 78 | * @var null|bool |
||
| 79 | */ |
||
| 80 | protected $validated = null; |
||
| 81 | /** |
||
| 82 | * Validation order position |
||
| 83 | * |
||
| 84 | * @var int |
||
| 85 | */ |
||
| 86 | const POSITION = 100; |
||
| 87 | /** |
||
| 88 | * GET request |
||
| 89 | * |
||
| 90 | * @var string |
||
| 91 | */ |
||
| 92 | const METHOD_GET = 'GET'; |
||
| 93 | /** |
||
| 94 | * POST request |
||
| 95 | * |
||
| 96 | * @var string |
||
| 97 | */ |
||
| 98 | const METHOD_POST = 'POST'; |
||
| 99 | /** |
||
| 100 | * Minimum submission time |
||
| 101 | * |
||
| 102 | * @var float |
||
| 103 | */ |
||
| 104 | const MINIMUM_SUBMISSION = 3; |
||
| 105 | /** |
||
| 106 | * Minimum submission time for follow-up submissions |
||
| 107 | * |
||
| 108 | * @var float |
||
| 109 | */ |
||
| 110 | const MINIMUM_FOLLOWUP_SUBMISSION = 1; |
||
| 111 | /** |
||
| 112 | * Maximum submission time |
||
| 113 | * |
||
| 114 | * @var float |
||
| 115 | */ |
||
| 116 | const MAXIMUM_SUBMISSION = 3600; |
||
| 117 | /** |
||
| 118 | * Block access |
||
| 119 | * |
||
| 120 | * @var string |
||
| 121 | */ |
||
| 122 | const BLOCK = 'BLOCK'; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Set the request method vector |
||
| 126 | * |
||
| 127 | * @param string $previous Previous request |
||
| 128 | * @param string $current Current request |
||
| 129 | */ |
||
| 130 | 2 | public function setMethodVector(string $previous = null, string $current = null): void |
|
| 141 | |||
| 142 | /** |
||
| 143 | * Sanitize and validate a request method |
||
| 144 | * |
||
| 145 | * @param string $method Request method |
||
| 146 | * |
||
| 147 | * @return string Validated request method |
||
| 148 | * @throws InvalidArgumentException If the request method is invalid |
||
| 149 | */ |
||
| 150 | 2 | protected function validateRequestMethod(string $method): string |
|
| 162 | |||
| 163 | /** |
||
| 164 | * Sanitize and set the submission times |
||
| 165 | * |
||
| 166 | * @param float $max Maximum submission time |
||
| 167 | * @param float $min Minimum submission time |
||
| 168 | * @param float|null $minFollowUp Minimum submission time for follow-up submissions |
||
| 169 | */ |
||
| 170 | 2 | public function setSubmissionTimes(float $max = null, float $min = null, float $minFollowUp = null): void |
|
| 185 | |||
| 186 | /** |
||
| 187 | * Validate a request |
||
| 188 | * |
||
| 189 | * @param ServerRequestInterface $request Request |
||
| 190 | * @param Antibot $antibot Antibot instance |
||
| 191 | * |
||
| 192 | * @return bool |
||
| 193 | * @throws HmacValidationException |
||
| 194 | * @throws SkippedValidationException If no Antibot data has been submitted |
||
| 195 | */ |
||
| 196 | 4 | public function validate(ServerRequestInterface $request, Antibot $antibot): bool |
|
| 220 | |||
| 221 | /** |
||
| 222 | * Create protective form HTML |
||
| 223 | * |
||
| 224 | * @param ServerRequestInterface $request Request |
||
| 225 | * @param Antibot $antibot Antibot instance |
||
| 226 | * |
||
| 227 | * @return InputElement[] HMTL input elements |
||
| 228 | */ |
||
| 229 | 4 | public function armor(ServerRequestInterface $request, Antibot $antibot): array |
|
| 258 | |||
| 259 | /** |
||
| 260 | * Decrypt and validate an HMAC |
||
| 261 | * |
||
| 262 | * @param string $hmac HMAC |
||
| 263 | * @param ServerRequestInterface $request Request |
||
| 264 | * @param Antibot $antibot Antibot instance |
||
| 265 | * |
||
| 266 | * @return bool HMAC is valid |
||
| 267 | * @throws HmacValidationException If the request timing is invalid |
||
| 268 | */ |
||
| 269 | 3 | protected function validateHmac(string $hmac, ServerRequestInterface $request, Antibot $antibot): bool |
|
| 293 | |||
| 294 | /** |
||
| 295 | * Validate the request method vector |
||
| 296 | * |
||
| 297 | * @param ServerRequestInterface $request Request |
||
| 298 | * @param array $hmacParams HMAC parameters |
||
| 299 | * |
||
| 300 | * @throws HmacValidationException If the request method order is invalid |
||
| 301 | */ |
||
| 302 | 3 | protected function validateRequestMethodVector(ServerRequestInterface $request, array &$hmacParams): void |
|
| 318 | |||
| 319 | /** |
||
| 320 | * Validate the request timing |
||
| 321 | * |
||
| 322 | * @param string $hmac HMAC |
||
| 323 | * @param Antibot $antibot Antibot instance |
||
| 324 | * @param array $hmacParams HMAC parameters |
||
| 325 | * |
||
| 326 | * @return bool Request timings were enabled and validated successfully |
||
| 327 | * |
||
| 328 | * @throws HmacValidationException If the request timing is invalid |
||
| 329 | */ |
||
| 330 | 3 | protected function validateRequestTiming(string $hmac, Antibot $antibot, array $hmacParams): bool |
|
| 367 | |||
| 368 | /** |
||
| 369 | * Probe a timed HMAC both as initial and follow-up request |
||
| 370 | * |
||
| 371 | * @param string $hmac HMAC |
||
| 372 | * @param Antibot $antibot Antibot instance |
||
| 373 | * @param array $hmacParams HMAC params |
||
| 374 | * @param int $timestamp Timestamp |
||
| 375 | * @param int $initial Initial request threshold |
||
| 376 | * |
||
| 377 | * @return bool HMAC is valid |
||
| 378 | */ |
||
| 379 | 1 | protected function probeTimedHmacAsInitialAndFollowup( |
|
| 398 | |||
| 399 | /** |
||
| 400 | * Probe a timed HMAC |
||
| 401 | * |
||
| 402 | * @param string $hmac HMAC |
||
| 403 | * @param Antibot $antibot Antibot instance |
||
| 404 | * @param array $hmacParams HMAC params |
||
| 405 | * @param int $timestamp Timestamp |
||
| 406 | * @param bool $followUp Is a follow-up request |
||
| 407 | * |
||
| 408 | * @return bool HMAC is valid |
||
| 409 | */ |
||
| 410 | 1 | protected function probeTimedHmac( |
|
| 427 | |||
| 428 | /** |
||
| 429 | * Calculate the HMAC |
||
| 430 | * |
||
| 431 | * @param ServerRequestInterface $request Request |
||
| 432 | * @param Antibot $antibot Antibot instance |
||
| 433 | * @param int|null $now Current timestamp |
||
| 434 | * |
||
| 435 | * @return string HMAC |
||
| 436 | */ |
||
| 437 | 4 | protected function calculateHmac(ServerRequestInterface $request, Antibot $antibot, int &$now = null): string |
|
| 456 | |||
| 457 | /** |
||
| 458 | * Add request method vector data to the HMAC configuration |
||
| 459 | * |
||
| 460 | * @param ServerRequestInterface $request Request |
||
| 461 | * @param array $hmacParams HMAC parameters |
||
| 462 | */ |
||
| 463 | 4 | protected function calculateRequestMethodVectorHmac(ServerRequestInterface $request, array &$hmacParams): void |
|
| 473 | |||
| 474 | /** |
||
| 475 | * Add request timing data to the HMAC configuration |
||
| 476 | * |
||
| 477 | * @param Antibot $antibot Antibot instance |
||
| 478 | * @param array $hmacParams HMAC parameters |
||
| 479 | * @param int|null $now Current timestamp |
||
| 480 | */ |
||
| 481 | 4 | protected function calculateRequestTimingHmac(Antibot $antibot, array &$hmacParams, int &$now = null): void |
|
| 491 | } |
||
| 492 |