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 BaseImport 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 BaseImport, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class BaseImport extends CI_Model |
||
19 | { |
||
|
|||
20 | |||
21 | /** |
||
22 | * Class BaseImport |
||
23 | * @var BaseImport |
||
24 | */ |
||
25 | protected static $_instance; |
||
26 | |||
27 | /** |
||
28 | * Id currency |
||
29 | * @var Int |
||
30 | */ |
||
31 | public $currency = 2; |
||
32 | |||
33 | /** |
||
34 | * Charset |
||
35 | * @var string |
||
36 | */ |
||
37 | public $encoding = 'utf-8'; |
||
38 | |||
39 | /** |
||
40 | * language |
||
41 | * @var string |
||
42 | */ |
||
43 | public $languages = 'ru'; |
||
44 | |||
45 | /** |
||
46 | * language |
||
47 | * @var string |
||
48 | */ |
||
49 | public $mainLanguages; |
||
50 | |||
51 | /** |
||
52 | * Path to file |
||
53 | * @var string |
||
54 | */ |
||
55 | public $CSVsource = ''; |
||
56 | |||
57 | /** |
||
58 | * CSV delimiter |
||
59 | * @var string |
||
60 | */ |
||
61 | public $delimiter = ';'; |
||
62 | |||
63 | /** |
||
64 | * CSV enclosure |
||
65 | * @var string |
||
66 | */ |
||
67 | public $enclosure = '"'; |
||
68 | |||
69 | /** |
||
70 | * Import type |
||
71 | * @var string |
||
72 | */ |
||
73 | public $importType = ''; |
||
74 | |||
75 | /** |
||
76 | * Attributes |
||
77 | * @var array |
||
78 | */ |
||
79 | public $attributes = ''; |
||
80 | |||
81 | /** |
||
82 | * The maximum number of fields |
||
83 | * @var int |
||
84 | */ |
||
85 | public $maxRowLength = 0; |
||
86 | |||
87 | /** |
||
88 | * Content |
||
89 | * @var array |
||
90 | */ |
||
91 | public $content = []; |
||
92 | |||
93 | /** |
||
94 | * Settings |
||
95 | * @var array |
||
96 | */ |
||
97 | public $settings = []; |
||
98 | |||
99 | /** |
||
100 | * Possible attributes |
||
101 | * @var array |
||
102 | */ |
||
103 | public $possibleAttributes = []; |
||
104 | |||
105 | /** |
||
106 | * Count products in CSV file |
||
107 | * @var int |
||
108 | */ |
||
109 | public $countProduct; |
||
110 | |||
111 | public $allLanguages; |
||
112 | |||
113 | public function __construct() { |
||
114 | parent::__construct(); |
||
115 | $this->languages = My_Controller::getCurrentLocale(); |
||
116 | $this->languages = $this->input->post('language') ?: $this->languages; |
||
117 | $this->getLangs(); |
||
118 | } |
||
119 | |||
120 | private function getLangs() { |
||
121 | $langs = $this->db->get('languages')->result(); |
||
122 | foreach ($langs as $val) { |
||
123 | $this->allLanguages[] = $val->identif; |
||
124 | if ($val->default == 1) { |
||
125 | $this->mainLanguages = $val->identif; |
||
126 | } |
||
127 | } |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * Start CSV Import |
||
132 | * @param integer $offers The final position |
||
133 | * @param integer $limit Step |
||
134 | * @param integer $countProd count products |
||
135 | * @param $EmptyFields |
||
136 | * @return false|null |
||
137 | * @access public |
||
138 | * @author Kaero |
||
139 | * @copyright ImageCMS (c) 2012, Kaero <[email protected]> |
||
140 | */ |
||
141 | public function makeImport($offers, $limit, $countProd, $EmptyFields) { |
||
142 | |||
143 | $this->makeAttributesList(); |
||
144 | if ($offers == 0) { |
||
145 | $this->validateFile($offers, $limit); |
||
146 | } else { |
||
147 | $this->parseFile($offers, $limit); |
||
148 | $this->loadCategories($EmptyFields); |
||
149 | ProductsHandler::create()->make($EmptyFields); |
||
150 | $this->runProperties(); |
||
151 | } |
||
152 | if (ImportBootstrap::noErrors()) { |
||
153 | ImportBootstrap::create()->addMessage(Factor::SuccessImportCompleted . '<b>' . $countProd . '</b>', Factor::MessageTypeSuccess); |
||
154 | } else { |
||
155 | return FALSE; |
||
156 | } |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * @param string $attribute |
||
161 | * @return bool |
||
162 | */ |
||
163 | public function attributeExist($attribute) { |
||
164 | $attributes = \CI::$APP->input->post('attributes'); |
||
165 | if (!$attributes) { |
||
166 | return TRUE; |
||
167 | } |
||
168 | |||
169 | $attributes = explode(',', $attributes); |
||
170 | return in_array($attribute, $attributes) ? TRUE : FALSE; |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * Validate Information and parse CSV. As a goal we have $content variable with file information. |
||
175 | * @param integer $offers The final position |
||
176 | * @param integer $limit Step |
||
177 | * @return false|null |
||
178 | * @access public |
||
179 | * @author Kaero |
||
180 | * @copyright ImageCMS (c) 2012, Kaero <[email protected]> |
||
181 | */ |
||
182 | public function validateFile($offers, $limit) { |
||
183 | |||
184 | if (substr(sprintf('%o', fileperms(ImportBootstrap::getUploadDir())), -4) != '0777') { |
||
185 | ImportBootstrap::addMessage(Factor::ErrorFolderPermission); |
||
186 | return FALSE; |
||
187 | } |
||
188 | if (!$file = @fopen($this->CSVsource, 'r')) { |
||
189 | ImportBootstrap::addMessage(Factor::ErrorFileReadError); |
||
190 | return FALSE; |
||
191 | } |
||
192 | |||
193 | $row = fgetcsv($file, $this->maxRowLegth, $this->delimiter, $this->enclosure); |
||
194 | // if (!in_array('cat', $row) && !$this->attributeExist('cat')) { |
||
195 | // ImportBootstrap::addMessage(Factor::ErrorCategoryAttribute); |
||
196 | // return FALSE; |
||
197 | // } |
||
198 | // if (!in_array('name', $row) && !$this->attributeExist('name')) { |
||
199 | // ImportBootstrap::addMessage(Factor::ErrorNameAttribute); |
||
200 | // return FALSE; |
||
201 | // } |
||
202 | // Првоерка на url |
||
203 | // if (!in_array('url', $row)) { |
||
204 | // ImportBootstrap::addMessage(Factor::ErrorUrlAttribute); |
||
205 | // return FALSE; |
||
206 | // } |
||
207 | View Code Duplication | if (!in_array('prc', $row) && !$this->attributeExist('prc')) { |
|
208 | ImportBootstrap::addMessage(Factor::ErrorPriceAttribute); |
||
209 | return FALSE; |
||
210 | } |
||
211 | // if (!in_array('var', $row) && !$this->attributeExist('var')) { |
||
212 | // ImportBootstrap::addMessage(Factor::ErrorNameVariantAttribute); |
||
213 | // return FALSE; |
||
214 | // } |
||
215 | View Code Duplication | if (!in_array('num', $row) && !$this->attributeExist('prc') && $this->importType == Factor::ImportProducts) { |
|
216 | ImportBootstrap::addMessage(Factor::ErrorNumberAttribute); |
||
217 | return FALSE; |
||
218 | } |
||
219 | if ((count($this->possibleAttributes) - count(array_diff($this->possibleAttributes, $row))) == count($this->attributes)) { |
||
220 | $this->attributes = $row; |
||
221 | } elseif (count($row) === count($this->attributes)) { |
||
222 | rewind($file); |
||
223 | } else { |
||
224 | ImportBootstrap::addMessage(Factor::ErrorPossibleAttrValues); |
||
225 | return FALSE; |
||
226 | } |
||
227 | $this->parseFile($offers, $limit, $file); |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * File parsing |
||
232 | * @param integer $offers The final position |
||
233 | * @param integer $limit Step |
||
234 | * @param $file |
||
235 | * @return boolean |
||
236 | */ |
||
237 | public function parseFile($offers, $limit, $file = false) { |
||
238 | if (!$file) { |
||
239 | $file = @fopen($this->CSVsource, 'r'); |
||
240 | } |
||
241 | if ($offers > 0) { |
||
242 | $positionStart = $offers - $limit; |
||
243 | $cnt = 0; |
||
244 | $iOffer = 0; |
||
245 | while (($row = fgetcsv($file, $this->maxRowLegth, $this->delimiter, $this->enclosure)) !== false) { |
||
246 | // if ($cnt != 0) { |
||
247 | // if ($iOffer < $positionStart) { |
||
248 | // $iOffer++; |
||
249 | // }else{ |
||
250 | // $this->content[] = array_combine($this->attributes, array_map('trim', $row)); |
||
251 | // } |
||
252 | // } |
||
253 | // $cnt = 1; |
||
254 | // } |
||
255 | if ($cnt != 0) { |
||
256 | if ($iOffer < $positionStart) { |
||
257 | $iOffer++; |
||
258 | } else { |
||
259 | $this->content[] = array_combine($this->attributes, array_map('trim', $row)); |
||
260 | } |
||
261 | } |
||
262 | if ($cnt >= $offers) { |
||
263 | break; |
||
264 | } |
||
265 | $cnt++; |
||
266 | } |
||
267 | } else { |
||
268 | $cnt = 0; |
||
269 | while (($row = fgetcsv($file, $this->maxRowLegth, $this->delimiter, $this->enclosure)) !== false) { |
||
270 | if ($cnt != 0) { |
||
271 | $this->countProduct++; |
||
272 | } |
||
273 | $cnt = 1; |
||
274 | } |
||
275 | $_SESSION['countProductsInFile'] = $this->countProduct; |
||
276 | } |
||
277 | fclose($file); |
||
278 | return TRUE; |
||
279 | } |
||
280 | |||
281 | /** |
||
282 | * Set Import Type. Must be setted before import start. |
||
283 | * @param $type |
||
284 | * @return BaseImport |
||
285 | * @access public |
||
286 | * @author Kaero |
||
287 | * @copyright ImageCMS (c) 2012, Kaero <[email protected]> |
||
288 | */ |
||
289 | public function setImportType($type) { |
||
290 | $this->importType = $type; |
||
291 | return $this; |
||
292 | } |
||
293 | |||
294 | /** |
||
295 | * Set Import Settings. Must be setted before import start. |
||
296 | * @param $settings |
||
297 | * @return BaseImport |
||
298 | * @access public |
||
299 | * @author Kaero |
||
300 | * @copyright ImageCMS (c) 2012, Kaero <[email protected]> |
||
301 | */ |
||
302 | public function setSettings($settings) { |
||
303 | $this->settings = $settings; |
||
304 | $this->attributes = array_diff(explode(',', $this->settings['attributes']), [null]); |
||
305 | return $this; |
||
306 | } |
||
307 | |||
308 | /** |
||
309 | * Set Import file name. Must be setted before import start. |
||
310 | * @param string $fileName |
||
311 | * @return BaseImport |
||
312 | * @access public |
||
313 | * @author Kaero |
||
314 | * @copyright ImageCMS (c) 2012, Kaero <[email protected]> |
||
315 | */ |
||
316 | public function setFileName($fileName) { |
||
317 | try { |
||
318 | if (FALSE === file_exists($fileName)) { |
||
319 | throw new Exception(Factor::ErrorEmptySlot); |
||
320 | } |
||
321 | $this->CSVsource = $fileName; |
||
322 | return $this; |
||
323 | } catch (Exception $exc) { |
||
324 | $result[Factor::MessageTypeSuccess] = FALSE; |
||
325 | $result[Factor::MessageTypeError] = FALSE; |
||
326 | $result['message'] = $exc->getMessage(); |
||
327 | echo json_encode($result); |
||
328 | exit(); |
||
329 | } |
||
330 | } |
||
331 | |||
332 | /** |
||
333 | * BaseImport Singleton |
||
334 | * @return BaseImport |
||
335 | * @access public |
||
336 | * @author Kaero |
||
337 | * @copyright ImageCMS (c) 2012, Kaero <[email protected]> |
||
338 | */ |
||
339 | public static function create() { |
||
343 | |||
344 | /** |
||
345 | * Get attributes list. |
||
346 | * @return BaseImport |
||
347 | * @access public |
||
348 | * @author Kaero |
||
349 | * @copyright ImageCMS (c) 2012, Kaero <[email protected]> |
||
350 | */ |
||
351 | public function makeAttributesList() { |
||
396 | |||
397 | /** |
||
398 | * FROM PropertiesImport |
||
399 | */ |
||
400 | |||
401 | /** |
||
402 | * Process Properties Handling |
||
403 | * @access public |
||
404 | * @author Kaero |
||
405 | * @copyright ImageCMS (c) 2012, Kaero <[email protected]> |
||
406 | */ |
||
407 | public function runProperties() { |
||
479 | |||
480 | /** |
||
481 | * Add new value to custom field and save it. |
||
482 | * @param mixed $name |
||
483 | * @param mixed $value |
||
484 | * @access public |
||
485 | * @return void |
||
486 | * @author Kaero |
||
487 | * @copyright ImageCMS (c) 2012, Kaero <[email protected]> |
||
488 | */ |
||
489 | View Code Duplication | public function addCustomFieldValue($name, $value) { |
|
507 | |||
508 | /** |
||
509 | * FROM CategoryImport |
||
510 | */ |
||
511 | |||
512 | /** |
||
513 | * Process Categories |
||
514 | * @access public |
||
515 | * @author Kaero |
||
516 | * @copyright ImageCMS (c) 2012, Kaero <[email protected]> |
||
517 | */ |
||
518 | public function loadCategories() { |
||
537 | |||
538 | /** |
||
539 | * @param string $node |
||
540 | * @param string $key |
||
541 | */ |
||
542 | private function content($node, $key) { |
||
601 | |||
602 | /** |
||
603 | * Parse Category Name by slashes |
||
604 | * @param string $name |
||
605 | * @return array |
||
606 | * @access private |
||
607 | * @author Kaero |
||
608 | * @copyright ImageCMS (c) 2012, Kaero <[email protected]> |
||
609 | */ |
||
610 | private function parseCategoryName($name) { |
||
614 | |||
615 | } |