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 | * @var string Jmený prostor datového bloku odpovědi |
||
| 16 | */ |
||
| 17 | public $nameSpace = 'winstrom'; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Datový blok v poli odpovědi |
||
| 21 | * |
||
| 22 | * @var string |
||
| 23 | */ |
||
| 24 | public $resultField = 'results'; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Verze protokolu použitého pro komunikaci. |
||
| 28 | * @var string Verze použitého API |
||
| 29 | */ |
||
| 30 | public $protoVersion = '1.0'; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Agenda užitá objektem. |
||
| 34 | * |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | public $agenda = null; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Výchozí formát pro komunikaci. |
||
| 41 | * |
||
| 42 | * @link https://www.flexibee.eu/api/dokumentace/ref/format-types Přehled možných formátů |
||
| 43 | * |
||
| 44 | * @var string json|xml|... |
||
| 45 | */ |
||
| 46 | public $format = 'json'; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Curl Handle. |
||
| 50 | * |
||
| 51 | * @var resource |
||
| 52 | */ |
||
| 53 | public $curl = null; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var type |
||
| 57 | */ |
||
| 58 | public $company = FLEXIBEE_COMPANY; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var string |
||
| 62 | */ |
||
| 63 | public $url = FLEXIBEE_URL; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var string |
||
| 67 | */ |
||
| 68 | public $user = FLEXIBEE_LOGIN; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var string |
||
| 72 | */ |
||
| 73 | public $password = FLEXIBEE_PASSWORD; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Identifikační řetězec. |
||
| 77 | * |
||
| 78 | * @var string |
||
| 79 | */ |
||
| 80 | public $init = null; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Sloupeček s názvem. |
||
| 84 | * |
||
| 85 | * @var string |
||
| 86 | */ |
||
| 87 | public $nameColumn = 'nazev'; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Sloupeček obsahující datum vložení záznamu do shopu. |
||
| 91 | * |
||
| 92 | * @var string |
||
| 93 | */ |
||
| 94 | public $myCreateColumn = 'false'; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Slopecek obsahujici datum poslení modifikace záznamu do shopu. |
||
| 98 | * |
||
| 99 | * @var string |
||
| 100 | */ |
||
| 101 | public $myLastModifiedColumn = 'lastUpdate'; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Klíčový idendifikátor záznamu. |
||
| 105 | * |
||
| 106 | * @var string |
||
| 107 | */ |
||
| 108 | public $fbKeyColumn = 'id'; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Informace o posledním HTTP requestu. |
||
| 112 | * |
||
| 113 | * @var array |
||
| 114 | */ |
||
| 115 | public $info; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Informace o poslední HTTP chybě. |
||
| 119 | * |
||
| 120 | * @var array |
||
| 121 | */ |
||
| 122 | public $error; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Used codes storage |
||
| 126 | * @var array |
||
| 127 | */ |
||
| 128 | public $codes = null; |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Last Inserted ID |
||
| 132 | * @var int |
||
| 133 | */ |
||
| 134 | public $lastInsertedID = null; |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Default Line Prefix |
||
| 138 | * @var string |
||
| 139 | */ |
||
| 140 | public $prefix = '/c/'; |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Třída pro práci s FlexiBee. |
||
| 144 | * |
||
| 145 | * @param string $init výchozí selektor dat |
||
| 146 | */ |
||
| 147 | public function __construct($init = null) |
||
| 154 | |||
| 155 | public function curlInit() |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Nastaví Agendu pro Komunikaci. |
||
| 170 | * |
||
| 171 | * @param string $agenda |
||
| 172 | */ |
||
| 173 | public function setAgenda($agenda) |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Převede rekurzivně Objekt na pole. |
||
| 180 | * |
||
| 181 | * @param object $data |
||
| 182 | * |
||
| 183 | * @return array |
||
| 184 | */ |
||
| 185 | public static function object2array($data) |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Funkce, která provede I/O operaci a vyhodnotí výsledek. |
||
| 199 | * |
||
| 200 | * @param string $urlSuffix část URL za identifikátorem firmy. |
||
| 201 | * @param string $method HTTP/REST metoda |
||
| 202 | * @param string $format Requested format |
||
| 203 | */ |
||
| 204 | public function performRequest($urlSuffix = null, $method = 'GET', |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Give you last inserted record ID |
||
| 280 | * |
||
| 281 | * @return int |
||
| 282 | */ |
||
| 283 | function getLastImportId() |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Convert XML to array. |
||
| 290 | * |
||
| 291 | * @param string $xml |
||
| 292 | * |
||
| 293 | * @return array |
||
| 294 | */ |
||
| 295 | public static function xml2array($xml) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Odpojení od FlexiBee. |
||
| 312 | */ |
||
| 313 | public function disconnect() |
||
| 319 | |||
| 320 | public function __destruct() |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Načte data z FlexiBee. |
||
| 327 | * |
||
| 328 | * @param string $suffix dotaz |
||
| 329 | */ |
||
| 330 | public function loadFlexiData($suffix = null) |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Načte řádek dat z FlexiBee |
||
| 337 | * |
||
| 338 | * @param int $recordID |
||
| 339 | * @return array |
||
| 340 | */ |
||
| 341 | public function getFlexiRow($recordID) |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Načte data z FlexiBee. |
||
| 353 | * |
||
| 354 | * @param string $suffix dotaz |
||
| 355 | * @param string $conditions Volitelný filtrovací výraz |
||
| 356 | */ |
||
| 357 | public function getFlexiData($suffix = null, $conditions = null) |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Načte záznam z FlexiBee. |
||
| 383 | * |
||
| 384 | * @param int $id ID záznamu |
||
| 385 | * |
||
| 386 | * @return int počet načtených položek |
||
| 387 | */ |
||
| 388 | public function loadFromFlexiBee($id = null) |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Uloží data do FlexiBee. |
||
| 399 | * |
||
| 400 | * @param array $data |
||
| 401 | * |
||
| 402 | * @return array výsledek |
||
| 403 | */ |
||
| 404 | View Code Duplication | public function saveToFlexiBee($data = null) |
|
| 416 | |||
| 417 | /** |
||
| 418 | * Převede data do Json formátu pro FlexiBee. |
||
| 419 | * |
||
| 420 | * @param array $data |
||
| 421 | * |
||
| 422 | * @return string |
||
| 423 | */ |
||
| 424 | public function jsonizeData($data) |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Uloží záznam. |
||
| 438 | * |
||
| 439 | * @param array $data |
||
| 440 | * |
||
| 441 | * @return array odpověď |
||
| 442 | */ |
||
| 443 | View Code Duplication | public function insertToFlexiBee($data = null) |
|
| 453 | |||
| 454 | /** |
||
| 455 | * Test if given record ID exists in FlexiBee |
||
| 456 | * |
||
| 457 | * @param string|int $identifer |
||
| 458 | */ |
||
| 459 | function idExists($identifer = null) |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Test if given record exists in FlexiBee |
||
| 471 | * |
||
| 472 | * @param array $data |
||
| 473 | */ |
||
| 474 | function recordExists($data = null) |
||
| 484 | |||
| 485 | /** |
||
| 486 | * Vrací z FlexiBee sloupečky podle podmínek. |
||
| 487 | * |
||
| 488 | * @param array|int|string $conditions pole podmínek nebo ID záznamu |
||
| 489 | * @param array|string $orderBy třídit dle |
||
| 490 | * @param string $indexBy klice vysledku naplnit hodnotou ze |
||
| 491 | * sloupečku |
||
| 492 | * @param int $limit maximální počet vrácených záznamů |
||
| 493 | * |
||
| 494 | * @return array |
||
| 495 | */ |
||
| 496 | public function getAllFromFlexibee($conditions = null, $orderBy = null, |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Vrací z FlexiBee sloupečky podle podmínek. |
||
| 519 | * |
||
| 520 | * @param array $columnsList seznam položek |
||
| 521 | * @param array|int|string $conditions pole podmínek nebo ID záznamu |
||
| 522 | * @param array|string $orderBy třídit dle |
||
| 523 | * @param string $indexBy klice vysledku naplnit hodnotou ze |
||
| 524 | * sloupečku |
||
| 525 | * @param int $limit maximální počet vrácených záznamů |
||
| 526 | * |
||
| 527 | * @return array |
||
| 528 | */ |
||
| 529 | public function getColumnsFromFlexibee($columnsList, $conditions = null, |
||
| 561 | |||
| 562 | /** |
||
| 563 | * Vrací kód záznamu. |
||
| 564 | * |
||
| 565 | * @param array $data |
||
| 566 | * @todo papat i string |
||
| 567 | * |
||
| 568 | * @return string |
||
| 569 | */ |
||
| 570 | public function getKod($data = null, $unique = true) |
||
| 617 | |||
| 618 | /** |
||
| 619 | * Vyhledavani v záznamech objektu FlexiBee. |
||
| 620 | * |
||
| 621 | * @param string $what hledaný výraz |
||
| 622 | * |
||
| 623 | * @return array pole výsledků |
||
| 624 | */ |
||
| 625 | public function searchString($what) |
||
| 678 | |||
| 679 | /** |
||
| 680 | * Write Operation Result. |
||
| 681 | * |
||
| 682 | * @param array $resultData |
||
| 683 | * @param string $url URL |
||
| 684 | */ |
||
| 685 | public function logResult($resultData, $url = null) |
||
| 716 | |||
| 717 | /** |
||
| 718 | * Generuje fragment url pro filtrování |
||
| 719 | * |
||
| 720 | * @see https://www.flexibee.eu/api/dokumentace/ref/filters |
||
| 721 | * @param array $data |
||
| 722 | * @param string $operator and/or |
||
| 723 | * @return string |
||
| 724 | */ |
||
| 725 | static function flexiUrl($data, $operator = 'and') |
||
| 741 | } |
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..