Complex classes like Service 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 Service, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class Service extends Singleton |
||
| 12 | { |
||
| 13 | public const CTYPE_JSON = 'application/json'; |
||
| 14 | public const CTYPE_MULTIPART = 'multipart/form-data'; |
||
| 15 | public const CTYPE_FORM = 'application/x-www-form-urlencoded'; |
||
| 16 | public const CTYPE_PLAIN = 'text/plain'; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var String Url de destino de la llamada |
||
| 20 | */ |
||
| 21 | private $url; |
||
| 22 | /** |
||
| 23 | * @var array Parámetros de la llamada |
||
| 24 | */ |
||
| 25 | private $params; |
||
| 26 | /** |
||
| 27 | * @var array Opciones llamada |
||
| 28 | */ |
||
| 29 | private $options; |
||
| 30 | /** |
||
| 31 | * @var array Cabeceras de la llamada |
||
| 32 | */ |
||
| 33 | private $headers; |
||
| 34 | /** |
||
| 35 | * @var string type |
||
| 36 | */ |
||
| 37 | private $type; |
||
| 38 | /** |
||
| 39 | * @var resource $con |
||
| 40 | */ |
||
| 41 | private $con; |
||
| 42 | /** |
||
| 43 | * @var string $result |
||
| 44 | */ |
||
| 45 | private $result; |
||
| 46 | /** |
||
| 47 | * @var mixed |
||
| 48 | */ |
||
| 49 | private $info = []; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @Injectable |
||
| 53 | * @var \PSFS\base\Logger Log de las llamadas |
||
| 54 | */ |
||
| 55 | protected $log; |
||
| 56 | /** |
||
| 57 | * @Injectable |
||
| 58 | * @var \PSFS\base\Cache $cache |
||
| 59 | */ |
||
| 60 | protected $cache; |
||
| 61 | /** |
||
| 62 | * @var bool |
||
| 63 | */ |
||
| 64 | protected $isJson = true; |
||
| 65 | /** |
||
| 66 | * @var bool |
||
| 67 | */ |
||
| 68 | protected $isMultipart = false; |
||
| 69 | |||
| 70 | 1 | private function closeConnection() { |
|
| 75 | |||
| 76 | public function __destruct() |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @return String |
||
| 83 | */ |
||
| 84 | public function getUrl() |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @param String $url |
||
| 91 | */ |
||
| 92 | public function setUrl($url) |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @return string |
||
| 100 | */ |
||
| 101 | public function getResult() |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @param string $result |
||
| 108 | */ |
||
| 109 | public function setResult($result) |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @return array |
||
| 116 | */ |
||
| 117 | public function getParams() |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Add request param |
||
| 124 | * |
||
| 125 | * @param $key |
||
| 126 | * @param null $value |
||
| 127 | * |
||
| 128 | * @return \PSFS\base\Service |
||
| 129 | */ |
||
| 130 | public function addParam($key, $value = NULL) |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @return array |
||
| 139 | */ |
||
| 140 | public function getOptions() |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Add request param |
||
| 147 | * |
||
| 148 | * @param $key |
||
| 149 | * @param null $value |
||
| 150 | * |
||
| 151 | * @return \PSFS\base\Service |
||
| 152 | */ |
||
| 153 | public function addOption($key, $value = NULL) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @param array $params |
||
| 162 | */ |
||
| 163 | public function setParams($params) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @return array |
||
| 170 | */ |
||
| 171 | public function getHeaders() |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @param array $headers |
||
| 178 | */ |
||
| 179 | public function setHeaders($headers) |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @param $header |
||
| 186 | * @param null $content |
||
| 187 | * |
||
| 188 | * @return $this |
||
| 189 | */ |
||
| 190 | public function addHeader($header, $content = NULL) |
||
| 196 | |||
| 197 | /** |
||
| 198 | * @return string |
||
| 199 | */ |
||
| 200 | public function getType() |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @param string $type |
||
| 207 | */ |
||
| 208 | public function setType($type) |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @return Logger |
||
| 215 | */ |
||
| 216 | public function getLog() |
||
| 220 | |||
| 221 | /** |
||
| 222 | * @param Logger $log |
||
| 223 | */ |
||
| 224 | 1 | public function setLog($log) |
|
| 228 | |||
| 229 | /** |
||
| 230 | * @param bool $isJson |
||
| 231 | */ |
||
| 232 | public function setIsJson($isJson = true) { |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @return bool |
||
| 241 | */ |
||
| 242 | public function getIsJson() { |
||
| 245 | |||
| 246 | /** |
||
| 247 | * @param bool $isMultipart |
||
| 248 | */ |
||
| 249 | public function setIsMultipart($isMultipart = true) { |
||
| 255 | |||
| 256 | /** |
||
| 257 | * @return bool |
||
| 258 | */ |
||
| 259 | public function getIsMultipart() { |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Método que limpia el contexto de la llamada |
||
| 265 | */ |
||
| 266 | 1 | private function clearContext() |
|
| 274 | |||
| 275 | /** |
||
| 276 | * |
||
| 277 | */ |
||
| 278 | 1 | public function init() |
|
| 283 | |||
| 284 | /** |
||
| 285 | * Initialize CURL |
||
| 286 | */ |
||
| 287 | private function initialize() |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Generate auth header |
||
| 296 | * @param string $secret |
||
| 297 | * @param string $module |
||
| 298 | */ |
||
| 299 | protected function addRequestToken($secret, $module = 'PSFS') |
||
| 303 | |||
| 304 | /** |
||
| 305 | * @param $user |
||
| 306 | * @param $pass |
||
| 307 | */ |
||
| 308 | protected function addAuthHeader($user, $pass) { |
||
| 312 | |||
| 313 | protected function applyOptions() { |
||
| 318 | |||
| 319 | protected function applyHeaders() { |
||
| 328 | |||
| 329 | protected function setDefaults() |
||
| 330 | { |
||
| 331 | switch (strtoupper($this->type)) { |
||
| 332 | case Request::VERB_GET: |
||
| 333 | default: |
||
| 334 | $this->addOption(CURLOPT_CUSTOMREQUEST, Request::VERB_GET); |
||
| 335 | if(!empty($this->params)) { |
||
| 336 | $sep = !preg_match('/\?/', $this->getUrl()) ? '?' : ''; |
||
| 337 | $this->url = $this->url . $sep . http_build_query($this->params); |
||
| 338 | } |
||
| 339 | break; |
||
| 340 | case Request::VERB_POST: |
||
| 341 | $this->addOption(CURLOPT_CUSTOMREQUEST, Request::VERB_POST); |
||
| 342 | if($this->getIsJson()) { |
||
| 343 | $this->addOption(CURLOPT_POSTFIELDS, json_encode($this->params)); |
||
| 344 | } elseif($this->getIsMultipart()) { |
||
| 345 | $this->addOption(CURLOPT_POSTFIELDS, $this->params); |
||
| 346 | } else { |
||
| 347 | $this->addOption(CURLOPT_POSTFIELDS, http_build_query($this->params)); |
||
| 348 | } |
||
| 349 | break; |
||
| 350 | case Request::VERB_DELETE: |
||
| 351 | $this->addOption(CURLOPT_CUSTOMREQUEST, Request::VERB_DELETE); |
||
| 352 | break; |
||
| 353 | case Request::VERB_PUT: |
||
| 354 | $this->addOption(CURLOPT_CUSTOMREQUEST, Request::VERB_PUT); |
||
| 355 | |||
| 356 | if($this->getIsJson()) { |
||
| 357 | $this->addOption(CURLOPT_POSTFIELDS, json_encode($this->params)); |
||
| 358 | } elseif($this->getIsMultipart()) { |
||
| 359 | $this->addOption(CURLOPT_POSTFIELDS, $this->params); |
||
| 360 | } else { |
||
| 361 | $this->addOption(CURLOPT_POSTFIELDS, http_build_query($this->params)); |
||
| 362 | } |
||
| 363 | break; |
||
| 364 | case Request::VERB_PATCH: |
||
| 365 | $this->addOption(CURLOPT_CUSTOMREQUEST, Request::VERB_PATCH); |
||
| 366 | if($this->getIsJson()) { |
||
| 367 | $this->addOption(CURLOPT_POSTFIELDS, json_encode($this->params)); |
||
| 368 | } elseif($this->getIsMultipart()) { |
||
| 369 | $this->addOption(CURLOPT_POSTFIELDS, $this->params); |
||
| 370 | } else { |
||
| 371 | $this->addOption(CURLOPT_POSTFIELDS, http_build_query($this->params)); |
||
| 372 | } |
||
| 373 | break; |
||
| 374 | } |
||
| 375 | |||
| 376 | $this->addOption(CURLOPT_RETURNTRANSFER, true); |
||
| 377 | $this->addOption(CURLOPT_FOLLOWLOCATION, true); |
||
| 378 | $this->addOption(CURLOPT_SSL_VERIFYHOST, false); |
||
| 379 | $this->addOption(CURLOPT_SSL_VERIFYPEER, false); |
||
| 380 | } |
||
| 381 | |||
| 382 | public function callSrv() |
||
| 383 | { |
||
| 384 | $this->setDefaults(); |
||
| 385 | $this->applyOptions(); |
||
| 386 | $this->applyHeaders(); |
||
| 387 | if('debug' === Config::getParam('log.level')) { |
||
| 388 | curl_setopt($this->con, CURLOPT_VERBOSE, true); |
||
| 389 | $verbose = fopen('php://temp', 'w+'); |
||
| 390 | curl_setopt($this->con, CURLOPT_STDERR, $verbose); |
||
| 391 | } |
||
| 392 | $result = curl_exec($this->con); |
||
| 393 | $this->result = $this->isJson ? json_decode($result, true) : $result; |
||
| 394 | if('debug' === Config::getParam('log.level')) { |
||
| 395 | rewind($verbose); |
||
|
|
|||
| 396 | $verboseLog = stream_get_contents($verbose); |
||
| 397 | Logger::log($verboseLog, LOG_DEBUG, [ |
||
| 398 | 'headers' => $this->getHeaders(), |
||
| 399 | 'options' => $this->getOptions(), |
||
| 400 | 'url' => $this->getUrl(), |
||
| 401 | ]); |
||
| 402 | $this->info['verbose'] = $verboseLog; |
||
| 403 | } |
||
| 404 | Logger::log($this->url . ' response: ', LOG_DEBUG, is_array($this->result) ? $this->result : [$this->result]); |
||
| 405 | $this->info = array_merge($this->info, curl_getinfo($this->con)); |
||
| 406 | } |
||
| 407 | |||
| 408 | /** |
||
| 409 | * @return mixed |
||
| 410 | */ |
||
| 411 | public function getCallInfo() { |
||
| 414 | |||
| 415 | } |
||
| 416 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: