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 FlexiBeeRW 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 FlexiBeeRW, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class FlexiBeeRW extends FlexiBeeRO |
||
17 | { |
||
18 | /** |
||
19 | * Sloupeček obsahující datum vložení záznamu do shopu. |
||
20 | * |
||
21 | * @var string |
||
22 | */ |
||
23 | public $myCreateColumn = 'false'; |
||
24 | |||
25 | /** |
||
26 | * Slopecek obsahujici datum poslení modifikace záznamu do shopu. |
||
27 | * |
||
28 | * @var string |
||
29 | */ |
||
30 | public $myLastModifiedColumn = 'lastUpdate'; |
||
31 | |||
32 | /** |
||
33 | * Last Inserted ID. |
||
34 | * |
||
35 | * @var int |
||
36 | */ |
||
37 | public $lastInsertedID = null; |
||
38 | |||
39 | /** |
||
40 | * Array of fields for next curl POST operation |
||
41 | * |
||
42 | * @var string |
||
43 | */ |
||
44 | public $postFields = null; |
||
45 | |||
46 | /** |
||
47 | * Save record (if evidence allow to). |
||
48 | * Uloží záznam (pokud to evidence dovoluje) |
||
49 | * |
||
50 | * @param array $data Data to save |
||
51 | * @throws Exception Evidence does not support Import |
||
52 | * |
||
53 | * @return array odpověď |
||
54 | */ |
||
55 | public function insertToFlexiBee($data = null) |
||
92 | |||
93 | /** |
||
94 | * Give you last inserted record ID. |
||
95 | * |
||
96 | * @return int |
||
97 | */ |
||
98 | public function getLastInsertedId() |
||
102 | |||
103 | /** |
||
104 | * Smaže záznam |
||
105 | * Delete record in FlexiBee |
||
106 | * |
||
107 | * @param int|string $id identifikátor záznamu |
||
108 | * @return boolean Response code is 200 ? |
||
109 | */ |
||
110 | public function deleteFromFlexiBee($id = null) |
||
119 | |||
120 | /** |
||
121 | * Control for existing column names in evidence and take data |
||
122 | * |
||
123 | * @param array $data Data to keep |
||
124 | * @return int number of records taken |
||
125 | */ |
||
126 | public function takeData($data) |
||
155 | |||
156 | /** |
||
157 | * Control data for mandatory columns presence. |
||
158 | * |
||
159 | * @param array $data |
||
160 | * @return array List of missing columns. Empty if all is ok |
||
161 | */ |
||
162 | View Code Duplication | public function controlMandatoryColumns($data = null) |
|
180 | |||
181 | /** |
||
182 | * Control data for readonly columns presence. |
||
183 | * |
||
184 | * @param array $data |
||
185 | * @return array List of ReadOnly columns. Empty if all is ok |
||
186 | */ |
||
187 | View Code Duplication | public function controlReadOnlyColumns($data = null) |
|
205 | |||
206 | /** |
||
207 | * Convert Timestamp to FlexiBee Date format. |
||
208 | * |
||
209 | * @param int $timpestamp |
||
210 | * |
||
211 | * @return string FlexiBee Date or NULL |
||
212 | */ |
||
213 | View Code Duplication | public static function timestampToFlexiDate($timpestamp = null) |
|
223 | |||
224 | /** |
||
225 | * Convert Timestamp to Flexi DateTime format. |
||
226 | * |
||
227 | * @param int $timpestamp |
||
228 | * |
||
229 | * @return string FlexiBee DateTime or NULL |
||
230 | */ |
||
231 | View Code Duplication | public static function timestampToFlexiDateTime($timpestamp = null) |
|
241 | |||
242 | /** |
||
243 | * Add Data to evidence Branch |
||
244 | * Přidá data do větve |
||
245 | * |
||
246 | * @thanksto Karel Běl |
||
247 | * @param array $data pole dat |
||
248 | * @param string $relationPath path evidence (relation) pro vkládaná data |
||
249 | * @see Relations |
||
250 | */ |
||
251 | public function addArrayToBranch($data, $relationPath) |
||
266 | |||
267 | /** |
||
268 | * Vloží do větve data z objektu |
||
269 | * |
||
270 | * @param FlexiBeeRO $object objekt evidence |
||
271 | */ |
||
272 | public function addObjectToBranch($object) |
||
276 | |||
277 | /** |
||
278 | * @see https://www.flexibee.eu/api/dokumentace/ref/uzivatelske-vazby/ |
||
279 | */ |
||
280 | public function vazbaAdd() |
||
285 | |||
286 | /** |
||
287 | * @ |
||
288 | */ |
||
289 | public function vazbaDel() |
||
293 | |||
294 | /** |
||
295 | * Převede data do Json formátu pro FlexiBee. |
||
296 | * Pokud jsou štítky pole, jsou převedeny na seznam oddělený čárkou. |
||
297 | * Convert data to FlexiBee like Json format. |
||
298 | * Array of Labels is converted to coma separated list |
||
299 | * |
||
300 | * @param array $data |
||
301 | * |
||
302 | * @return string |
||
303 | */ |
||
304 | public function jsonizeData($data) |
||
313 | |||
314 | /** |
||
315 | * Insert current data into FlexiBee and load actual record data back |
||
316 | */ |
||
317 | public function refresh() |
||
322 | } |
||
323 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.