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 | |||
| 19 | const ACTION_METHOD_POST = 0; |
||
| 20 | const ACTION_METHOD_GET = 1; |
||
| 21 | |||
| 22 | const DECODE_FORMAT = 0; |
||
| 23 | const DECODE_ACTION = 1; |
||
| 24 | const DECODE_SEPARATOR = 2; |
||
| 25 | const DECODE_PARAMS = 3; |
||
| 26 | const DECODE_PARAM_SETTING_MARKER = 4; |
||
| 27 | |||
| 28 | const PARAM_FIELD_TYPE_STRING = 0; |
||
| 29 | const PARAM_FIELD_TYPE_INTEGER = 1; |
||
| 30 | const PARAM_FIELD_TYPE_MIX = 2; |
||
| 31 | |||
| 32 | const PARAM_SLUG_DEFAULT = 0; |
||
| 33 | const PARAM_SLUG_TYPE = 1; |
||
| 34 | const PARAM_SLUG_REQUIRE = 2; |
||
| 35 | const PARAM_SLUG_SPEC = 3; |
||
| 36 | const PARAM_SLUG_VARIABLE = 4; |
||
| 37 | |||
| 38 | const PARAM_SPEC_API_KEY = -1; |
||
| 39 | const PARAM_SPEC_FILE = -2; |
||
| 40 | const PARAM_SPEC_CAPTCHA = -3; |
||
| 41 | const PARAM_SPEC_CODE = -4; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Сервис на который будем загружать капчу. |
||
| 45 | * |
||
| 46 | * @var string |
||
| 47 | */ |
||
| 48 | protected $host; |
||
| 49 | protected $scheme = 'http'; |
||
| 50 | protected $errorLang = DeCaptchaErrors::LANG_EN; |
||
| 51 | protected $lastRunTime = null; |
||
| 52 | /** @var DeCaptchaErrors */ |
||
| 53 | protected $errorObject; |
||
| 54 | protected $causeAnError = false; |
||
| 55 | |||
| 56 | protected $limit = []; |
||
| 57 | protected $paramsSpec = []; |
||
| 58 | protected $params = []; |
||
| 59 | protected $limitSettings = []; |
||
| 60 | protected $decodeSettings = []; |
||
| 61 | protected $actions = []; |
||
| 62 | protected $paramsNames = []; |
||
| 63 | |||
| 64 | protected function resetLimits() |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @param $action |
||
| 73 | * |
||
| 74 | * @return bool |
||
| 75 | */ |
||
| 76 | protected function limitHasNotYetEnded($action) |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @param $action |
||
| 83 | * @param $data |
||
| 84 | * |
||
| 85 | * @throws DeCaptchaErrors |
||
| 86 | * |
||
| 87 | * @return array |
||
| 88 | */ |
||
| 89 | protected function decodeResponse($action, $data) |
||
| 113 | |||
| 114 | 3 | /** |
|
| 115 | 3 | * @param $errorLang |
|
| 116 | 2 | */ |
|
| 117 | 2 | public function setErrorLang($errorLang) |
|
| 121 | 1 | ||
| 122 | /** |
||
| 123 | * Узнаём путь до файла |
||
| 124 | 1 | * Если передана ссылка, то скачиваем и кладём во временную директорию. |
|
| 125 | 1 | * |
|
| 126 | * @param string $fileName |
||
| 127 | 2 | * |
|
| 128 | 1 | * @throws Exception |
|
| 129 | * |
||
| 130 | 1 | * @return string |
|
| 131 | */ |
||
| 132 | protected function getFilePath($fileName) |
||
| 150 | |||
| 151 | /** |
||
| 152 | 1 | * @param $action |
|
| 153 | 1 | * |
|
| 154 | * @return string |
||
| 155 | */ |
||
| 156 | protected function getActionUrl($action) |
||
| 160 | 1 | ||
| 161 | /** |
||
| 162 | * @return string |
||
| 163 | */ |
||
| 164 | protected function getBaseUrl() |
||
| 168 | |||
| 169 | 2 | /** |
|
| 170 | * @param $params |
||
| 171 | 2 | */ |
|
| 172 | 1 | public function setParams($params) |
|
| 180 | |||
| 181 | /** |
||
| 182 | * @param $param |
||
| 183 | * @param $value |
||
| 184 | */ |
||
| 185 | 1 | public function setParam($param, $value) |
|
| 189 | 1 | ||
| 190 | 1 | /** |
|
| 191 | 1 | * @param $param |
|
| 192 | 1 | * |
|
| 193 | * @return \CURLFile|mixed|null|string |
||
| 194 | */ |
||
| 195 | public function getParamSpec($param) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @param $action |
||
| 216 | * |
||
| 217 | * @throws DeCaptchaErrors |
||
| 218 | * |
||
| 219 | * @return array |
||
| 220 | */ |
||
| 221 | protected function getParams($action) |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @param string $action |
||
| 264 | * |
||
| 265 | * @return string |
||
| 266 | */ |
||
| 267 | protected function getResponse($action) |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Задержка выполнения. |
||
| 274 | * |
||
| 275 | * @param int $delay Количество секунд |
||
| 276 | * @param \Closure|null $callback |
||
| 277 | * |
||
| 278 | * @return mixed |
||
| 279 | */ |
||
| 280 | protected function executionDelayed($delay = 0, $callback = null) |
||
| 291 | |||
| 292 | /** |
||
| 293 | * @param string $url |
||
| 294 | * @param $data |
||
| 295 | * @param bool $isPost |
||
| 296 | * @param bool $isJson |
||
| 297 | * |
||
| 298 | * @throws DeCaptchaErrors |
||
| 299 | * |
||
| 300 | * @return mixed |
||
| 301 | */ |
||
| 302 | protected function getCurlResponse($url, $data, $isPost = true, $isJson = false) |
||
| 341 | |||
| 342 | abstract public function getCode(); |
||
| 343 | |||
| 344 | abstract public function getError(); |
||
| 345 | } |
||
| 346 |