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 |
||
| 18 | class Client extends GenericClient |
||
| 19 | { |
||
| 20 | /* json status codes */ |
||
| 21 | const STATUS_CODE_CAPCHA_NOT_READY = 0; |
||
| 22 | const STATUS_CODE_OK = 1; |
||
| 23 | |||
| 24 | /* status codes */ |
||
| 25 | const STATUS_OK_REPORT_RECORDED = 'OK_REPORT_RECORDED'; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var int |
||
| 29 | */ |
||
| 30 | protected $recaptchaRTimeout = 15; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | protected $serverBaseUri = 'http://rucaptcha.com'; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Your application ID in Rucaptcha catalog. |
||
| 39 | * The value `1013` is ID of this library. Set in false if you want to turn off sending any ID. |
||
| 40 | * |
||
| 41 | * @see https://rucaptcha.com/software/view/php-api-client |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | protected $softId = '1013'; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @inheritdoc |
||
| 48 | */ |
||
| 49 | public function sendCaptcha($content, array $extra = []) |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Bulk captcha result. |
||
| 59 | * |
||
| 60 | * @param int[] $captchaIds # Captcha task Ids array |
||
| 61 | * @return string[] # Array $captchaId => $captchaText or false if is not ready |
||
| 62 | * @throws GuzzleException |
||
| 63 | */ |
||
| 64 | public function getCaptchaResultBulk(array $captchaIds) |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Returns balance of account. |
||
| 94 | * |
||
| 95 | * @return string |
||
| 96 | * @throws Throwable |
||
| 97 | */ |
||
| 98 | public function getBalance() |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Alias of $this->reportBad(); |
||
| 111 | * |
||
| 112 | * @param string $captchaId |
||
| 113 | * @return bool |
||
| 114 | * @throws Throwable |
||
| 115 | * @deprecated |
||
| 116 | */ |
||
| 117 | public function badCaptcha($captchaId) |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Alias of $this->reportGood(); |
||
| 124 | * |
||
| 125 | * @param string $captchaId |
||
| 126 | * @return bool |
||
| 127 | * @throws ErrorResponseException |
||
| 128 | * @throws Throwable |
||
| 129 | * @deprecated |
||
| 130 | */ |
||
| 131 | public function goodCaptcha($captchaId) |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Report of wrong recognition. |
||
| 138 | * |
||
| 139 | * @param string $captchaId |
||
| 140 | * @return bool |
||
| 141 | * @throws ErrorResponseException |
||
| 142 | * @throws Throwable |
||
| 143 | */ |
||
| 144 | public function reportBad($captchaId) |
||
| 163 | |||
| 164 | |||
| 165 | /** |
||
| 166 | * Reports rucaptcha for good recognition. |
||
| 167 | * |
||
| 168 | * @param $captchaId |
||
| 169 | * @return bool |
||
| 170 | * @throws ErrorResponseException |
||
| 171 | * @throws Throwable |
||
| 172 | */ |
||
| 173 | public function reportGood($captchaId) |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @param string $captchaId # Captcha task ID |
||
| 195 | * @return array | false # Solved captcha and cost array or false if captcha is not ready |
||
| 196 | * @throws ErrorResponseException |
||
| 197 | * @throws Throwable |
||
| 198 | */ |
||
| 199 | public function getCaptchaResultWithCost($captchaId) |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Add pingback url to rucaptcha whitelist. |
||
| 230 | * |
||
| 231 | * @param string $url |
||
| 232 | * @return bool # true if added and exception if fail |
||
| 233 | * @throws ErrorResponseException |
||
| 234 | * @throws Throwable |
||
| 235 | */ |
||
| 236 | public function addPingback($url) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Returns pingback whitelist items. |
||
| 258 | * |
||
| 259 | * @return string[] # List of urls |
||
| 260 | * @throws ErrorResponseException |
||
| 261 | * @throws Throwable |
||
| 262 | */ |
||
| 263 | public function getPingbacks() |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Remove pingback url from whitelist. |
||
| 287 | * |
||
| 288 | * @param string $uri |
||
| 289 | * @return bool |
||
| 290 | * @throws ErrorResponseException |
||
| 291 | * @throws Throwable |
||
| 292 | */ |
||
| 293 | public function deletePingback($uri) |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Truncate pingback whitelist. |
||
| 315 | * |
||
| 316 | * @return bool |
||
| 317 | * @throws ErrorResponseException |
||
| 318 | * @throws Throwable |
||
| 319 | */ |
||
| 320 | public function deleteAllPingbacks() |
||
| 324 | |||
| 325 | /* Recaptcha v2 */ |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Sent recaptcha v2 |
||
| 329 | * |
||
| 330 | * @param string $googleKey |
||
| 331 | * @param string $pageUrl |
||
| 332 | * @param array $extra |
||
| 333 | * |
||
| 334 | * @return string |
||
| 335 | * |
||
| 336 | * @throws ErrorResponseException |
||
| 337 | * @throws Throwable |
||
| 338 | */ |
||
| 339 | public function sendRecaptchaV2($googleKey, $pageUrl, $extra = []) |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Recaptcha V2 recognition. |
||
| 373 | * |
||
| 374 | * @param string $googleKey |
||
| 375 | * @param string $pageUrl |
||
| 376 | * @param array $extra # Captcha options |
||
| 377 | * |
||
| 378 | * @return string # Code to place in hidden form |
||
| 379 | * |
||
| 380 | * @throws ErrorResponseException |
||
| 381 | * @throws InvalidArgumentException |
||
| 382 | * @throws RuntimeException |
||
| 383 | * @throws Throwable |
||
| 384 | */ |
||
| 385 | public function recognizeRecaptchaV2($googleKey, $pageUrl, $extra = []) |
||
| 410 | |||
| 411 | /* Recaptcha v3 */ |
||
| 412 | |||
| 413 | /** |
||
| 414 | * @param string $googleKey |
||
| 415 | * @param string $pageUrl |
||
| 416 | * @param string $action |
||
| 417 | * @param string $minScore |
||
| 418 | * @param array $extra |
||
| 419 | * |
||
| 420 | * @return string |
||
| 421 | * |
||
| 422 | * @throws ErrorResponseException |
||
| 423 | * @throws Throwable |
||
| 424 | * |
||
| 425 | * @see https://rucaptcha.com/blog/for_webmaster/recaptcha-v3-obhod |
||
| 426 | */ |
||
| 427 | public function sendRecaptchaV3($googleKey, $pageUrl, $action, $minScore = '0.3', $extra = []) |
||
| 459 | |||
| 460 | /** |
||
| 461 | * @param string $googleKey |
||
| 462 | * @param string $pageUrl |
||
| 463 | * @param string $action |
||
| 464 | * @param string $minScore |
||
| 465 | * @param array $extra |
||
| 466 | * |
||
| 467 | * @return false|string |
||
| 468 | * |
||
| 469 | * @throws ErrorResponseException |
||
| 470 | * @throws InvalidArgumentException |
||
| 471 | * @throws RuntimeException |
||
| 472 | * @throws Throwable |
||
| 473 | */ |
||
| 474 | public function recognizeRecaptchaV3($googleKey, $pageUrl, $action, $minScore = '0.3', $extra = []) |
||
| 499 | |||
| 500 | /** |
||
| 501 | * Keycaptcha recognition. |
||
| 502 | * |
||
| 503 | * @param string $SSCUserId |
||
| 504 | * @param string $SSCSessionId |
||
| 505 | * @param string $SSCWebServerSign |
||
| 506 | * @param string $SSCWebServerSign2 |
||
| 507 | * @param string $pageUrl |
||
| 508 | * @param array $extra |
||
| 509 | * |
||
| 510 | * @return string # Captcha ID |
||
| 511 | * |
||
| 512 | * @throws ErrorResponseException |
||
| 513 | * @throws Throwable |
||
| 514 | */ |
||
| 515 | public function sendKeyCaptcha( |
||
| 553 | |||
| 554 | /** |
||
| 555 | * Keycaptcha recognition. |
||
| 556 | * |
||
| 557 | * @param string $SSCUserId |
||
| 558 | * @param string $SSCSessionId |
||
| 559 | * @param string $SSCWebServerSign |
||
| 560 | * @param string $SSCWebServerSign2 |
||
| 561 | * @param string $pageUrl |
||
| 562 | * @param array $extra |
||
| 563 | * @return string # Code to place into id="capcode" input value |
||
| 564 | * @throws ErrorResponseException |
||
| 565 | * @throws InvalidArgumentException |
||
| 566 | * @throws RuntimeException |
||
| 567 | * @throws Throwable |
||
| 568 | */ |
||
| 569 | public function recognizeKeyCaptcha( |
||
| 602 | |||
| 603 | /** |
||
| 604 | * Override generic method for using json response. |
||
| 605 | * |
||
| 606 | * @param string $captchaId # Captcha task ID |
||
| 607 | * @return false|string # Solved captcha text or false if captcha is not ready |
||
| 608 | * @throws ErrorResponseException |
||
| 609 | * @throws InvalidArgumentException |
||
| 610 | * @throws Throwable |
||
| 611 | */ |
||
| 612 | public function getCaptchaResult($captchaId) |
||
| 642 | |||
| 643 | /** |
||
| 644 | * Match error code by response. |
||
| 645 | * |
||
| 646 | * @param string $responseText |
||
| 647 | * @return int |
||
| 648 | */ |
||
| 649 | private function getErrorCode($responseText) |
||
| 656 | } |
||
| 657 |