Complex classes like ApiClient 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 ApiClient, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class ApiClient extends \Ease\Brick |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * Version of php-primaerp library |
||
| 20 | * |
||
| 21 | * @var string |
||
| 22 | */ |
||
| 23 | public static $libVersion = '0.1'; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Communication protocol version used. |
||
| 27 | * |
||
| 28 | * @var string API version |
||
| 29 | */ |
||
| 30 | public $protoVersion = 'v1'; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * URL of object data in primaERP API |
||
| 34 | * @var string url |
||
| 35 | */ |
||
| 36 | public $apiURL = null; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Datový blok v poli odpovědi. |
||
| 40 | * Data block in response field. |
||
| 41 | * |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | public $resultField = 'results'; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Section used by object |
||
| 48 | * |
||
| 49 | * @link http://devdoc.primaerp.com/rest/index.html |
||
| 50 | * @var string |
||
| 51 | */ |
||
| 52 | public $section = null; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Curl Handle. |
||
| 56 | * |
||
| 57 | * @var resource |
||
| 58 | */ |
||
| 59 | public $curl = null; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * tenant |
||
| 63 | * |
||
| 64 | * @link http://devdoc.primaerp.com/rest/authentication.html Identifikátor firmy |
||
| 65 | * @var string |
||
| 66 | */ |
||
| 67 | public $company = null; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Server[:port] |
||
| 71 | * @var string |
||
| 72 | */ |
||
| 73 | public $url = null; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * REST API Username (usually user's email) |
||
| 77 | * @var string |
||
| 78 | */ |
||
| 79 | public $user = null; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * REST API Password |
||
| 83 | * @var string |
||
| 84 | */ |
||
| 85 | public $password = null; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * REST API Key |
||
| 89 | * @var string |
||
| 90 | */ |
||
| 91 | public $apikey = null; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var array Pole HTTP hlaviček odesílaných s každým požadavkem |
||
| 95 | */ |
||
| 96 | public $defaultHttpHeaders = ['User-Agent' => 'php-primaERP']; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Default additional request url parameters after question mark |
||
| 100 | * |
||
| 101 | * @var array |
||
| 102 | */ |
||
| 103 | public $defaultUrlParams = []; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Identifikační řetězec. |
||
| 107 | * |
||
| 108 | * @var string |
||
| 109 | */ |
||
| 110 | public $init = null; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Informace o posledním HTTP requestu. |
||
| 114 | * |
||
| 115 | * @var * |
||
| 116 | */ |
||
| 117 | public $curlInfo; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Informace o poslední HTTP chybě. |
||
| 121 | * |
||
| 122 | * @var string |
||
| 123 | */ |
||
| 124 | public $lastCurlError = null; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Used codes storage. |
||
| 128 | * |
||
| 129 | * @var array |
||
| 130 | */ |
||
| 131 | public $codes = null; |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Last Inserted ID. |
||
| 135 | * |
||
| 136 | * @var int |
||
| 137 | */ |
||
| 138 | public $lastInsertedID = null; |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Raw Content of last curl response |
||
| 142 | * |
||
| 143 | * @var string |
||
| 144 | */ |
||
| 145 | public $lastCurlResponse; |
||
| 146 | |||
| 147 | /** |
||
| 148 | * HTTP Response code of last request |
||
| 149 | * |
||
| 150 | * @var int |
||
| 151 | */ |
||
| 152 | public $lastResponseCode = null; |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Body data for next curl POST operation |
||
| 156 | * |
||
| 157 | * @var string |
||
| 158 | */ |
||
| 159 | protected $postFields = null; |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Last operation result data or message(s) |
||
| 163 | * |
||
| 164 | * @var array |
||
| 165 | */ |
||
| 166 | public $lastResult = null; |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Nuber from @rowCount |
||
| 170 | * @var int |
||
| 171 | */ |
||
| 172 | public $rowCount = null; |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Save 404 results to log ? |
||
| 176 | * @var boolean |
||
| 177 | */ |
||
| 178 | protected $ignoreNotFound = false; |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Array of errors caused by last request |
||
| 182 | * @var array |
||
| 183 | */ |
||
| 184 | private $errors = []; |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Access Token Info |
||
| 188 | * @var Token |
||
| 189 | */ |
||
| 190 | protected $tokener = null; |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Class for read only interaction with IPEX. |
||
| 194 | * |
||
| 195 | * @param mixed $init default record id or initial data |
||
| 196 | * @param array $options Connection settings override |
||
| 197 | */ |
||
| 198 | 1 | public function __construct($init = null, $options = []) |
|
| 214 | |||
| 215 | /** |
||
| 216 | * SetUp Object to be ready for connect |
||
| 217 | * |
||
| 218 | * @param array $options Object Options (company,url,user,password,section, |
||
| 219 | * defaultUrlParams,debug) |
||
| 220 | */ |
||
| 221 | 2 | public function setUp($options = []) |
|
| 234 | |||
| 235 | /** |
||
| 236 | * Inicializace CURL |
||
| 237 | */ |
||
| 238 | 2 | public function curlInit() |
|
| 250 | |||
| 251 | /** |
||
| 252 | * Zinicializuje objekt dle daných dat. Možné hodnoty: |
||
| 253 | * |
||
| 254 | * * 234 - interní číslo záznamu k načtení |
||
| 255 | * * code:LOPATA - kód záznamu |
||
| 256 | * * BAGR - kód záznamu ka načtení |
||
| 257 | * * ['id'=>24,'nazev'=>'hoblík'] - pole hodnot k předvyplnění |
||
| 258 | * * 743.json?relations=adresa,vazby - část url s parametry k načtení |
||
| 259 | * |
||
| 260 | * @param mixed $init číslo/"(code:)kód"/(část)URI záznamu k načtení | pole hodnot k předvyplnění |
||
| 261 | */ |
||
| 262 | 2 | public function processInit($init) |
|
| 268 | |||
| 269 | /** |
||
| 270 | * Nastaví Sekci pro Komunikaci. |
||
| 271 | * Set section for communication |
||
| 272 | * |
||
| 273 | * @param string $section section pathName to use |
||
| 274 | * @return boolean section switching status |
||
| 275 | */ |
||
| 276 | 1 | public function setSection($section) |
|
| 281 | |||
| 282 | /** |
||
| 283 | * Vrací právě používanou evidenci pro komunikaci |
||
| 284 | * Obtain current used section |
||
| 285 | * |
||
| 286 | * @return string |
||
| 287 | */ |
||
| 288 | 2 | public function getSection() |
|
| 292 | |||
| 293 | /** |
||
| 294 | * Převede rekurzivně Objekt na pole. |
||
| 295 | * |
||
| 296 | * @param object|array $object |
||
| 297 | * |
||
| 298 | * @return array |
||
| 299 | */ |
||
| 300 | 1 | public static function object2array($object) |
|
| 320 | |||
| 321 | /** |
||
| 322 | * Připraví data pro odeslání do FlexiBee |
||
| 323 | * |
||
| 324 | * @param string $data |
||
| 325 | */ |
||
| 326 | 1 | public function setPostFields($data) |
|
| 330 | |||
| 331 | /** |
||
| 332 | * Return basic URL for used Evidence |
||
| 333 | * |
||
| 334 | * @return string Evidence URL |
||
| 335 | */ |
||
| 336 | 2 | public function getSectionURL() |
|
| 345 | |||
| 346 | /** |
||
| 347 | * Add suffix to Evidence URL |
||
| 348 | * |
||
| 349 | * @param string $urlSuffix |
||
| 350 | * |
||
| 351 | * @return string |
||
| 352 | */ |
||
| 353 | 2 | public function sectionUrlWithSuffix($urlSuffix) |
|
| 361 | |||
| 362 | /** |
||
| 363 | * Update $this->apiURL |
||
| 364 | */ |
||
| 365 | 2 | public function updateApiURL() |
|
| 369 | |||
| 370 | /** |
||
| 371 | * Funkce, která provede I/O operaci a vyhodnotí výsledek. |
||
| 372 | * |
||
| 373 | * @param string $urlSuffix část URL za identifikátorem firmy. |
||
| 374 | * @param string $method HTTP/REST metoda |
||
| 375 | * |
||
| 376 | * @return array|boolean Výsledek operace |
||
| 377 | */ |
||
| 378 | 1 | public function requestData($urlSuffix = null, $method = 'GET') |
|
| 399 | |||
| 400 | 2 | public function authentication() |
|
| 406 | |||
| 407 | /** |
||
| 408 | * Add params to url |
||
| 409 | * |
||
| 410 | * @param string $url originall url |
||
| 411 | * @param array $params value to add |
||
| 412 | * @param boolean $override replace already existing values ? |
||
| 413 | * |
||
| 414 | * @return string url with parameters added |
||
| 415 | */ |
||
| 416 | 2 | public function addUrlParams($url, $params, $override = false) |
|
| 435 | |||
| 436 | /** |
||
| 437 | * Add Default Url params to given url if not overrided |
||
| 438 | * |
||
| 439 | * @param string $urlRaw |
||
| 440 | * |
||
| 441 | * @return string url with default params added |
||
| 442 | */ |
||
| 443 | 2 | public function addDefaultUrlParams($urlRaw) |
|
| 447 | |||
| 448 | /** |
||
| 449 | * Parse primaERP API Response |
||
| 450 | * |
||
| 451 | * @param string $responseRaw raw response body |
||
| 452 | * |
||
| 453 | * @return array |
||
| 454 | */ |
||
| 455 | 2 | public function rawResponseToArray($responseRaw) |
|
| 465 | |||
| 466 | /** |
||
| 467 | * Parse Response array |
||
| 468 | * |
||
| 469 | * @param array $responseDecoded |
||
| 470 | * @param int $responseCode Request Response Code |
||
| 471 | * |
||
| 472 | * @return array main data part of response |
||
| 473 | */ |
||
| 474 | 1 | public function parseResponse($responseDecoded, $responseCode) |
|
| 507 | |||
| 508 | /** |
||
| 509 | * Parse error message response |
||
| 510 | * |
||
| 511 | * @param array $responseDecoded |
||
| 512 | * @return int number of errors processed |
||
| 513 | */ |
||
| 514 | 1 | public function parseError(array $responseDecoded) |
|
| 519 | |||
| 520 | /** |
||
| 521 | * Vykonej HTTP požadavek |
||
| 522 | * |
||
| 523 | * @link https://www.ipex.eu/api/dokumentace/ref/urls/ Sestavování URL |
||
| 524 | * @param string $url URL požadavku |
||
| 525 | * @param string $method HTTP Method GET|POST|PUT|OPTIONS|DELETE |
||
| 526 | * @return int HTTP Response CODE |
||
| 527 | */ |
||
| 528 | 2 | public function doCurlRequest($url, $method) |
|
| 573 | |||
| 574 | 1 | public function loadFromAPI($key) |
|
| 578 | |||
| 579 | /** |
||
| 580 | * Write Operation Result. |
||
| 581 | * |
||
| 582 | * @param array $resultData |
||
| 583 | * @param string $url URL |
||
| 584 | * @return boolean Log save success |
||
| 585 | */ |
||
| 586 | 2 | public function logResult($resultData = null, $url = null) |
|
| 603 | |||
| 604 | /** |
||
| 605 | * Current Token String |
||
| 606 | * |
||
| 607 | * @return string |
||
| 608 | */ |
||
| 609 | 2 | public function getTokenString() |
|
| 613 | |||
| 614 | /** |
||
| 615 | * Set or get ignore not found pages flag |
||
| 616 | * |
||
| 617 | * @param boolean $ignore set flag to |
||
| 618 | * |
||
| 619 | * @return boolean get flag state |
||
| 620 | */ |
||
| 621 | 1 | public function ignore404($ignore = null) |
|
| 628 | |||
| 629 | /** |
||
| 630 | * Odpojení od primaERP. |
||
| 631 | */ |
||
| 632 | 2 | public function disconnect() |
|
| 639 | |||
| 640 | /** |
||
| 641 | * Reconnect After unserialization |
||
| 642 | */ |
||
| 643 | 1 | public function __wakeup() |
|
| 648 | |||
| 649 | /** |
||
| 650 | * Disconnect CURL befere pass away |
||
| 651 | */ |
||
| 652 | 2 | public function __destruct() |
|
| 656 | } |
||
| 657 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..