Complex classes like DeCaptchaAbstract 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 DeCaptchaAbstract, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | abstract class DeCaptchaAbstract implements DeCaptchaInterface |
||
| 11 | { |
||
| 12 | const RESPONSE_TYPE_STRING = 0; |
||
| 13 | const RESPONSE_TYPE_JSON = 1; |
||
| 14 | |||
| 15 | const ACTION_FIELDS = 0; |
||
| 16 | const ACTION_URI = 1; |
||
| 17 | const ACTION_METHOD = 2; |
||
| 18 | const ACTION_JSON = 3; |
||
| 19 | |||
| 20 | const ACTION_METHOD_POST = 0; |
||
| 21 | const ACTION_METHOD_GET = 1; |
||
| 22 | |||
| 23 | const DECODE_FORMAT = 0; |
||
| 24 | const DECODE_ACTION = 1; |
||
| 25 | const DECODE_SEPARATOR = 2; |
||
| 26 | const DECODE_PARAMS = 3; |
||
| 27 | const DECODE_PARAM_SETTING_MARKER = 4; |
||
| 28 | |||
| 29 | const PARAM_FIELD_TYPE_STRING = 0; |
||
| 30 | const PARAM_FIELD_TYPE_INTEGER = 1; |
||
| 31 | const PARAM_FIELD_TYPE_MIX = 2; |
||
| 32 | |||
| 33 | const PARAM_SLUG_DEFAULT = 0; |
||
| 34 | const PARAM_SLUG_TYPE = 1; |
||
| 35 | const PARAM_SLUG_REQUIRE = 2; |
||
| 36 | const PARAM_SLUG_SPEC = 3; |
||
| 37 | const PARAM_SLUG_VARIABLE = 4; |
||
| 38 | const PARAM_SLUG_CODING = 5; |
||
| 39 | |||
| 40 | const PARAM_SLUG_CODING_BASE64 = 0; |
||
| 41 | |||
| 42 | const PARAM_SPEC_API_KEY = -1; |
||
| 43 | const PARAM_SPEC_FILE = -2; |
||
| 44 | const PARAM_SPEC_CAPTCHA = -3; |
||
| 45 | const PARAM_SPEC_CODE = -4; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Сервис на который будем загружать капчу. |
||
| 49 | * |
||
| 50 | * @var string |
||
| 51 | */ |
||
| 52 | protected $host; |
||
| 53 | protected $scheme = 'http'; |
||
| 54 | protected $errorLang = DeCaptchaErrors::LANG_EN; |
||
| 55 | protected $lastRunTime = null; |
||
| 56 | /** @var DeCaptchaErrors */ |
||
| 57 | protected $errorObject; |
||
| 58 | protected $causeAnError = false; |
||
| 59 | |||
| 60 | protected $limit = []; |
||
| 61 | protected $paramsSpec = []; |
||
| 62 | protected $params = []; |
||
| 63 | protected $limitSettings = []; |
||
| 64 | protected $decodeSettings = []; |
||
| 65 | protected $actions = []; |
||
| 66 | protected $paramsNames = []; |
||
| 67 | |||
| 68 | protected function resetLimits() |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @param $action |
||
| 77 | * |
||
| 78 | * @return bool |
||
| 79 | */ |
||
| 80 | protected function limitHasNotYetEnded($action) |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @param $action |
||
| 87 | * @param $data |
||
| 88 | * |
||
| 89 | * @throws DeCaptchaErrors |
||
| 90 | * |
||
| 91 | * @return array |
||
| 92 | 3 | */ |
|
| 93 | protected function decodeResponse($action, $data) |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @param $errorLang |
||
| 136 | 4 | */ |
|
| 137 | 4 | public function setErrorLang($errorLang) |
|
| 141 | |||
| 142 | /** |
||
| 143 | * Узнаём путь до файла |
||
| 144 | 2 | * Если передана ссылка, то скачиваем и кладём во временную директорию. |
|
| 145 | 2 | * |
|
| 146 | * @param string $fileName |
||
| 147 | * |
||
| 148 | * @throws Exception |
||
| 149 | * |
||
| 150 | * @return string |
||
| 151 | */ |
||
| 152 | 1 | protected function getFilePath($fileName) |
|
| 170 | |||
| 171 | 2 | /** |
|
| 172 | 1 | * @param $action |
|
| 173 | * |
||
| 174 | 1 | * @return string |
|
| 175 | */ |
||
| 176 | protected function getActionUrl($action) |
||
| 180 | |||
| 181 | /** |
||
| 182 | * @return string |
||
| 183 | */ |
||
| 184 | protected function getBaseUrl() |
||
| 188 | 1 | ||
| 189 | 1 | /** |
|
| 190 | 1 | * @param $params |
|
| 191 | 1 | */ |
|
| 192 | 1 | public function setParams($params) |
|
| 200 | |||
| 201 | /** |
||
| 202 | * @param $param |
||
| 203 | * @param $value |
||
| 204 | */ |
||
| 205 | public function setParam($param, $value) |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @param $param |
||
| 212 | * @param $coding |
||
| 213 | * |
||
| 214 | * @return \CURLFile|mixed|null|string |
||
| 215 | */ |
||
| 216 | public function getParamSpec($param, $coding = null) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @param $action |
||
| 241 | * |
||
| 242 | * @throws DeCaptchaErrors |
||
| 243 | * |
||
| 244 | * @return array |
||
| 245 | */ |
||
| 246 | protected function getParams($action) |
||
| 286 | |||
| 287 | /** |
||
| 288 | * @param string $action |
||
| 289 | * |
||
| 290 | * @return string |
||
| 291 | */ |
||
| 292 | protected function getResponse($action) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Задержка выполнения. |
||
| 304 | * |
||
| 305 | * @param int $delay Количество секунд |
||
| 306 | * @param \Closure|null $callback |
||
| 307 | * |
||
| 308 | * @return mixed |
||
| 309 | */ |
||
| 310 | protected function executionDelayed($delay = 0, $callback = null) |
||
| 321 | |||
| 322 | /** |
||
| 323 | * @param string $url |
||
| 324 | * @param array $data |
||
| 325 | * @param bool $isPost |
||
| 326 | * @param bool $isJson |
||
| 327 | * |
||
| 328 | * @throws DeCaptchaErrors |
||
| 329 | * |
||
| 330 | * @return string |
||
| 331 | */ |
||
| 332 | protected function getCurlResponse($url, $data, $isPost = true, $isJson = false) |
||
| 371 | |||
| 372 | abstract public function getCode(); |
||
| 373 | |||
| 374 | abstract public function getError(); |
||
| 375 | } |
||
| 376 |