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 | * Evidence užitá objektem. |
||
| 36 | * |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | public $evidence = 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 | * HTTP Response code of last request |
||
| 149 | * |
||
| 150 | * @var int |
||
| 151 | */ |
||
| 152 | public $lastResponseCode = null; |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Array of fields for next curl POST operation |
||
| 156 | * |
||
| 157 | * @var array |
||
| 158 | */ |
||
| 159 | protected $postFields = []; |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Třída pro práci s FlexiBee. |
||
| 163 | * |
||
| 164 | * @param string $init výchozí selektor dat |
||
| 165 | */ |
||
| 166 | 32 | public function __construct($init = null) |
|
| 173 | |||
| 174 | 32 | public function curlInit() |
|
| 186 | |||
| 187 | /** |
||
| 188 | * Nastaví Agendu pro Komunikaci. |
||
| 189 | * |
||
| 190 | * @param string $evidence |
||
| 191 | */ |
||
| 192 | 16 | public function setEvidence($evidence) |
|
| 196 | |||
| 197 | /** |
||
| 198 | * Převede rekurzivně Objekt na pole. |
||
| 199 | * |
||
| 200 | * @param object|array $object |
||
| 201 | * |
||
| 202 | * @return array |
||
| 203 | */ |
||
| 204 | 16 | public static function object2array($object) |
|
| 224 | |||
| 225 | /** |
||
| 226 | * Funkce, která provede I/O operaci a vyhodnotí výsledek. |
||
| 227 | * |
||
| 228 | * @param string $urlSuffix část URL za identifikátorem firmy. |
||
| 229 | * @param string $method HTTP/REST metoda |
||
| 230 | * @param string $format Requested format |
||
| 231 | */ |
||
| 232 | 16 | public function performRequest($urlSuffix = null, $method = 'GET', |
|
| 332 | |||
| 333 | /** |
||
| 334 | * Give you last inserted record ID. |
||
| 335 | * |
||
| 336 | * @return int |
||
| 337 | */ |
||
| 338 | public function getLastInsertedId() |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Convert XML to array. |
||
| 345 | * |
||
| 346 | * @param string $xml |
||
| 347 | * |
||
| 348 | * @return array |
||
| 349 | */ |
||
| 350 | 16 | public static function xml2array($xml) |
|
| 369 | |||
| 370 | /** |
||
| 371 | * Odpojení od FlexiBee. |
||
| 372 | */ |
||
| 373 | 17 | public function disconnect() |
|
| 380 | |||
| 381 | 17 | public function __destruct() |
|
| 385 | |||
| 386 | /** |
||
| 387 | * Načte data z FlexiBee. |
||
| 388 | * |
||
| 389 | * @param string $suffix dotaz |
||
| 390 | */ |
||
| 391 | public function loadFlexiData($suffix = null) |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Načte řádek dat z FlexiBee. |
||
| 398 | * |
||
| 399 | * @param int $recordID id požadovaného záznamu |
||
| 400 | * |
||
| 401 | * @return array |
||
| 402 | */ |
||
| 403 | 16 | public function getFlexiRow($recordID) |
|
| 413 | |||
| 414 | /** |
||
| 415 | * Načte data z FlexiBee. |
||
| 416 | * |
||
| 417 | * @param string $suffix dotaz |
||
| 418 | * @param string $conditions Volitelný filtrovací výraz |
||
| 419 | */ |
||
| 420 | 15 | public function getFlexiData($suffix = null, $conditions = null) |
|
| 444 | |||
| 445 | /** |
||
| 446 | * Načte záznam z FlexiBee. |
||
| 447 | * |
||
| 448 | * @param int $id ID záznamu |
||
| 449 | * |
||
| 450 | * @return int počet načtených položek |
||
| 451 | */ |
||
| 452 | 16 | public function loadFromFlexiBee($id = null) |
|
| 460 | |||
| 461 | /** |
||
| 462 | * Uloží data do FlexiBee. |
||
| 463 | * |
||
| 464 | * @param array $data |
||
| 465 | * |
||
| 466 | * @return array výsledek |
||
| 467 | */ |
||
| 468 | public function saveToFlexiBee($data = null) |
||
| 480 | |||
| 481 | /** |
||
| 482 | * Převede data do Json formátu pro FlexiBee. |
||
| 483 | * |
||
| 484 | * @param array $data |
||
| 485 | * |
||
| 486 | * @return string |
||
| 487 | */ |
||
| 488 | 16 | public function jsonizeData($data) |
|
| 499 | |||
| 500 | /** |
||
| 501 | * Uloží záznam. |
||
| 502 | * |
||
| 503 | * @param array $data |
||
| 504 | * |
||
| 505 | * @return array odpověď |
||
| 506 | */ |
||
| 507 | public function insertToFlexiBee($data = null) |
||
| 515 | |||
| 516 | /** |
||
| 517 | * Test if given record ID exists in FlexiBee. |
||
| 518 | * |
||
| 519 | * @param string|int $identifer |
||
| 520 | */ |
||
| 521 | public function idExists($identifer = null) |
||
| 531 | |||
| 532 | /** |
||
| 533 | * Test if given record exists in FlexiBee. |
||
| 534 | * |
||
| 535 | * @param array $data |
||
| 536 | */ |
||
| 537 | public function recordExists($data = null) |
||
| 548 | |||
| 549 | /** |
||
| 550 | * Vrací z FlexiBee sloupečky podle podmínek. |
||
| 551 | * |
||
| 552 | * @param array|int|string $conditions pole podmínek nebo ID záznamu |
||
| 553 | * @param array|string $orderBy třídit dle |
||
| 554 | * @param string $indexBy klice vysledku naplnit hodnotou ze |
||
| 555 | * sloupečku |
||
| 556 | * @param int $limit maximální počet vrácených záznamů |
||
| 557 | * |
||
| 558 | * @return array |
||
| 559 | */ |
||
| 560 | public function getAllFromFlexibee($conditions = null, $orderBy = null, |
||
| 579 | |||
| 580 | /** |
||
| 581 | * Vrací z FlexiBee sloupečky podle podmínek. |
||
| 582 | * |
||
| 583 | * @param string[] $columnsList seznam položek |
||
| 584 | * @param array|int|string $conditions pole podmínek nebo ID záznamu |
||
| 585 | * @param array|string $orderBy třídit dle |
||
| 586 | * @param string $indexBy klice vysledku naplnit hodnotou ze |
||
| 587 | * sloupečku |
||
| 588 | * @param int $limit maximální počet vrácených záznamů |
||
| 589 | * |
||
| 590 | * @return array |
||
| 591 | */ |
||
| 592 | public function getColumnsFromFlexibee($columnsList, $conditions = null, |
||
| 624 | |||
| 625 | /** |
||
| 626 | * Vrací kód záznamu. |
||
| 627 | * |
||
| 628 | * @param mixed $data |
||
| 629 | * |
||
| 630 | * @todo papat i string |
||
| 631 | * |
||
| 632 | * @return string |
||
| 633 | */ |
||
| 634 | 16 | public function getKod($data = null, $unique = true) |
|
| 687 | |||
| 688 | /** |
||
| 689 | * Vyhledavani v záznamech objektu FlexiBee. |
||
| 690 | * |
||
| 691 | * @param string $what hledaný výraz |
||
| 692 | * |
||
| 693 | * @return array pole výsledků |
||
| 694 | */ |
||
| 695 | public function searchString($what) |
||
| 748 | |||
| 749 | /** |
||
| 750 | * Write Operation Result. |
||
| 751 | * |
||
| 752 | * @param array $resultData |
||
| 753 | * @param string $url URL |
||
| 754 | */ |
||
| 755 | 16 | public function logResult($resultData, $url = null) |
|
| 796 | |||
| 797 | /** |
||
| 798 | * Generuje fragment url pro filtrování. |
||
| 799 | * |
||
| 800 | * @see https://www.flexibee.eu/api/dokumentace/ref/filters |
||
| 801 | * |
||
| 802 | * @param array $data |
||
| 803 | * @param string $operator and/or |
||
| 804 | * |
||
| 805 | * @return string |
||
| 806 | */ |
||
| 807 | 16 | public static function flexiUrl(array $data, $operator = 'and') |
|
| 824 | |||
| 825 | /** |
||
| 826 | * Smaže záznam |
||
| 827 | * |
||
| 828 | * @param int|string $id identifikátor záznamu |
||
| 829 | * @return boolean Response code is 200 ? |
||
| 830 | */ |
||
| 831 | public function deleteFromFlexiBee($id) |
||
| 837 | } |
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..