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) |
||
56 | { |
||
57 | $info = $this->getEvidenceInfo(); |
||
58 | switch ($info['importStatus']) { |
||
59 | case 'DISALLOWED': |
||
60 | throw new \Exception(sprintf('Inserting data to r/o evidence %s', |
||
61 | $this->getEvidence())); |
||
62 | case 'NOT_DIRECT': |
||
63 | throw new \Exception(sprintf('Inserting data to slave only evidence %s', |
||
64 | $this->getEvidence())); |
||
65 | case 'NOT_DOCUMENTED': |
||
66 | if ($this->debug === true) { |
||
67 | $this->addStatusMessage(sprintf('Inserting data to undocumneted evidence %s', |
||
68 | $this->getEvidence())); |
||
69 | } |
||
70 | |||
71 | break; |
||
72 | case 'SUPPORTED': |
||
73 | default: |
||
74 | break; |
||
75 | } |
||
76 | |||
77 | if (is_null($data)) { |
||
78 | $data = $this->getData(); |
||
79 | } |
||
80 | if ($this->debug === true) { |
||
81 | $missingColumns = $this->controlMandatoryColumns(); |
||
82 | if (count($missingColumns)) { |
||
83 | $this->addStatusMessage(sprintf('Given data does not contain requied Columns: %s', |
||
84 | implode(',', $missingColumns)) |
||
85 | , 'warning'); |
||
86 | } |
||
87 | } |
||
88 | $this->postFields = $this->jsonizeData($data); |
||
89 | return $this->performRequest($this->evidence.'.'.$this->format, 'PUT'); |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * Give you last inserted record ID. |
||
94 | * |
||
95 | * @return int |
||
96 | */ |
||
97 | public function getLastInsertedId() |
||
101 | |||
102 | /** |
||
103 | * Smaže záznam |
||
104 | * Delete record in FlexiBee |
||
105 | * |
||
106 | * @param int|string $id identifikátor záznamu |
||
107 | * @return boolean Response code is 200 ? |
||
108 | */ |
||
109 | public function deleteFromFlexiBee($id = null) |
||
118 | |||
119 | /** |
||
120 | * Control for existing column names in evidence and take data |
||
121 | * |
||
122 | * @param array $data Data to keep |
||
123 | * @return int number of records taken |
||
124 | */ |
||
125 | public function takeData($data) |
||
126 | { |
||
127 | if ($this->debug === true) { |
||
128 | $fbColumns = $this->getColumnsInfo(); |
||
129 | foreach ($this->getRelationsInfo() as $relation) { |
||
130 | if (is_array($relation) && isset($relation['url'])) { |
||
131 | $fbRelations[$relation['url']] = $relation['url']; |
||
|
|||
132 | } |
||
133 | } |
||
134 | |||
135 | if (count($fbColumns)) { |
||
136 | foreach ($data as $key => $value) { |
||
137 | if (!array_key_exists($key, $fbColumns)) { |
||
138 | |||
139 | if (!array_key_exists($key, $fbRelations)) { |
||
140 | $this->addStatusMessage(sprintf('unknown column %s for evidence %s', |
||
141 | $key, $this->getEvidence()), 'warning'); |
||
142 | } else { |
||
143 | if (!is_array($value)) { |
||
144 | $this->addStatusMessage(sprintf('subevidence %s in evidence %s must bee an array', |
||
145 | $key, $this->getEvidence()), 'warning'); |
||
146 | } |
||
147 | } |
||
148 | } |
||
149 | } |
||
150 | } |
||
151 | } |
||
152 | return parent::takeData($data); |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * Control data for mandatory columns presence. |
||
157 | * |
||
158 | * @param array $data |
||
159 | * @return array List of missing columns. Empty if all is ok |
||
160 | */ |
||
161 | View Code Duplication | public function controlMandatoryColumns($data = null) |
|
162 | { |
||
163 | if (is_null($data)) { |
||
164 | $data = $this->getData(); |
||
165 | } |
||
166 | |||
167 | $missingMandatoryColumns = []; |
||
168 | |||
169 | $fbColumns = $this->getColumnsInfo(); |
||
170 | foreach ($fbColumns as $columnName => $columnInfo) { |
||
171 | $mandatory = ($columnInfo['mandatory'] == 'true'); |
||
172 | if ($mandatory && !array_key_exists($columnName, $data)) { |
||
173 | $missingMandatoryColumns[$columnName] = $columnInfo['name']; |
||
174 | } |
||
175 | } |
||
176 | |||
177 | return $missingMandatoryColumns; |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * Control data for readonly columns presence. |
||
182 | * |
||
183 | * @param array $data |
||
184 | * @return array List of ReadOnly columns. Empty if all is ok |
||
185 | */ |
||
186 | View Code Duplication | public function controlReadOnlyColumns($data = null) |
|
204 | |||
205 | /** |
||
206 | * Convert Timestamp to FlexiBee Date format. |
||
207 | * |
||
208 | * @param int $timpestamp |
||
209 | * |
||
210 | * @return string FlexiBee Date or NULL |
||
211 | */ |
||
212 | View Code Duplication | public static function timestampToFlexiDate($timpestamp = null) |
|
222 | |||
223 | /** |
||
224 | * Convert Timestamp to Flexi DateTime format. |
||
225 | * |
||
226 | * @param int $timpestamp |
||
227 | * |
||
228 | * @return string FlexiBee DateTime or NULL |
||
229 | */ |
||
230 | View Code Duplication | public static function timestampToFlexiDateTime($timpestamp = null) |
|
240 | |||
241 | /** |
||
242 | * Přidá data do větve |
||
243 | * |
||
244 | * @thanksto Karel Běl |
||
245 | * @param array $data pole dat |
||
246 | * @param string $type path evidence pro vkládaná data |
||
247 | * @see Relations |
||
248 | */ |
||
249 | public function addArrayToBranch($data, $type) |
||
273 | |||
274 | /** |
||
275 | * Vloží do větve data z objektu |
||
276 | * |
||
277 | * @param FlexiBeeRO $object objekt evidence |
||
278 | */ |
||
279 | public function addObjectToBranch($object) |
||
283 | |||
284 | /** |
||
285 | * Převede data do Json formátu pro FlexiBee. |
||
286 | * Pokud jsou štítky pole, jsou převedeny na seznam oddělený čárkou. |
||
287 | * Convert data to FlexiBee like Json format. |
||
288 | * Array of Labels is converted to coma separated list |
||
289 | * |
||
290 | * @param array $data |
||
291 | * |
||
292 | * @return string |
||
293 | */ |
||
294 | public function jsonizeData($data) |
||
303 | |||
304 | } |
||
305 |
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.