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 | * Validation order position |
||
71 | * |
||
72 | * @var int |
||
73 | */ |
||
74 | const POSITION = 100; |
||
75 | /** |
||
76 | * GET request |
||
77 | * |
||
78 | * @var string |
||
79 | */ |
||
80 | const METHOD_GET = 'GET'; |
||
81 | /** |
||
82 | * POST request |
||
83 | * |
||
84 | * @var string |
||
85 | */ |
||
86 | const METHOD_POST = 'POST'; |
||
87 | /** |
||
88 | * Minimum submission time |
||
89 | * |
||
90 | * @var float |
||
91 | */ |
||
92 | const MINIMUM_SUBMISSION = 3; |
||
93 | /** |
||
94 | * Minimum submission time for follow-up submissions |
||
95 | * |
||
96 | * @var float |
||
97 | */ |
||
98 | const MINIMUM_FOLLOWUP_SUBMISSION = 1; |
||
99 | /** |
||
100 | * Maximum submission time |
||
101 | * |
||
102 | * @var float |
||
103 | */ |
||
104 | const MAXIMUM_SUBMISSION = 3600; |
||
105 | /** |
||
106 | * Block access |
||
107 | * |
||
108 | * @var string |
||
109 | */ |
||
110 | const BLOCK = 'BLOCK'; |
||
111 | |||
112 | /** |
||
113 | * Set the request method vector |
||
114 | * |
||
115 | * @param string $previous Previous request |
||
116 | * @param string $current Current request |
||
117 | */ |
||
118 | 2 | public function setMethodVector(string $previous = null, string $current = null): void |
|
129 | |||
130 | /** |
||
131 | * Sanitize and validate a request method |
||
132 | * |
||
133 | * @param string $method Request method |
||
134 | * |
||
135 | * @return string Validated request method |
||
136 | * @throws InvalidArgumentException If the request method is invalid |
||
137 | */ |
||
138 | 2 | protected function validateRequestMethod(string $method): string |
|
150 | |||
151 | /** |
||
152 | * Sanitize and set the submission times |
||
153 | * |
||
154 | * @param float $max Maximum submission time |
||
155 | * @param float $min Minimum submission time |
||
156 | * @param float|null $minFollowUp Minimum submission time for follow-up submissions |
||
157 | */ |
||
158 | 2 | public function setSubmissionTimes(float $max = null, float $min = null, float $minFollowUp = null): void |
|
173 | |||
174 | /** |
||
175 | * Validate a request |
||
176 | * |
||
177 | * @param ServerRequestInterface $request Request |
||
178 | * @param Antibot $antibot Antibot instance |
||
179 | * |
||
180 | * @return bool |
||
181 | * @throws HmacValidationException |
||
182 | * @throws SkippedValidationException If no Antibot data has been submitted |
||
183 | */ |
||
184 | 3 | public function validate(ServerRequestInterface $request, Antibot $antibot): bool |
|
195 | |||
196 | /** |
||
197 | * Create protective form HTML |
||
198 | * |
||
199 | * @param ServerRequestInterface $request Request |
||
200 | * @param Antibot $antibot Antibot instance |
||
201 | * |
||
202 | * @return InputElement[] HMTL input elements |
||
203 | */ |
||
204 | 4 | public function armor(ServerRequestInterface $request, Antibot $antibot): array |
|
226 | |||
227 | /** |
||
228 | * Decrypt and validate an HMAC |
||
229 | * |
||
230 | * @param string $hmac HMAC |
||
231 | * @param ServerRequestInterface $request Request |
||
232 | * @param Antibot $antibot Antibot instance |
||
233 | * |
||
234 | * @return bool HMAC is valid |
||
235 | * @throws HmacValidationException If the request timing is invalid |
||
236 | */ |
||
237 | 3 | protected function validateHmac(string $hmac, ServerRequestInterface $request, Antibot $antibot): bool |
|
238 | { |
||
239 | // $previousMethod = null; |
||
240 | 3 | $hmacParams = [$antibot->getUnique()]; |
|
241 | |||
242 | // Short-circuit blocked HMAC |
||
243 | 3 | $hmacBlock = $hmacParams; |
|
244 | 3 | $hmacBlock[] = self::BLOCK; |
|
245 | 3 | if (HmacFactory::createFromString(serialize($hmacBlock), $antibot->getUnique()) === $hmac) { |
|
246 | return false; |
||
247 | } |
||
248 | |||
249 | // Validate the request method vector |
||
250 | 3 | $this->validateRequestMethodVector($request, $hmacParams); |
|
251 | |||
252 | // If the request timings validate |
||
253 | 3 | if ($this->validateRequestTiming($hmac, $antibot, $hmacParams)) { |
|
254 | 1 | return true; |
|
255 | } |
||
256 | |||
257 | // Else: Do a simple validation without request timings |
||
258 | 2 | $currentHMAC = HmacFactory::createFromString(serialize($hmacParams), $antibot->getUnique()); |
|
259 | |||
260 | 2 | return $hmac === $currentHMAC; |
|
261 | } |
||
262 | |||
263 | /** |
||
264 | * Validate the request method vector |
||
265 | * |
||
266 | * @param ServerRequestInterface $request Request |
||
267 | * @param array $hmacParams HMAC parameters |
||
268 | * |
||
269 | * @throws HmacValidationException If the request method order is invalid |
||
270 | */ |
||
271 | 3 | protected function validateRequestMethodVector(ServerRequestInterface $request, array &$hmacParams): void |
|
287 | |||
288 | /** |
||
289 | * Validate the request timing |
||
290 | * |
||
291 | * @param string $hmac HMAC |
||
292 | * @param Antibot $antibot Antibot instance |
||
293 | * @param array $hmacParams HMAC parameters |
||
294 | * |
||
295 | * @return bool Request timings were enabled and validated successfully |
||
296 | * |
||
297 | * @throws HmacValidationException If the request timing is invalid |
||
298 | */ |
||
299 | 3 | protected function validateRequestTiming(string $hmac, Antibot $antibot, array $hmacParams): bool |
|
336 | |||
337 | /** |
||
338 | * Probe a timed HMAC both as initial and follow-up request |
||
339 | * |
||
340 | * @param string $hmac HMAC |
||
341 | * @param Antibot $antibot Antibot instance |
||
342 | * @param array $hmacParams HMAC params |
||
343 | * @param int $timestamp Timestamp |
||
344 | * @param int $initial Initial request threshold |
||
345 | * |
||
346 | * @return bool HMAC is valid |
||
347 | */ |
||
348 | 1 | protected function probeTimedHmacAsInitialAndFollowup( |
|
367 | |||
368 | /** |
||
369 | * Probe a timed HMAC |
||
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 bool $followUp Is a follow-up request |
||
376 | * |
||
377 | * @return bool HMAC is valid |
||
378 | */ |
||
379 | 1 | protected function probeTimedHmac( |
|
396 | |||
397 | /** |
||
398 | * Calculate the HMAC |
||
399 | * |
||
400 | * @param ServerRequestInterface $request Request |
||
401 | * @param Antibot $antibot Antibot instance |
||
402 | * @param int|null $now Current timestamp |
||
403 | * |
||
404 | * @return string HMAC |
||
405 | */ |
||
406 | 4 | protected function calculateHmac(ServerRequestInterface $request, Antibot $antibot, int &$now = null): string |
|
425 | |||
426 | /** |
||
427 | * Add request method vector data to the HMAC configuration |
||
428 | * |
||
429 | * @param ServerRequestInterface $request Request |
||
430 | * @param array $hmacParams HMAC parameters |
||
431 | */ |
||
432 | 4 | protected function calculateRequestMethodVectorHmac(ServerRequestInterface $request, array &$hmacParams): void |
|
441 | |||
442 | /** |
||
443 | * Add request timing data to the HMAC configuration |
||
444 | * |
||
445 | * @param Antibot $antibot Antibot instance |
||
446 | * @param array $hmacParams HMAC parameters |
||
447 | * @param int|null $now Current timestamp |
||
448 | */ |
||
449 | 4 | protected function calculateRequestTimingHmac(Antibot $antibot, array &$hmacParams, int &$now = null): void |
|
459 | } |
||
460 |