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 | |||
| 31 | const PARAM_SLUG_DEFAULT = 0; |
||
| 32 | const PARAM_SLUG_TYPE = 1; |
||
| 33 | const PARAM_SLUG_REQUIRE = 2; |
||
| 34 | const PARAM_SLUG_SPEC = 3; |
||
| 35 | const PARAM_SLUG_VARIABLE = 4; |
||
| 36 | |||
| 37 | const PARAM_SPEC_KEY = 0; |
||
| 38 | const PARAM_SPEC_FILE = 1; |
||
| 39 | const PARAM_SPEC_CAPTCHA = 2; |
||
| 40 | const PARAM_SPEC_CODE = 3; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Сервис на который будем загружать капчу. |
||
| 44 | * |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | protected $domain; |
||
| 48 | protected $errorLang = DeCaptchaErrors::LANG_EN; |
||
| 49 | protected $lastRunTime = null; |
||
| 50 | /** @var DeCaptchaErrors */ |
||
| 51 | protected $errorObject; |
||
| 52 | protected $causeAnError = false; |
||
| 53 | |||
| 54 | protected $limit = []; |
||
| 55 | protected $paramsSpec = []; |
||
| 56 | protected $params = []; |
||
| 57 | protected $limitSettings = []; |
||
| 58 | protected $decodeSettings = []; |
||
| 59 | protected $actions = []; |
||
| 60 | protected $paramsNames = []; |
||
| 61 | |||
| 62 | protected function resetLimits() |
||
| 68 | |||
| 69 | protected function limitHasNotYetEnded($action) |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Запуск распознавания капчи. |
||
| 76 | * |
||
| 77 | * @deprecated |
||
| 78 | * |
||
| 79 | * @param string $filePath Путь до файла или ссылка на него |
||
| 80 | * |
||
| 81 | * @return bool |
||
| 82 | */ |
||
| 83 | public function run($filePath) |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Не верно распознана. |
||
| 90 | */ |
||
| 91 | public function notTrue() |
||
| 95 | 1 | ||
| 96 | 1 | protected function decodeResponse($action, $data) |
|
| 120 | 1 | ||
| 121 | 1 | public function setErrorLang($errorLang) |
|
| 125 | 1 | ||
| 126 | /** |
||
| 127 | 2 | * Узнаём путь до файла |
|
| 128 | 1 | * Если передана ссылка, то скачиваем и кладём во временную директорию. |
|
| 129 | * |
||
| 130 | 1 | * @param string $fileName |
|
| 131 | * |
||
| 132 | * @throws Exception |
||
| 133 | * |
||
| 134 | * @return string |
||
| 135 | */ |
||
| 136 | 4 | protected function getFilePath($fileName) |
|
| 154 | |||
| 155 | /** |
||
| 156 | * @param $action |
||
| 157 | * |
||
| 158 | * @return string |
||
| 159 | 1 | */ |
|
| 160 | 1 | protected function getActionUrl($action) |
|
| 164 | |||
| 165 | /** |
||
| 166 | * @return string |
||
| 167 | */ |
||
| 168 | protected function getBaseUrl() |
||
| 172 | 1 | ||
| 173 | public function setParams($params) |
||
| 181 | |||
| 182 | public function setParamSpec($param, $value) |
||
| 186 | 1 | ||
| 187 | 1 | public function getParamSpec($param) |
|
| 204 | |||
| 205 | protected function getParams($action) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * @param string $action |
||
| 242 | * |
||
| 243 | * @return string |
||
| 244 | */ |
||
| 245 | protected function getResponse($action) |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Задержка выполнения. |
||
| 252 | * |
||
| 253 | * @param int $delay Количество секунд |
||
| 254 | * @param \Closure|null $callback |
||
| 255 | * |
||
| 256 | * @return mixed |
||
| 257 | */ |
||
| 258 | protected function executionDelayed($delay = 0, $callback = null) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @param string $url |
||
| 272 | * @param $data |
||
| 273 | * @param $isPost |
||
| 274 | * |
||
| 275 | * @throws DeCaptchaErrors |
||
| 276 | * |
||
| 277 | * @return mixed |
||
| 278 | */ |
||
| 279 | protected function getCurlResponse($url, $data, $isPost = true) |
||
| 306 | |||
| 307 | abstract public function recognize($filePath); |
||
| 308 | |||
| 309 | abstract public function getCode(); |
||
| 310 | |||
| 311 | abstract public function getError(); |
||
| 312 | } |
||
| 313 |