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', |
|
335 | |||
336 | /** |
||
337 | * Give you last inserted record ID. |
||
338 | * |
||
339 | * @return int |
||
340 | */ |
||
341 | public function getLastInsertedId() |
||
345 | |||
346 | /** |
||
347 | * Convert XML to array. |
||
348 | * |
||
349 | * @param string $xml |
||
350 | 16 | * |
|
351 | * @return array |
||
352 | 16 | */ |
|
353 | public static function xml2array($xml) |
||
372 | |||
373 | 17 | /** |
|
374 | * Odpojení od FlexiBee. |
||
375 | 17 | */ |
|
376 | 17 | public function disconnect() |
|
383 | 17 | ||
384 | 17 | public function __destruct() |
|
388 | |||
389 | /** |
||
390 | * Načte data z FlexiBee. |
||
391 | * |
||
392 | * @param string $suffix dotaz |
||
393 | */ |
||
394 | public function loadFlexiData($suffix = null) |
||
398 | |||
399 | /** |
||
400 | * Načte řádek dat z FlexiBee. |
||
401 | * |
||
402 | * @param int $recordID id požadovaného záznamu |
||
403 | 16 | * |
|
404 | * @return array |
||
405 | 16 | */ |
|
406 | 16 | public function getFlexiRow($recordID) |
|
416 | |||
417 | /** |
||
418 | * Načte data z FlexiBee. |
||
419 | * |
||
420 | 15 | * @param string $suffix dotaz |
|
421 | * @param string $conditions Volitelný filtrovací výraz |
||
422 | 15 | */ |
|
423 | 10 | public function getFlexiData($suffix = null, $conditions = null) |
|
447 | |||
448 | /** |
||
449 | * Načte záznam z FlexiBee. |
||
450 | * |
||
451 | * @param int $id ID záznamu |
||
452 | 16 | * |
|
453 | * @return int počet načtených položek |
||
454 | 16 | */ |
|
455 | 16 | public function loadFromFlexiBee($id = null) |
|
463 | |||
464 | /** |
||
465 | * Převede data do Json formátu pro FlexiBee. |
||
466 | * |
||
467 | * @param array $data |
||
468 | * |
||
469 | * @return string |
||
470 | */ |
||
471 | public function jsonizeData($data) |
||
482 | |||
483 | /** |
||
484 | * Uloží záznam. |
||
485 | * |
||
486 | * @param array $data |
||
487 | * |
||
488 | 16 | * @return array odpověď |
|
489 | */ |
||
490 | public function insertToFlexiBee($data = null) |
||
498 | |||
499 | /** |
||
500 | * Test if given record ID exists in FlexiBee. |
||
501 | * |
||
502 | * @param string|int $identifer |
||
503 | */ |
||
504 | public function idExists($identifer = null) |
||
514 | |||
515 | /** |
||
516 | * Test if given record exists in FlexiBee. |
||
517 | * |
||
518 | * @param array $data |
||
519 | */ |
||
520 | public function recordExists($data = null) |
||
531 | |||
532 | /** |
||
533 | * Vrací z FlexiBee sloupečky podle podmínek. |
||
534 | * |
||
535 | * @param array|int|string $conditions pole podmínek nebo ID záznamu |
||
536 | * @param array|string $orderBy třídit dle |
||
537 | * @param string $indexBy klice vysledku naplnit hodnotou ze |
||
538 | * sloupečku |
||
539 | * @param int $limit maximální počet vrácených záznamů |
||
540 | * |
||
541 | * @return array |
||
542 | */ |
||
543 | public function getAllFromFlexibee($conditions = null, $orderBy = null, |
||
562 | |||
563 | /** |
||
564 | * Vrací z FlexiBee sloupečky podle podmínek. |
||
565 | * |
||
566 | * @param string[] $columnsList seznam položek |
||
567 | * @param array|int|string $conditions pole podmínek nebo ID záznamu |
||
568 | * @param array|string $orderBy třídit dle |
||
569 | * @param string $indexBy klice vysledku naplnit hodnotou ze |
||
570 | * sloupečku |
||
571 | * @param int $limit maximální počet vrácených záznamů |
||
572 | * |
||
573 | * @return array |
||
574 | */ |
||
575 | public function getColumnsFromFlexibee($columnsList, $conditions = null, |
||
607 | |||
608 | /** |
||
609 | * Vrací kód záznamu. |
||
610 | * |
||
611 | * @param mixed $data |
||
612 | * |
||
613 | * @todo papat i string |
||
614 | * |
||
615 | * @return string |
||
616 | */ |
||
617 | public function getKod($data = null, $unique = true) |
||
670 | 16 | ||
671 | 16 | /** |
|
672 | 16 | * Vyhledavani v záznamech objektu FlexiBee. |
|
673 | 16 | * |
|
674 | 16 | * @param string $what hledaný výraz |
|
675 | 16 | * |
|
676 | 16 | * @return array pole výsledků |
|
677 | 16 | */ |
|
678 | 16 | public function searchString($what) |
|
731 | |||
732 | /** |
||
733 | * Write Operation Result. |
||
734 | * |
||
735 | * @param array $resultData |
||
736 | * @param string $url URL |
||
737 | */ |
||
738 | public function logResult($resultData, $url = null) |
||
779 | |||
780 | 16 | /** |
|
781 | * Generuje fragment url pro filtrování. |
||
782 | * |
||
783 | 16 | * @see https://www.flexibee.eu/api/dokumentace/ref/filters |
|
784 | * |
||
785 | * @param array $data |
||
786 | 16 | * @param string $operator and/or |
|
787 | 16 | * |
|
788 | 16 | * @return string |
|
789 | 16 | */ |
|
790 | 16 | public static function flexiUrl(array $data, $operator = 'and') |
|
807 | 16 | ||
808 | /** |
||
809 | 16 | * Smaže záznam |
|
810 | 16 | * |
|
811 | * @param int|string $id identifikátor záznamu |
||
812 | 16 | * @return boolean Response code is 200 ? |
|
813 | 16 | */ |
|
814 | 16 | public function deleteFromFlexiBee($id) |
|
820 | } |
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..