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 | /** |
||
| 16 | * Сервис на который будем загружать капчу. |
||
| 17 | * |
||
| 18 | * @var string |
||
| 19 | */ |
||
| 20 | protected $domain; |
||
| 21 | |||
| 22 | protected $errorLang = DeCaptchaErrors::LANG_EN; |
||
| 23 | |||
| 24 | protected $responseType = self::RESPONSE_TYPE_STRING; |
||
| 25 | /** |
||
| 26 | * Ваш API key. |
||
| 27 | * |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | protected $apiKey; |
||
| 31 | /** |
||
| 32 | * @var int |
||
| 33 | */ |
||
| 34 | protected $captchaId; |
||
| 35 | |||
| 36 | protected $inUrl = 'in.php'; |
||
| 37 | protected $resUrl = 'res.php'; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @return void |
||
| 41 | */ |
||
| 42 | abstract public function notTrue(); |
||
| 43 | |||
| 44 | public function setErrorLang($errorLang) |
||
| 45 | { |
||
| 46 | $this->errorLang = $errorLang; |
||
| 47 | } |
||
| 48 | |||
| 49 | abstract protected function decodeResponse($data, $type, $format = self::RESPONSE_TYPE_STRING); |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Узнаём путь до файла |
||
| 53 | * Если передана ссылка, то скачиваем и кладём во временную директорию. |
||
| 54 | * |
||
| 55 | * @param string $fileName |
||
| 56 | * |
||
| 57 | * @throws Exception |
||
| 58 | * |
||
| 59 | * @return string |
||
| 60 | */ |
||
| 61 | protected function getFilePath($fileName) |
||
| 62 | { |
||
| 63 | if (strpos($fileName, 'http://') !== false || strpos($fileName, 'https://') !== false) { |
||
| 64 | try { |
||
| 65 | $current = file_get_contents($fileName); |
||
| 66 | } catch (\Exception $e) { |
||
| 67 | throw new DeCaptchaErrors(DeCaptchaErrors::ERROR_FILE_IS_NOT_LOADED, $fileName, $this->errorLang); |
||
| 68 | } |
||
| 69 | $path = tempnam(sys_get_temp_dir(), 'captcha'); |
||
| 70 | file_put_contents($path, $current); |
||
| 71 | |||
| 72 | return $path; |
||
| 73 | } |
||
| 74 | if (file_exists($fileName)) { |
||
| 75 | return $fileName; |
||
| 76 | } |
||
| 77 | throw new DeCaptchaErrors(DeCaptchaErrors::ERROR_FILE_NOT_FOUND, $fileName, $this->errorLang); |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @return string |
||
| 82 | */ |
||
| 83 | protected function getBaseUrl() |
||
| 87 | |||
| 88 | const PARAM_FIELD_TYPE_STRING = 0; |
||
| 89 | const PARAM_FIELD_TYPE_INTEGER = 1; |
||
| 90 | |||
| 91 | const PARAM_SLUG_DEFAULT = 0; |
||
| 92 | 3 | const PARAM_SLUG_TYPE = 1; |
|
| 93 | const PARAM_SLUG_REQUIRE = 2; |
||
| 94 | 3 | const PARAM_SLUG_SPEC = 3; |
|
| 95 | 1 | const PARAM_SLUG_VARIABLE = 4; |
|
| 96 | 1 | ||
| 97 | 3 | const PARAM_SPEC_KEY = 0; |
|
| 98 | const PARAM_SPEC_FILE = 1; |
||
| 99 | 3 | const PARAM_SPEC_CAPTCHA = 2; |
|
| 100 | |||
| 101 | protected $paramsNames = []; |
||
| 102 | |||
| 103 | protected $paramsSettings = []; |
||
| 104 | |||
| 105 | protected $paramsSpec = []; |
||
| 106 | |||
| 107 | protected $params = []; |
||
| 108 | |||
| 109 | public function setParams($params) |
||
| 117 | 2 | ||
| 118 | 1 | public function setParamSpec($param, $value) |
|
| 122 | |||
| 123 | public function getParamSpec($param) |
||
| 138 | |||
| 139 | protected function getParams($action) |
||
| 172 | 1 | ||
| 173 | /** |
||
| 174 | 1 | * @param array $data |
|
| 175 | * |
||
| 176 | * @return string |
||
| 177 | */ |
||
| 178 | protected function getActionUrl($data) |
||
| 186 | 1 | ||
| 187 | 1 | /** |
|
| 188 | 1 | * @param string $action |
|
| 189 | 1 | * |
|
| 190 | 1 | * @return string |
|
| 191 | 1 | */ |
|
| 192 | 1 | protected function getResponse($action) |
|
| 196 | |||
| 197 | /** |
||
| 198 | * @return string |
||
| 199 | */ |
||
| 200 | protected function getInUrl() |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Проверка на то произошла ли ошибка. |
||
| 207 | * |
||
| 208 | * @param $error |
||
| 209 | * |
||
| 210 | * @throws DeCaptchaErrors |
||
| 211 | */ |
||
| 212 | protected function isError($error) |
||
| 218 | |||
| 219 | protected $lastRunTime = null; |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Задержка выполнения. |
||
| 223 | * |
||
| 224 | * @param int $delay Количество секунд |
||
| 225 | * @param \Closure|null $callback |
||
| 226 | * |
||
| 227 | * @return mixed |
||
| 228 | */ |
||
| 229 | protected function executionDelayed($delay = 0, $callback = null) |
||
| 240 | |||
| 241 | /** |
||
| 242 | * @param $url |
||
| 243 | * @param $data |
||
| 244 | * @return mixed |
||
| 245 | * @throws DeCaptchaErrors |
||
| 246 | */ |
||
| 247 | protected function getCurlResponse($url, $data) |
||
| 266 | } |
||
| 267 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: