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 | 26 | public function __construct($init = null) |
|
159 | |||
160 | 26 | 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', |
|
219 | $format = null) |
||
220 | { |
||
221 | 13 | if (is_null($format)) { |
|
222 | 13 | $format = $this->format; |
|
223 | 13 | } |
|
224 | 13 | if (is_null($urlSuffix)) { |
|
225 | 2 | $urlSuffix = $this->agenda.'.'.$format; |
|
226 | 2 | } |
|
227 | 13 | $url = $this->url.$this->prefix.$this->company.'/'.$urlSuffix; |
|
228 | 13 | curl_setopt($this->curl, CURLOPT_URL, $url); |
|
229 | // Nastavení samotné operace |
||
230 | 13 | curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, $method); |
|
231 | |||
232 | // Proveď samotnou operaci |
||
233 | 13 | $response = curl_exec($this->curl); |
|
234 | |||
235 | 13 | $this->info = curl_getinfo($this->curl); |
|
|
|||
236 | |||
237 | 13 | $responseCode = curl_getinfo($this->curl, CURLINFO_HTTP_CODE); |
|
238 | |||
239 | 13 | if ($responseCode != 200 && $responseCode != 201) { |
|
240 | 13 | $this->error = curl_error($this->curl); |
|
241 | 13 | $response = (json_encode(json_decode($response, true, 10), |
|
242 | 13 | JSON_PRETTY_PRINT)); |
|
243 | |||
244 | 13 | $response = preg_replace_callback('/\\\\u([0-9a-fA-F]{4})/', |
|
245 | 13 | function ($match) { |
|
246 | 13 | return mb_convert_encoding(pack('H*', $match[1]), 'UTF-8', |
|
247 | 13 | 'UCS-2BE'); |
|
248 | 13 | }, $response); |
|
249 | |||
250 | 13 | if ($responseCode == 400) { |
|
251 | 1 | $this->logResult(self::object2array(current(json_decode($response)))); |
|
252 | 1 | } else { |
|
253 | 13 | $this->addStatusMessage(sprintf('Error (HTTP %d): <pre>%s</pre> %s', |
|
254 | 13 | curl_getinfo($this->curl, CURLINFO_HTTP_CODE), |
|
255 | 13 | stripslashes($response), $this->error), 'error'); |
|
256 | 13 | $this->addStatusMessage($url); |
|
257 | } |
||
258 | 13 | if ($response == 'null') { |
|
259 | return; |
||
260 | } |
||
261 | |||
262 | 13 | return self::object2array(current(json_decode($response))); |
|
263 | } |
||
264 | |||
265 | // Parse response |
||
266 | switch ($format) { |
||
267 | 12 | case 'json': |
|
268 | 12 | $decoded = json_decode($response, true, 10); |
|
269 | 12 | if (($method == 'PUT') && isset($decoded[$this->nameSpace][$this->resultField][0]['id'])) { |
|
270 | $this->lastInsertedID = $decoded[$this->nameSpace][$this->resultField][0]['id']; |
||
271 | } else { |
||
272 | 12 | $this->lastInsertedID = null; |
|
273 | } |
||
274 | // $decodeError = json_last_error_msg(); |
||
275 | // $this->addStatusMessage($decodeError); |
||
276 | |||
277 | 12 | break; |
|
278 | 2 | case 'xml': |
|
279 | 2 | $decoded = self::xml2array($response); |
|
280 | 2 | break; |
|
281 | } |
||
282 | |||
283 | // Get response body root automatically |
||
284 | 12 | if (isset($decoded[$this->nameSpace])) { |
|
285 | 12 | $decoded = $decoded[$this->nameSpace]; |
|
286 | 12 | } |
|
287 | |||
288 | 12 | return $decoded; |
|
289 | } |
||
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) |
|
309 | { |
||
310 | 13 | $arr = []; |
|
311 | |||
312 | 13 | if (is_string($xml)) { |
|
313 | 13 | $xml = simplexml_load_string($xml); |
|
314 | 13 | } |
|
315 | |||
316 | 13 | foreach ($xml->children() as $r) { |
|
317 | 13 | $t = []; |
|
318 | 13 | if (count($r->children()) == 0) { |
|
319 | 13 | $arr[$r->getName()] = strval($r); |
|
320 | 13 | } else { |
|
321 | 13 | $arr[$r->getName()][] = self::xml2array($r); |
|
322 | } |
||
323 | 13 | } |
|
324 | |||
325 | 13 | return $arr; |
|
326 | } |
||
327 | |||
328 | /** |
||
329 | * Odpojení od FlexiBee. |
||
330 | */ |
||
331 | 14 | public function disconnect() |
|
338 | |||
339 | 14 | 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 | 12 | public function getFlexiData($suffix = null, $conditions = null) |
|
379 | { |
||
380 | 12 | if (!is_null($conditions)) { |
|
381 | 10 | if ($conditions[0] != '/') { |
|
382 | 10 | $conditions = '/'.rawurlencode('('.($conditions).')'); |
|
383 | 10 | } |
|
384 | 10 | } else { |
|
385 | 12 | $conditions = ''; |
|
386 | } |
||
387 | 12 | if ($suffix) { |
|
388 | $transactions = $this->performRequest($this->agenda.$conditions.'.'.$this->format.'?'.$suffix, |
||
389 | 'GET'); |
||
390 | } else { |
||
391 | 12 | $transactions = $this->performRequest($this->agenda.$conditions.'.'.$this->format, |
|
392 | 12 | 'GET'); |
|
393 | } |
||
394 | 12 | if (isset($transactions[$this->agenda])) { |
|
395 | 10 | $result = $transactions[$this->agenda]; |
|
396 | 10 | } else { |
|
397 | 2 | $result = $transactions; |
|
398 | } |
||
399 | |||
400 | 12 | return $result; |
|
401 | } |
||
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 | 13 | public function logResult($resultData, $url = null) |
|
711 | { |
||
712 | 13 | if ($url) { |
|
713 | 13 | $this->logger->addStatusMessage($url); |
|
714 | 13 | } |
|
715 | |||
716 | 13 | if (isset($resultData['results'])) { |
|
717 | 13 | $status = null; |
|
718 | 13 | if ($resultData['success'] == 'false') { |
|
719 | 13 | $status = 'error'; |
|
720 | 13 | } else { |
|
721 | 13 | $status = 'success'; |
|
722 | } |
||
723 | 13 | foreach ($resultData['results'] as $result) { |
|
724 | 13 | if (isset($result['request-id'])) { |
|
725 | 13 | $rid = $result['request-id']; |
|
726 | 13 | } else { |
|
727 | 13 | $rid = ''; |
|
728 | } |
||
729 | 13 | if (isset($result['errors'])) { |
|
730 | 13 | foreach ($result['errors'] as $error) { |
|
731 | 13 | $this->logger->addStatusMessage($rid.': '.$error['message'], |
|
732 | 13 | $status); |
|
733 | 13 | } |
|
734 | 13 | } |
|
735 | 13 | } |
|
736 | 13 | } |
|
737 | 13 | if (is_object($this->logger)) { |
|
738 | 13 | $this->logger->flush(get_class($this)); |
|
739 | 13 | } |
|
740 | 13 | } |
|
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(array $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..