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 = 1; |
||
13 | const RESPONSE_TYPE_JSON = 2; |
||
14 | |||
15 | const ACTION_FIELDS = 1; |
||
16 | const ACTION_URI = 2; |
||
17 | const ACTION_METHOD = 3; |
||
18 | const ACTION_JSON = 4; |
||
19 | |||
20 | const ACTION_METHOD_POST = 1; |
||
21 | const ACTION_METHOD_GET = 2; |
||
22 | |||
23 | const DECODE_FORMAT = 1; |
||
24 | const DECODE_ACTION = 2; |
||
25 | const DECODE_SEPARATOR = 3; |
||
26 | const DECODE_PARAMS = 4; |
||
27 | const DECODE_PARAM_SETTING_MARKER = 5; |
||
28 | |||
29 | const PARAM_FIELD_TYPE_STRING = 1; |
||
30 | const PARAM_FIELD_TYPE_INTEGER = 2; |
||
31 | const PARAM_FIELD_TYPE_MIX = 3; |
||
32 | const PARAM_FIELD_TYPE_OBJECT = 4; |
||
33 | const PARAM_FIELD_TYPE_BOOLEAN = 5; |
||
34 | |||
35 | const PARAM_SLUG_DEFAULT = 1; |
||
36 | const PARAM_SLUG_TYPE = 2; |
||
37 | const PARAM_SLUG_REQUIRE = 3; |
||
38 | const PARAM_SLUG_SPEC = 4; |
||
39 | const PARAM_SLUG_VARIABLE = 5; |
||
40 | const PARAM_SLUG_CODING = 6; |
||
41 | const PARAM_SLUG_NOTWIKI = 7; |
||
42 | const PARAM_SLUG_ENUM = 8; |
||
43 | const PARAM_SLUG_WIKI = 9; |
||
44 | |||
45 | const PARAM_SLUG_CODING_BASE64 = 1; |
||
46 | |||
47 | const PARAM_SPEC_API_KEY = -1; |
||
48 | const PARAM_SPEC_FILE = -2; |
||
49 | const PARAM_SPEC_CAPTCHA = -3; |
||
50 | const PARAM_SPEC_CODE = -4; |
||
51 | |||
52 | /** |
||
53 | * Сервис на который будем загружать капчу. |
||
54 | * |
||
55 | * @var string |
||
56 | */ |
||
57 | protected $host; |
||
58 | protected $scheme = 'http'; |
||
59 | protected $errorLang = DeCaptchaErrors::LANG_EN; |
||
60 | protected $lastRunTime = null; |
||
61 | /** @var DeCaptchaErrors */ |
||
62 | protected $errorObject; |
||
63 | protected $causeAnError = false; |
||
64 | |||
65 | protected $limit = []; |
||
66 | protected $paramsSpec = []; |
||
67 | protected $params = []; |
||
68 | protected $limitSettings = []; |
||
69 | protected $decodeSettings = []; |
||
70 | protected $actions = []; |
||
71 | protected $paramsNames = []; |
||
72 | |||
73 | protected function resetLimits() |
||
79 | |||
80 | /** |
||
81 | * @param $action |
||
82 | * |
||
83 | * @return bool |
||
84 | */ |
||
85 | protected function limitHasNotYetEnded($action) |
||
89 | |||
90 | /** |
||
91 | * @param $action |
||
92 | 3 | * @param $data |
|
93 | * |
||
94 | 3 | * @throws DeCaptchaErrors |
|
95 | 1 | * |
|
96 | 1 | * @return array |
|
97 | 3 | */ |
|
98 | protected function decodeResponse($action, $data) |
||
138 | |||
139 | /** |
||
140 | * @param $errorLang |
||
141 | */ |
||
142 | public function setErrorLang($errorLang) |
||
146 | |||
147 | /** |
||
148 | * Узнаём путь до файла |
||
149 | * Если передана ссылка, то скачиваем и кладём во временную директорию. |
||
150 | * |
||
151 | * @param string $fileName |
||
152 | 1 | * |
|
153 | 1 | * @throws Exception |
|
154 | * |
||
155 | * @return string |
||
156 | */ |
||
157 | protected function getFilePath($fileName) |
||
175 | |||
176 | /** |
||
177 | * @param $action |
||
178 | * |
||
179 | * @return string |
||
180 | */ |
||
181 | protected function getActionUrl($action) |
||
185 | 1 | ||
186 | 1 | /** |
|
187 | 1 | * @return string |
|
188 | 1 | */ |
|
189 | 1 | protected function getBaseUrl() |
|
193 | |||
194 | /** |
||
195 | * @param $params |
||
196 | */ |
||
197 | public function setParams($params) |
||
205 | |||
206 | /** |
||
207 | * @param $param |
||
208 | * @param $value |
||
209 | */ |
||
210 | public function setParam($param, $value) |
||
214 | |||
215 | /** |
||
216 | * @param $param |
||
217 | * @param $spec |
||
218 | * @param $coding |
||
219 | * |
||
220 | * @return \CURLFile|mixed|null|string |
||
221 | */ |
||
222 | public function getParamSpec($param, $spec = null, $coding = null) |
||
251 | |||
252 | /** |
||
253 | * @param $action |
||
254 | * @param $field |
||
255 | * |
||
256 | * @throws DeCaptchaErrors |
||
257 | * |
||
258 | * @return array |
||
259 | */ |
||
260 | protected function getParams($action, $field = null) |
||
311 | |||
312 | /** |
||
313 | * @param string $action |
||
314 | * |
||
315 | * @return string |
||
316 | */ |
||
317 | protected function getResponse($action) |
||
326 | |||
327 | /** |
||
328 | * Задержка выполнения. |
||
329 | * |
||
330 | * @param int $delay Количество секунд |
||
331 | * @param \Closure|null $callback |
||
332 | * |
||
333 | * @return mixed |
||
334 | */ |
||
335 | protected function executionDelayed($delay = 0, $callback = null) |
||
346 | |||
347 | /** |
||
348 | * @param string $url |
||
349 | * @param array $data |
||
350 | * @param bool $isPost |
||
351 | * @param bool $isJson |
||
352 | * |
||
353 | * @throws DeCaptchaErrors |
||
354 | * |
||
355 | * @return string |
||
356 | */ |
||
357 | protected function curlResponse($url, $data, $isPost = true, $isJson = false) |
||
396 | |||
397 | abstract public function getCode(); |
||
398 | |||
399 | abstract public function getError(); |
||
400 | } |
||
401 |