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 | 28 | public $lastResponseCode = null; |
|
153 | |||
154 | 28 | /** |
|
155 | * Třída pro práci s FlexiBee. |
||
156 | 28 | * |
|
157 | 28 | * @param string $init výchozí selektor dat |
|
158 | 28 | */ |
|
159 | public function __construct($init = null) |
||
166 | 28 | ||
167 | 28 | public function curlInit() |
|
179 | |||
180 | 14 | /** |
|
181 | 14 | * Nastaví Agendu pro Komunikaci. |
|
182 | * |
||
183 | * @param string $evidence |
||
184 | */ |
||
185 | public function setEvidence($evidence) |
||
189 | |||
190 | 14 | /** |
|
191 | * Převede rekurzivně Objekt na pole. |
||
192 | 14 | * |
|
193 | 14 | * @param object|array $object |
|
194 | 14 | * |
|
195 | 14 | * @return array |
|
196 | 14 | */ |
|
197 | 14 | public static function object2array($object) |
|
217 | |||
218 | 14 | /** |
|
219 | * Funkce, která provede I/O operaci a vyhodnotí výsledek. |
||
220 | * |
||
221 | 14 | * @param string $urlSuffix část URL za identifikátorem firmy. |
|
222 | 14 | * @param string $method HTTP/REST metoda |
|
223 | 14 | * @param string $format Requested format |
|
224 | 14 | */ |
|
225 | 3 | public function performRequest($urlSuffix = null, $method = 'GET', |
|
311 | |||
312 | 14 | /** |
|
313 | 14 | * Give you last inserted record ID. |
|
314 | 14 | * |
|
315 | * @return int |
||
316 | 14 | */ |
|
317 | 14 | public function getLastInsertedId() |
|
321 | 14 | ||
322 | /** |
||
323 | 14 | * Convert XML to array. |
|
324 | * |
||
325 | 14 | * @param string $xml |
|
326 | * |
||
327 | * @return array |
||
328 | */ |
||
329 | public static function xml2array($xml) |
||
348 | |||
349 | /** |
||
350 | * Odpojení od FlexiBee. |
||
351 | */ |
||
352 | public function disconnect() |
||
359 | |||
360 | public function __destruct() |
||
364 | 14 | ||
365 | 14 | /** |
|
366 | 9 | * Načte data z FlexiBee. |
|
367 | 9 | * |
|
368 | * @param string $suffix dotaz |
||
369 | 14 | */ |
|
370 | public function loadFlexiData($suffix = null) |
||
374 | |||
375 | /** |
||
376 | * Načte řádek dat z FlexiBee. |
||
377 | * |
||
378 | 13 | * @param int $recordID id požadovaného záznamu |
|
379 | * |
||
380 | 13 | * @return array |
|
381 | 10 | */ |
|
382 | 10 | public function getFlexiRow($recordID) |
|
392 | 13 | ||
393 | /** |
||
394 | 13 | * Načte data z FlexiBee. |
|
395 | 10 | * |
|
396 | 10 | * @param string $suffix dotaz |
|
397 | 3 | * @param string $conditions Volitelný filtrovací výraz |
|
398 | */ |
||
399 | public function getFlexiData($suffix = null, $conditions = null) |
||
423 | |||
424 | /** |
||
425 | * Načte záznam z FlexiBee. |
||
426 | * |
||
427 | * @param int $id ID záznamu |
||
428 | * |
||
429 | * @return int počet načtených položek |
||
430 | */ |
||
431 | public function loadFromFlexiBee($id = null) |
||
439 | |||
440 | /** |
||
441 | * Uloží data do FlexiBee. |
||
442 | * |
||
443 | * @param array $data |
||
444 | * |
||
445 | * @return array výsledek |
||
446 | 14 | */ |
|
447 | View Code Duplication | public function saveToFlexiBee($data = null) |
|
459 | |||
460 | /** |
||
461 | * Převede data do Json formátu pro FlexiBee. |
||
462 | * |
||
463 | * @param array $data |
||
464 | * |
||
465 | * @return string |
||
466 | */ |
||
467 | public function jsonizeData($data) |
||
478 | |||
479 | /** |
||
480 | * Uloží záznam. |
||
481 | * |
||
482 | * @param array $data |
||
483 | * |
||
484 | * @return array odpověď |
||
485 | */ |
||
486 | View Code Duplication | public function insertToFlexiBee($data = null) |
|
496 | |||
497 | /** |
||
498 | * Test if given record ID exists in FlexiBee. |
||
499 | * |
||
500 | * @param string|int $identifer |
||
501 | */ |
||
502 | public function idExists($identifer = null) |
||
512 | |||
513 | /** |
||
514 | * Test if given record exists in FlexiBee. |
||
515 | * |
||
516 | * @param array $data |
||
517 | */ |
||
518 | public function recordExists($data = null) |
||
529 | |||
530 | /** |
||
531 | * Vrací z FlexiBee sloupečky podle podmínek. |
||
532 | * |
||
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 getAllFromFlexibee($conditions = null, $orderBy = null, |
||
560 | |||
561 | /** |
||
562 | * Vrací z FlexiBee sloupečky podle podmínek. |
||
563 | * |
||
564 | * @param string[] $columnsList seznam položek |
||
565 | * @param array|int|string $conditions pole podmínek nebo ID záznamu |
||
566 | * @param array|string $orderBy třídit dle |
||
567 | * @param string $indexBy klice vysledku naplnit hodnotou ze |
||
568 | * sloupečku |
||
569 | * @param int $limit maximální počet vrácených záznamů |
||
570 | * |
||
571 | * @return array |
||
572 | */ |
||
573 | public function getColumnsFromFlexibee($columnsList, $conditions = null, |
||
605 | |||
606 | 14 | /** |
|
607 | 14 | * Vrací kód záznamu. |
|
608 | 14 | * |
|
609 | 14 | * @param mixed $data |
|
610 | 14 | * |
|
611 | 14 | * @todo papat i string |
|
612 | 14 | * |
|
613 | 14 | * @return string |
|
614 | 14 | */ |
|
615 | 14 | public function getKod($data = null, $unique = true) |
|
668 | |||
669 | /** |
||
670 | * Vyhledavani v záznamech objektu FlexiBee. |
||
671 | * |
||
672 | * @param string $what hledaný výraz |
||
673 | * |
||
674 | * @return array pole výsledků |
||
675 | */ |
||
676 | public function searchString($what) |
||
729 | 14 | ||
730 | 14 | /** |
|
731 | 14 | * Write Operation Result. |
|
732 | 14 | * |
|
733 | * @param array $resultData |
||
734 | 14 | * @param string $url URL |
|
735 | 14 | */ |
|
736 | 14 | public function logResult($resultData, $url = null) |
|
777 | |||
778 | /** |
||
779 | * Generuje fragment url pro filtrování. |
||
780 | * |
||
781 | * @see https://www.flexibee.eu/api/dokumentace/ref/filters |
||
782 | * |
||
783 | * @param array $data |
||
784 | * @param string $operator and/or |
||
785 | * |
||
786 | * @return string |
||
787 | */ |
||
788 | public static function flexiUrl(array $data, $operator = 'and') |
||
805 | |||
806 | /** |
||
807 | * Smaže záznam |
||
808 | * |
||
809 | * @param int|string $id identifikátor záznamu |
||
810 | */ |
||
811 | public function deleteFromFlexiBee($id) |
||
815 | } |
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..