Complex classes like Client 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 Client, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class Client extends GenericClient |
||
17 | { |
||
18 | /* json status codes */ |
||
19 | const STATUS_CODE_CAPCHA_NOT_READY = 0; |
||
20 | const STATUS_CODE_OK = 1; |
||
21 | |||
22 | /* status codes */ |
||
23 | const STATUS_OK_REPORT_RECORDED = 'OK_REPORT_RECORDED'; |
||
24 | |||
25 | /** |
||
26 | * @var int |
||
27 | */ |
||
28 | protected $recaptchaRTimeout = 15; |
||
29 | |||
30 | /** |
||
31 | * @var string |
||
32 | */ |
||
33 | protected $serverBaseUri = 'http://rucaptcha.com'; |
||
34 | |||
35 | /** |
||
36 | * Your application ID in Rucaptcha catalog. |
||
37 | * The value `1013` is ID of this library. Set in false if you want to turn off sending any ID. |
||
38 | * |
||
39 | * @see https://rucaptcha.com/software/view/php-api-client |
||
40 | * @var string |
||
41 | */ |
||
42 | protected $softId = '1013'; |
||
43 | |||
44 | /** |
||
45 | * @inheritdoc |
||
46 | */ |
||
47 | public function sendCaptcha($content, array $extra = []) |
||
54 | |||
55 | /** |
||
56 | * Bulk captcha result. |
||
57 | * |
||
58 | * @param int[] $captchaIds # Captcha task Ids array |
||
59 | * @return string[] # Array $captchaId => $captchaText or false if is not ready |
||
60 | * @throws ErrorResponseException |
||
61 | */ |
||
62 | public function getCaptchaResultBulk(array $captchaIds) |
||
86 | |||
87 | /** |
||
88 | * Returns balance of account. |
||
89 | * |
||
90 | * @return string |
||
91 | */ |
||
92 | public function getBalance() |
||
100 | |||
101 | /** |
||
102 | * Report of wrong recognition. |
||
103 | * |
||
104 | * @param string $captchaId |
||
105 | * @return bool |
||
106 | * @throws ErrorResponseException |
||
107 | */ |
||
108 | public function badCaptcha($captchaId) |
||
125 | |||
126 | /** |
||
127 | * Returns server health data. |
||
128 | * |
||
129 | * @param string|string[] $paramsList # List of metrics to be returned |
||
130 | * @return array # Array of load metrics $metric => $value formatted |
||
131 | */ |
||
132 | public function getLoad($paramsList = ['waiting', 'load', 'minbid', 'averageRecognitionTime']) |
||
148 | |||
149 | /** |
||
150 | * Returns load data as XML. |
||
151 | * |
||
152 | * @return \SimpleXMLElement |
||
153 | */ |
||
154 | public function getLoadXml() |
||
162 | |||
163 | /** |
||
164 | * @param string $captchaId # Captcha task ID |
||
165 | * @return array | false # Solved captcha and cost array or false if captcha is not ready |
||
166 | * @throws ErrorResponseException |
||
167 | */ |
||
168 | public function getCaptchaResultWithCost($captchaId) |
||
194 | |||
195 | /** |
||
196 | * Add pingback url to rucaptcha whitelist. |
||
197 | * |
||
198 | * @param string $url |
||
199 | * @return bool # true if added and exception if fail |
||
200 | * @throws ErrorResponseException |
||
201 | */ |
||
202 | public function addPingback($url) |
||
219 | |||
220 | /** |
||
221 | * Returns pingback whitelist items. |
||
222 | * |
||
223 | * @return string[] # List of urls |
||
224 | * @throws ErrorResponseException |
||
225 | */ |
||
226 | public function getPingbacks() |
||
245 | |||
246 | /** |
||
247 | * Remove pingback url from whitelist. |
||
248 | * |
||
249 | * @param string $uri |
||
250 | * @return bool |
||
251 | * @throws ErrorResponseException |
||
252 | */ |
||
253 | public function deletePingback($uri) |
||
269 | |||
270 | /** |
||
271 | * Truncate pingback whitelist. |
||
272 | * |
||
273 | * @return bool |
||
274 | * @throws ErrorResponseException |
||
275 | */ |
||
276 | public function deleteAllPingbacks() |
||
280 | |||
281 | /* Recaptcha v2 */ |
||
282 | |||
283 | public function sendRecapthaV2($googleKey, $pageUrl, $extra = []) |
||
310 | |||
311 | /** |
||
312 | * Recaptcha V2 recognition. |
||
313 | * |
||
314 | * @param string $googleKey |
||
315 | * @param string $pageUrl |
||
316 | * @param array $extra # Captcha options |
||
317 | * @return string # Code to place in hidden form |
||
318 | * @throws RuntimeException |
||
319 | */ |
||
320 | public function recognizeRecaptchaV2($googleKey, $pageUrl, $extra = []) |
||
347 | |||
348 | /** |
||
349 | * Keycaptcha recognition. |
||
350 | * |
||
351 | * @param string $SSCUserId |
||
352 | * @param string $SSCSessionId |
||
353 | * @param string $SSCWebServerSign |
||
354 | * @param string $SSCWebServerSign2 |
||
355 | * @param string $pageUrl |
||
356 | * @param array $extra |
||
357 | * @return string # Captcha ID |
||
358 | * @throws ErrorResponseException |
||
359 | */ |
||
360 | public function sendKeyCaptcha( |
||
397 | |||
398 | /** |
||
399 | * Keycaptcha recognition. |
||
400 | * |
||
401 | * @param string $SSCUserId |
||
402 | * @param string $SSCSessionId |
||
403 | * @param string $SSCWebServerSign |
||
404 | * @param string $SSCWebServerSign2 |
||
405 | * @param string $pageUrl |
||
406 | * @param array $extra |
||
407 | * @return string # Code to place into id="capcode" input value |
||
408 | * @throws RuntimeException |
||
409 | */ |
||
410 | public function recognizeKeyCaptcha( |
||
445 | |||
446 | /** |
||
447 | * Override generic method for using json response. |
||
448 | * |
||
449 | * @param string $captchaId # Captcha task ID |
||
450 | * @return false|string # Solved captcha text or false if captcha is not ready |
||
451 | * @throws ErrorResponseException |
||
452 | * @throws InvalidArgumentException |
||
453 | */ |
||
454 | public function getCaptchaResult($captchaId) |
||
484 | |||
485 | /** |
||
486 | * Match error code by response. |
||
487 | * |
||
488 | * @param string $responseText |
||
489 | * @return int |
||
490 | */ |
||
491 | private function getErrorCode($responseText) |
||
498 | } |
||
499 |