Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like FlexiBee 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 FlexiBee, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class FlexiBee extends \Ease\Brick |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * Základní namespace pro komunikaci s FlexiBEE. |
||
| 15 | * |
||
| 16 | * @var string Jmený prostor datového bloku odpovědi |
||
| 17 | */ |
||
| 18 | public $nameSpace = 'winstrom'; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Datový blok v poli odpovědi. |
||
| 22 | * |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | public $resultField = 'results'; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Verze protokolu použitého pro komunikaci. |
||
| 29 | * |
||
| 30 | * @var string Verze použitého API |
||
| 31 | */ |
||
| 32 | public $protoVersion = '1.0'; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Agenda užitá objektem. |
||
| 36 | * |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | public $agenda = null; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Výchozí formát pro komunikaci. |
||
| 43 | * |
||
| 44 | * @link https://www.flexibee.eu/api/dokumentace/ref/format-types Přehled možných formátů |
||
| 45 | * |
||
| 46 | * @var string json|xml|... |
||
| 47 | */ |
||
| 48 | public $format = 'json'; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Curl Handle. |
||
| 52 | * |
||
| 53 | * @var resource |
||
| 54 | */ |
||
| 55 | public $curl = null; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var type |
||
| 59 | */ |
||
| 60 | public $company = FLEXIBEE_COMPANY; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var string |
||
| 64 | */ |
||
| 65 | public $url = FLEXIBEE_URL; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var string |
||
| 69 | */ |
||
| 70 | public $user = FLEXIBEE_LOGIN; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var string |
||
| 74 | */ |
||
| 75 | public $password = FLEXIBEE_PASSWORD; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Identifikační řetězec. |
||
| 79 | * |
||
| 80 | * @var string |
||
| 81 | */ |
||
| 82 | public $init = null; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Sloupeček s názvem. |
||
| 86 | * |
||
| 87 | * @var string |
||
| 88 | */ |
||
| 89 | public $nameColumn = 'nazev'; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Sloupeček obsahující datum vložení záznamu do shopu. |
||
| 93 | * |
||
| 94 | * @var string |
||
| 95 | */ |
||
| 96 | public $myCreateColumn = 'false'; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Slopecek obsahujici datum poslení modifikace záznamu do shopu. |
||
| 100 | * |
||
| 101 | * @var string |
||
| 102 | */ |
||
| 103 | public $myLastModifiedColumn = 'lastUpdate'; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Klíčový idendifikátor záznamu. |
||
| 107 | * |
||
| 108 | * @var string |
||
| 109 | */ |
||
| 110 | public $fbKeyColumn = 'id'; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Informace o posledním HTTP requestu. |
||
| 114 | * |
||
| 115 | * @var array |
||
| 116 | */ |
||
| 117 | public $info; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Informace o poslední HTTP chybě. |
||
| 121 | * |
||
| 122 | * @var array |
||
| 123 | */ |
||
| 124 | public $error; |
||
| 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 | * Default Line Prefix. |
||
| 142 | * |
||
| 143 | * @var string |
||
| 144 | */ |
||
| 145 | public $prefix = '/c/'; |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Třída pro práci s FlexiBee. |
||
| 149 | * |
||
| 150 | * @param string $init výchozí selektor dat |
||
| 151 | */ |
||
| 152 | 13 | public function __construct($init = null) |
|
| 159 | |||
| 160 | 13 | public function curlInit() |
|
| 172 | |||
| 173 | /** |
||
| 174 | * Nastaví Agendu pro Komunikaci. |
||
| 175 | * |
||
| 176 | * @param string $agenda |
||
| 177 | */ |
||
| 178 | 13 | public function setAgenda($agenda) |
|
| 182 | |||
| 183 | /** |
||
| 184 | * Převede rekurzivně Objekt na pole. |
||
| 185 | * |
||
| 186 | * @param object|array $object |
||
| 187 | * |
||
| 188 | * @return array |
||
| 189 | */ |
||
| 190 | 13 | public static function object2array($object) |
|
| 204 | |||
| 205 | /** |
||
| 206 | * Funkce, která provede I/O operaci a vyhodnotí výsledek. |
||
| 207 | * |
||
| 208 | * @param string $urlSuffix část URL za identifikátorem firmy. |
||
| 209 | * @param string $method HTTP/REST metoda |
||
| 210 | * @param string $format Requested format |
||
| 211 | */ |
||
| 212 | 13 | public function performRequest($urlSuffix = null, $method = 'GET', |
|
| 285 | |||
| 286 | /** |
||
| 287 | * Give you last inserted record ID. |
||
| 288 | * |
||
| 289 | * @return int |
||
| 290 | */ |
||
| 291 | public function getLastImportId() |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Convert XML to array. |
||
| 298 | * |
||
| 299 | * @param string $xml |
||
| 300 | * |
||
| 301 | * @return array |
||
| 302 | */ |
||
| 303 | public static function xml2array($xml) |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Odpojení od FlexiBee. |
||
| 320 | */ |
||
| 321 | 1 | public function disconnect() |
|
| 327 | |||
| 328 | 1 | public function __destruct() |
|
| 332 | |||
| 333 | /** |
||
| 334 | * Načte data z FlexiBee. |
||
| 335 | * |
||
| 336 | * @param string $suffix dotaz |
||
| 337 | */ |
||
| 338 | public function loadFlexiData($suffix = null) |
||
| 339 | { |
||
| 340 | return $this->takeData($this->getFlexiData($suffix)); |
||
| 341 | } |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Načte řádek dat z FlexiBee. |
||
| 345 | * |
||
| 346 | * @param int $recordID |
||
| 347 | * |
||
| 348 | * @return array |
||
| 349 | */ |
||
| 350 | public function getFlexiRow($recordID) |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Načte data z FlexiBee. |
||
| 363 | * |
||
| 364 | * @param string $suffix dotaz |
||
| 365 | * @param string $conditions Volitelný filtrovací výraz |
||
| 366 | */ |
||
| 367 | 13 | public function getFlexiData($suffix = null, $conditions = null) |
|
| 368 | { |
||
| 369 | 13 | if (!is_null($conditions)) { |
|
| 370 | if ($conditions[0] != '/') { |
||
| 371 | $conditions = '/'.rawurlencode('('.($conditions).')'); |
||
| 372 | } |
||
| 373 | } else { |
||
| 374 | 13 | $conditions = ''; |
|
| 375 | } |
||
| 376 | 13 | if ($suffix) { |
|
| 377 | $transactions = $this->performRequest($this->agenda.$conditions.'.'.$this->format.'?'.$suffix, |
||
| 378 | 'GET'); |
||
| 379 | } else { |
||
| 380 | 13 | $transactions = $this->performRequest($this->agenda.$conditions.'.'.$this->format, |
|
| 381 | 13 | 'GET'); |
|
| 382 | } |
||
| 383 | 13 | if (isset($transactions[$this->agenda])) { |
|
| 384 | $result = $transactions[$this->agenda]; |
||
| 385 | } else { |
||
| 386 | 13 | $result = $transactions; |
|
| 387 | } |
||
| 388 | |||
| 389 | 13 | return $result; |
|
| 390 | } |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Načte záznam z FlexiBee. |
||
| 394 | * |
||
| 395 | * @param int $id ID záznamu |
||
| 396 | * |
||
| 397 | * @return int počet načtených položek |
||
| 398 | */ |
||
| 399 | public function loadFromFlexiBee($id = null) |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Uloží data do FlexiBee. |
||
| 410 | * |
||
| 411 | * @param array $data |
||
| 412 | * |
||
| 413 | * @return array výsledek |
||
| 414 | */ |
||
| 415 | View Code Duplication | public function saveToFlexiBee($data = null) |
|
| 427 | |||
| 428 | /** |
||
| 429 | * Převede data do Json formátu pro FlexiBee. |
||
| 430 | * |
||
| 431 | * @param array $data |
||
| 432 | * |
||
| 433 | * @return string |
||
| 434 | */ |
||
| 435 | 13 | public function jsonizeData($data) |
|
| 446 | |||
| 447 | /** |
||
| 448 | * Uloží záznam. |
||
| 449 | * |
||
| 450 | * @param array $data |
||
| 451 | * |
||
| 452 | * @return array odpověď |
||
| 453 | */ |
||
| 454 | View Code Duplication | public function insertToFlexiBee($data = null) |
|
| 464 | |||
| 465 | /** |
||
| 466 | * Test if given record ID exists in FlexiBee. |
||
| 467 | * |
||
| 468 | * @param string|int $identifer |
||
| 469 | */ |
||
| 470 | public function idExists($identifer = null) |
||
| 480 | |||
| 481 | /** |
||
| 482 | * Test if given record exists in FlexiBee. |
||
| 483 | * |
||
| 484 | * @param array $data |
||
| 485 | */ |
||
| 486 | public function recordExists($data = null) |
||
| 497 | |||
| 498 | /** |
||
| 499 | * Vrací z FlexiBee sloupečky podle podmínek. |
||
| 500 | * |
||
| 501 | * @param array|int|string $conditions pole podmínek nebo ID záznamu |
||
| 502 | * @param array|string $orderBy třídit dle |
||
| 503 | * @param string $indexBy klice vysledku naplnit hodnotou ze |
||
| 504 | * sloupečku |
||
| 505 | * @param int $limit maximální počet vrácených záznamů |
||
| 506 | * |
||
| 507 | * @return array |
||
| 508 | */ |
||
| 509 | public function getAllFromFlexibee($conditions = null, $orderBy = null, |
||
| 528 | |||
| 529 | /** |
||
| 530 | * Vrací z FlexiBee sloupečky podle podmínek. |
||
| 531 | * |
||
| 532 | * @param string[] $columnsList seznam položek |
||
| 533 | * @param array|int|string $conditions pole podmínek nebo ID záznamu |
||
| 534 | * @param array|string $orderBy třídit dle |
||
| 535 | * @param string $indexBy klice vysledku naplnit hodnotou ze |
||
| 536 | * sloupečku |
||
| 537 | * @param int $limit maximální počet vrácených záznamů |
||
| 538 | * |
||
| 539 | * @return array |
||
| 540 | */ |
||
| 541 | public function getColumnsFromFlexibee($columnsList, $conditions = null, |
||
| 573 | |||
| 574 | /** |
||
| 575 | * Vrací kód záznamu. |
||
| 576 | * |
||
| 577 | * @param array $data |
||
| 578 | * |
||
| 579 | * @todo papat i string |
||
| 580 | * |
||
| 581 | * @return string |
||
| 582 | */ |
||
| 583 | public function getKod($data = null, $unique = true) |
||
| 631 | |||
| 632 | /** |
||
| 633 | * Vyhledavani v záznamech objektu FlexiBee. |
||
| 634 | * |
||
| 635 | * @param string $what hledaný výraz |
||
| 636 | * |
||
| 637 | * @return array pole výsledků |
||
| 638 | */ |
||
| 639 | public function searchString($what) |
||
| 692 | |||
| 693 | /** |
||
| 694 | * Write Operation Result. |
||
| 695 | * |
||
| 696 | * @param array $resultData |
||
| 697 | * @param string $url URL |
||
| 698 | */ |
||
| 699 | public function logResult($resultData, $url = null) |
||
| 730 | |||
| 731 | /** |
||
| 732 | * Generuje fragment url pro filtrování. |
||
| 733 | * |
||
| 734 | * @see https://www.flexibee.eu/api/dokumentace/ref/filters |
||
| 735 | * |
||
| 736 | * @param array $data |
||
| 737 | * @param string $operator and/or |
||
| 738 | * |
||
| 739 | * @return string |
||
| 740 | */ |
||
| 741 | 13 | public static function flexiUrl($data, $operator = 'and') |
|
| 758 | } |
||
| 759 |
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..