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 | public function notTrue() |
||
| 81 | |||
| 82 | protected function decodeResponse($action, $data) |
||
| 106 | |||
| 107 | public function setErrorLang($errorLang) |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Узнаём путь до файла |
||
| 114 | 3 | * Если передана ссылка, то скачиваем и кладём во временную директорию. |
|
| 115 | 3 | * |
|
| 116 | 2 | * @param string $fileName |
|
| 117 | 2 | * |
|
| 118 | 1 | * @throws Exception |
|
| 119 | 1 | * |
|
| 120 | 1 | * @return string |
|
| 121 | 1 | */ |
|
| 122 | protected function getFilePath($fileName) |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @param $action |
||
| 143 | * |
||
| 144 | 2 | * @return string |
|
| 145 | 2 | */ |
|
| 146 | protected function getActionUrl($action) |
||
| 150 | |||
| 151 | /** |
||
| 152 | 1 | * @return string |
|
| 153 | 1 | */ |
|
| 154 | protected function getBaseUrl() |
||
| 158 | |||
| 159 | 1 | public function setParams($params) |
|
| 167 | |||
| 168 | public function setParamSpec($param, $value) |
||
| 172 | 1 | ||
| 173 | public function getParamSpec($param) |
||
| 190 | 1 | ||
| 191 | 1 | protected function getParams($action) |
|
| 225 | |||
| 226 | /** |
||
| 227 | * @param string $action |
||
| 228 | * |
||
| 229 | * @return string |
||
| 230 | */ |
||
| 231 | protected function getResponse($action) |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Задержка выполнения. |
||
| 238 | * |
||
| 239 | * @param int $delay Количество секунд |
||
| 240 | * @param \Closure|null $callback |
||
| 241 | * |
||
| 242 | * @return mixed |
||
| 243 | */ |
||
| 244 | protected function executionDelayed($delay = 0, $callback = null) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * @param string $url |
||
| 258 | * @param $data |
||
| 259 | * @param $isPost |
||
| 260 | * |
||
| 261 | * @throws DeCaptchaErrors |
||
| 262 | * |
||
| 263 | * @return mixed |
||
| 264 | */ |
||
| 265 | protected function getCurlResponse($url, $data, $isPost = true) |
||
| 292 | |||
| 293 | abstract public function getCode(); |
||
| 294 | |||
| 295 | abstract public function getError(); |
||
| 296 | } |
||
| 297 |