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 $domain; |
||
| 49 | protected $errorLang = DeCaptchaErrors::LANG_EN; |
||
| 50 | protected $lastRunTime = null; |
||
| 51 | /** @var DeCaptchaErrors */ |
||
| 52 | protected $errorObject; |
||
| 53 | protected $causeAnError = false; |
||
| 54 | |||
| 55 | protected $limit = []; |
||
| 56 | protected $paramsSpec = []; |
||
| 57 | protected $params = []; |
||
| 58 | protected $limitSettings = []; |
||
| 59 | protected $decodeSettings = []; |
||
| 60 | protected $actions = []; |
||
| 61 | protected $paramsNames = []; |
||
| 62 | |||
| 63 | protected function resetLimits() |
||
| 64 | { |
||
| 65 | foreach ($this->limitSettings as $action => $value) { |
||
| 66 | $this->limit[$action] = $value; |
||
| 67 | } |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @param $action |
||
| 72 | * |
||
| 73 | * @return bool |
||
| 74 | */ |
||
| 75 | protected function limitHasNotYetEnded($action) |
||
| 76 | { |
||
| 77 | return $this->limit[$action]-- > 0; |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Не верно распознана. |
||
| 82 | */ |
||
| 83 | public function notTrue() |
||
| 84 | { |
||
| 85 | $this->getResponse('reportbad'); |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @param $action |
||
| 90 | * @param $data |
||
| 91 | * |
||
| 92 | 3 | * @throws DeCaptchaErrors |
|
| 93 | * |
||
| 94 | 3 | * @return array |
|
| 95 | 1 | */ |
|
| 96 | 1 | protected function decodeResponse($action, $data) |
|
| 120 | 1 | ||
| 121 | 1 | /** |
|
| 122 | * @param $errorLang |
||
| 123 | */ |
||
| 124 | 1 | public function setErrorLang($errorLang) |
|
| 128 | 1 | ||
| 129 | /** |
||
| 130 | 1 | * Узнаём путь до файла |
|
| 131 | * Если передана ссылка, то скачиваем и кладём во временную директорию. |
||
| 132 | * |
||
| 133 | * @param string $fileName |
||
| 134 | * |
||
| 135 | * @throws Exception |
||
| 136 | 4 | * |
|
| 137 | 4 | * @return string |
|
| 138 | */ |
||
| 139 | protected function getFilePath($fileName) |
||
| 157 | |||
| 158 | /** |
||
| 159 | 1 | * @param $action |
|
| 160 | 1 | * |
|
| 161 | * @return string |
||
| 162 | */ |
||
| 163 | protected function getActionUrl($action) |
||
| 167 | |||
| 168 | /** |
||
| 169 | 2 | * @return string |
|
| 170 | */ |
||
| 171 | 2 | protected function getBaseUrl() |
|
| 175 | |||
| 176 | /** |
||
| 177 | * @param $params |
||
| 178 | */ |
||
| 179 | public function setParams($params) |
||
| 187 | 1 | ||
| 188 | 1 | /** |
|
| 189 | 1 | * @param $param |
|
| 190 | 1 | * @param $value |
|
| 191 | 1 | */ |
|
| 192 | 1 | public function setParam($param, $value) |
|
| 193 | { |
||
| 194 | $this->params[$param] = $value; |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * @param $param |
||
| 199 | * |
||
| 200 | * @return \CURLFile|mixed|null|string |
||
| 201 | */ |
||
| 202 | public function getParamSpec($param) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @param $action |
||
| 222 | * |
||
| 223 | * @throws DeCaptchaErrors |
||
| 224 | * |
||
| 225 | * @return array |
||
| 226 | */ |
||
| 227 | protected function getParams($action) |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @param string $action |
||
| 270 | * |
||
| 271 | * @return string |
||
| 272 | */ |
||
| 273 | protected function getResponse($action) |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Задержка выполнения. |
||
| 280 | * |
||
| 281 | * @param int $delay Количество секунд |
||
| 282 | * @param \Closure|null $callback |
||
| 283 | * |
||
| 284 | * @return mixed |
||
| 285 | */ |
||
| 286 | protected function executionDelayed($delay = 0, $callback = null) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * @param string $url |
||
| 300 | * @param $data |
||
| 301 | * @param $isPost |
||
| 302 | * |
||
| 303 | * @throws DeCaptchaErrors |
||
| 304 | * |
||
| 305 | * @return mixed |
||
| 306 | */ |
||
| 307 | protected function getCurlResponse($url, $data, $isPost = true) |
||
| 335 | |||
| 336 | abstract public function getCode(); |
||
| 337 | |||
| 338 | abstract public function getError(); |
||
| 339 | } |
||
| 340 |