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) |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Nastaví Agendu pro Komunikaci. |
||
| 165 | * |
||
| 166 | * @param string $agenda |
||
| 167 | */ |
||
| 168 | public function setAgenda($agenda) |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Převede rekurzivně Objekt na pole. |
||
| 175 | * |
||
| 176 | * @param object $data |
||
| 177 | * |
||
| 178 | * @return array |
||
| 179 | */ |
||
| 180 | public static function object2array($data) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Funkce, která provede I/O operaci a vyhodnotí výsledek. |
||
| 194 | * |
||
| 195 | * @param string $urlSuffix část URL za identifikátorem firmy. |
||
| 196 | * @param string $method HTTP/REST metoda |
||
| 197 | * @param string $format Requested format |
||
| 198 | */ |
||
| 199 | public function performRequest($urlSuffix = null, $method = 'GET', |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Give you last inserted record ID |
||
| 275 | * |
||
| 276 | * @return int |
||
| 277 | */ |
||
| 278 | function getLastImportId() |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Convert XML to array. |
||
| 285 | * |
||
| 286 | * @param string $xml |
||
| 287 | * |
||
| 288 | * @return array |
||
| 289 | */ |
||
| 290 | public static function xml2array($xml) |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Odpojení od FlexiBee. |
||
| 307 | */ |
||
| 308 | public function disconnect() |
||
| 314 | |||
| 315 | public function __destruct() |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Načte data z FlexiBee. |
||
| 322 | * |
||
| 323 | * @param string $suffix dotaz |
||
| 324 | */ |
||
| 325 | public function loadFlexiData($suffix = null) |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Načte řádek dat z FlexiBee |
||
| 332 | * |
||
| 333 | * @param int $recordID |
||
| 334 | * @return array |
||
| 335 | */ |
||
| 336 | public function getFlexiRow($recordID) |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Načte data z FlexiBee. |
||
| 348 | * |
||
| 349 | * @param string $suffix dotaz |
||
| 350 | * @param string $conditions Volitelný filtrovací výraz |
||
| 351 | */ |
||
| 352 | public function getFlexiData($suffix = null, $conditions = null) |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Načte záznam z FlexiBee. |
||
| 378 | * |
||
| 379 | * @param int $id ID záznamu |
||
| 380 | * |
||
| 381 | * @return int počet načtených položek |
||
| 382 | */ |
||
| 383 | public function loadFromFlexiBee($id = null) |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Uloží data do FlexiBee. |
||
| 394 | * |
||
| 395 | * @param array $data |
||
| 396 | * |
||
| 397 | * @return array výsledek |
||
| 398 | */ |
||
| 399 | View Code Duplication | public function saveToFlexiBee($data = null) |
|
| 411 | |||
| 412 | /** |
||
| 413 | * Převede data do Json formátu pro FlexiBee. |
||
| 414 | * |
||
| 415 | * @param array $data |
||
| 416 | * |
||
| 417 | * @return string |
||
| 418 | */ |
||
| 419 | public function jsonizeData($data) |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Uloží záznam. |
||
| 433 | * |
||
| 434 | * @param array $data |
||
| 435 | * |
||
| 436 | * @return array odpověď |
||
| 437 | */ |
||
| 438 | View Code Duplication | public function insertToFlexiBee($data = null) |
|
| 448 | |||
| 449 | /** |
||
| 450 | * Test if given record ID exists in FlexiBee |
||
| 451 | * |
||
| 452 | * @param string|int $identifer |
||
| 453 | */ |
||
| 454 | function idExists($identifer = null) |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Test if given record exists in FlexiBee |
||
| 466 | * |
||
| 467 | * @param array $data |
||
| 468 | */ |
||
| 469 | function recordExists($data = null) |
||
| 479 | |||
| 480 | /** |
||
| 481 | * Vrací z FlexiBee sloupečky podle podmínek. |
||
| 482 | * |
||
| 483 | * @param array|int|string $conditions pole podmínek nebo ID záznamu |
||
| 484 | * @param array|string $orderBy třídit dle |
||
| 485 | * @param string $indexBy klice vysledku naplnit hodnotou ze |
||
| 486 | * sloupečku |
||
| 487 | * @param int $limit maximální počet vrácených záznamů |
||
| 488 | * |
||
| 489 | * @return array |
||
| 490 | */ |
||
| 491 | public function getAllFromFlexibee($conditions = null, $orderBy = null, |
||
| 511 | |||
| 512 | /** |
||
| 513 | * Vrací z FlexiBee sloupečky podle podmínek. |
||
| 514 | * |
||
| 515 | * @param array $columnsList seznam položek |
||
| 516 | * @param array|int|string $conditions pole podmínek nebo ID záznamu |
||
| 517 | * @param array|string $orderBy třídit dle |
||
| 518 | * @param string $indexBy klice vysledku naplnit hodnotou ze |
||
| 519 | * sloupečku |
||
| 520 | * @param int $limit maximální počet vrácených záznamů |
||
| 521 | * |
||
| 522 | * @return array |
||
| 523 | */ |
||
| 524 | public function getColumnsFromFlexibee($columnsList, $conditions = null, |
||
| 556 | |||
| 557 | /** |
||
| 558 | * Vrací kód záznamu. |
||
| 559 | * |
||
| 560 | * @param array $data |
||
| 561 | * @todo papat i string |
||
| 562 | * |
||
| 563 | * @return string |
||
| 564 | */ |
||
| 565 | public function getKod($data = null, $unique = true) |
||
| 612 | |||
| 613 | /** |
||
| 614 | * Vyhledavani v záznamech objektu FlexiBee. |
||
| 615 | * |
||
| 616 | * @param string $what hledaný výraz |
||
| 617 | * |
||
| 618 | * @return array pole výsledků |
||
| 619 | */ |
||
| 620 | public function searchString($what) |
||
| 673 | |||
| 674 | /** |
||
| 675 | * Write Operation Result. |
||
| 676 | * |
||
| 677 | * @param array $resultData |
||
| 678 | * @param string $url URL |
||
| 679 | */ |
||
| 680 | public function logResult($resultData, $url = null) |
||
| 711 | |||
| 712 | /** |
||
| 713 | * Generuje fragment url pro filtrování |
||
| 714 | * |
||
| 715 | * @see https://www.flexibee.eu/api/dokumentace/ref/filters |
||
| 716 | * @param array $data |
||
| 717 | * @param string $operator and/or |
||
| 718 | * @return string |
||
| 719 | */ |
||
| 720 | static function flexiUrl($data, $operator = 'and') |
||
| 736 | } |
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..