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 | * Agenda užitá objektem. |
||
36 | * |
||
37 | * @var string |
||
38 | */ |
||
39 | public $agenda = 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 | * Třída pro práci s FlexiBee. |
||
149 | * |
||
150 | * @param string $init výchozí selektor dat |
||
151 | */ |
||
152 | 13 | public function __construct($init = null) |
|
159 | |||
160 | 13 | public function curlInit() |
|
172 | |||
173 | /** |
||
174 | * Nastaví Agendu pro Komunikaci. |
||
175 | * |
||
176 | * @param string $agenda |
||
177 | */ |
||
178 | 13 | public function setAgenda($agenda) |
|
182 | |||
183 | /** |
||
184 | * Převede rekurzivně Objekt na pole. |
||
185 | * |
||
186 | * @param object|array $object |
||
187 | * |
||
188 | * @return array |
||
189 | */ |
||
190 | 13 | public static function object2array($object) |
|
210 | |||
211 | /** |
||
212 | * Funkce, která provede I/O operaci a vyhodnotí výsledek. |
||
213 | * |
||
214 | * @param string $urlSuffix část URL za identifikátorem firmy. |
||
215 | * @param string $method HTTP/REST metoda |
||
216 | * @param string $format Requested format |
||
217 | */ |
||
218 | 13 | public function performRequest($urlSuffix = null, $method = 'GET', |
|
290 | |||
291 | /** |
||
292 | * Give you last inserted record ID. |
||
293 | * |
||
294 | * @return int |
||
295 | */ |
||
296 | public function getLastInsertedId() |
||
300 | |||
301 | /** |
||
302 | * Convert XML to array. |
||
303 | * |
||
304 | * @param string $xml |
||
305 | * |
||
306 | * @return array |
||
307 | */ |
||
308 | 13 | public static function xml2array($xml) |
|
327 | |||
328 | /** |
||
329 | * Odpojení od FlexiBee. |
||
330 | */ |
||
331 | 1 | public function disconnect() |
|
338 | |||
339 | 1 | public function __destruct() |
|
343 | |||
344 | /** |
||
345 | * Načte data z FlexiBee. |
||
346 | * |
||
347 | * @param string $suffix dotaz |
||
348 | */ |
||
349 | public function loadFlexiData($suffix = null) |
||
353 | |||
354 | /** |
||
355 | * Načte řádek dat z FlexiBee. |
||
356 | * |
||
357 | * @param int $recordID id požadovaného záznamu |
||
358 | * |
||
359 | * @return array |
||
360 | */ |
||
361 | 13 | public function getFlexiRow($recordID) |
|
371 | |||
372 | /** |
||
373 | * Načte data z FlexiBee. |
||
374 | * |
||
375 | * @param string $suffix dotaz |
||
376 | * @param string $conditions Volitelný filtrovací výraz |
||
377 | */ |
||
378 | 13 | public function getFlexiData($suffix = null, $conditions = null) |
|
402 | |||
403 | /** |
||
404 | * Načte záznam z FlexiBee. |
||
405 | * |
||
406 | * @param int $id ID záznamu |
||
407 | * |
||
408 | * @return int počet načtených položek |
||
409 | */ |
||
410 | 13 | public function loadFromFlexiBee($id = null) |
|
418 | |||
419 | /** |
||
420 | * Uloží data do FlexiBee. |
||
421 | * |
||
422 | * @param array $data |
||
423 | * |
||
424 | * @return array výsledek |
||
425 | */ |
||
426 | View Code Duplication | public function saveToFlexiBee($data = null) |
|
438 | |||
439 | /** |
||
440 | * Převede data do Json formátu pro FlexiBee. |
||
441 | * |
||
442 | * @param array $data |
||
443 | * |
||
444 | * @return string |
||
445 | */ |
||
446 | 13 | public function jsonizeData($data) |
|
457 | |||
458 | /** |
||
459 | * Uloží záznam. |
||
460 | * |
||
461 | * @param array $data |
||
462 | * |
||
463 | * @return array odpověď |
||
464 | */ |
||
465 | View Code Duplication | public function insertToFlexiBee($data = null) |
|
475 | |||
476 | /** |
||
477 | * Test if given record ID exists in FlexiBee. |
||
478 | * |
||
479 | * @param string|int $identifer |
||
480 | */ |
||
481 | public function idExists($identifer = null) |
||
491 | |||
492 | /** |
||
493 | * Test if given record exists in FlexiBee. |
||
494 | * |
||
495 | * @param array $data |
||
496 | */ |
||
497 | public function recordExists($data = null) |
||
508 | |||
509 | /** |
||
510 | * Vrací z FlexiBee sloupečky podle podmínek. |
||
511 | * |
||
512 | * @param array|int|string $conditions pole podmínek nebo ID záznamu |
||
513 | * @param array|string $orderBy třídit dle |
||
514 | * @param string $indexBy klice vysledku naplnit hodnotou ze |
||
515 | * sloupečku |
||
516 | * @param int $limit maximální počet vrácených záznamů |
||
517 | * |
||
518 | * @return array |
||
519 | */ |
||
520 | public function getAllFromFlexibee($conditions = null, $orderBy = null, |
||
539 | |||
540 | /** |
||
541 | * Vrací z FlexiBee sloupečky podle podmínek. |
||
542 | * |
||
543 | * @param string[] $columnsList seznam položek |
||
544 | * @param array|int|string $conditions pole podmínek nebo ID záznamu |
||
545 | * @param array|string $orderBy třídit dle |
||
546 | * @param string $indexBy klice vysledku naplnit hodnotou ze |
||
547 | * sloupečku |
||
548 | * @param int $limit maximální počet vrácených záznamů |
||
549 | * |
||
550 | * @return array |
||
551 | */ |
||
552 | public function getColumnsFromFlexibee($columnsList, $conditions = null, |
||
584 | |||
585 | /** |
||
586 | * Vrací kód záznamu. |
||
587 | * |
||
588 | * @param array $data |
||
589 | * |
||
590 | * @todo papat i string |
||
591 | * |
||
592 | * @return string |
||
593 | */ |
||
594 | public function getKod($data = null, $unique = true) |
||
642 | |||
643 | /** |
||
644 | * Vyhledavani v záznamech objektu FlexiBee. |
||
645 | * |
||
646 | * @param string $what hledaný výraz |
||
647 | * |
||
648 | * @return array pole výsledků |
||
649 | */ |
||
650 | public function searchString($what) |
||
703 | |||
704 | /** |
||
705 | * Write Operation Result. |
||
706 | * |
||
707 | * @param array $resultData |
||
708 | * @param string $url URL |
||
709 | */ |
||
710 | public function logResult($resultData, $url = null) |
||
741 | |||
742 | /** |
||
743 | * Generuje fragment url pro filtrování. |
||
744 | * |
||
745 | * @see https://www.flexibee.eu/api/dokumentace/ref/filters |
||
746 | * |
||
747 | * @param array $data |
||
748 | * @param string $operator and/or |
||
749 | * |
||
750 | * @return string |
||
751 | */ |
||
752 | 13 | public static function flexiUrl($data, $operator = 'and') |
|
769 | } |
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..