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 |
||
| 22 | class BaseImport extends CI_Model |
||
| 23 | { |
||
|
|
|||
| 24 | |||
| 25 | /** |
||
| 26 | * Class BaseImport |
||
| 27 | * @var BaseImport |
||
| 28 | */ |
||
| 29 | protected static $_instance; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Id currency |
||
| 33 | * @var Int |
||
| 34 | */ |
||
| 35 | public $currency = 2; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Charset |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | public $encoding = 'utf-8'; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * language |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | public $languages = 'ru'; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * language |
||
| 51 | * @var string |
||
| 52 | */ |
||
| 53 | public $mainLanguages; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Path to file |
||
| 57 | * @var string |
||
| 58 | */ |
||
| 59 | public $CSVsource = ''; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * CSV delimiter |
||
| 63 | * @var string |
||
| 64 | */ |
||
| 65 | public $delimiter = ';'; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * CSV enclosure |
||
| 69 | * @var string |
||
| 70 | */ |
||
| 71 | public $enclosure = '"'; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Import type |
||
| 75 | * @var string |
||
| 76 | */ |
||
| 77 | public $importType = ''; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Attributes |
||
| 81 | * @var array |
||
| 82 | */ |
||
| 83 | public $attributes = ''; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * The maximum number of fields |
||
| 87 | * @var int |
||
| 88 | */ |
||
| 89 | public $maxRowLength = 0; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Content |
||
| 93 | * @var array |
||
| 94 | */ |
||
| 95 | public $content = []; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Settings |
||
| 99 | * @var array |
||
| 100 | */ |
||
| 101 | public $settings = []; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Possible attributes |
||
| 105 | * @var array |
||
| 106 | */ |
||
| 107 | public $possibleAttributes = []; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Count products in CSV file |
||
| 111 | * @var int |
||
| 112 | */ |
||
| 113 | public $countProduct; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @var array |
||
| 117 | */ |
||
| 118 | public $allLanguages = []; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @var int |
||
| 122 | */ |
||
| 123 | public $maxRowLegth; |
||
| 124 | |||
| 125 | public function __construct() { |
||
| 131 | |||
| 132 | private function getLangs() { |
||
| 141 | |||
| 142 | /** |
||
| 143 | * BaseImport Singleton |
||
| 144 | * @return BaseImport |
||
| 145 | * @access public |
||
| 146 | * @author Kaero |
||
| 147 | * @copyright ImageCMS (c) 2012, Kaero <[email protected]> |
||
| 148 | */ |
||
| 149 | public static function create() { |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Start CSV Import |
||
| 156 | * @param integer $offers The final position |
||
| 157 | * @param integer $limit Step |
||
| 158 | * @param integer $countProd count products |
||
| 159 | * @param $EmptyFields |
||
| 160 | * @return false|null |
||
| 161 | * @access public |
||
| 162 | * @author Kaero |
||
| 163 | * @copyright ImageCMS (c) 2012, Kaero <[email protected]> |
||
| 164 | */ |
||
| 165 | public function makeImport($offers, $limit, $countProd, $EmptyFields) { |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Get attributes list. |
||
| 185 | * @return BaseImport |
||
| 186 | * @access public |
||
| 187 | * @author Kaero |
||
| 188 | * @copyright ImageCMS (c) 2012, Kaero <[email protected]> |
||
| 189 | */ |
||
| 190 | public function makeAttributesList() { |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Validate Information and parse CSV. As a goal we have $content variable with file information. |
||
| 239 | * @param integer $offers The final position |
||
| 240 | * @param integer $limit Step |
||
| 241 | * @return false|null |
||
| 242 | * @access public |
||
| 243 | * @author Kaero |
||
| 244 | * @copyright ImageCMS (c) 2012, Kaero <[email protected]> |
||
| 245 | */ |
||
| 246 | public function validateFile($offers, $limit) { |
||
| 278 | |||
| 279 | /** |
||
| 280 | * @param string $attribute |
||
| 281 | * @return bool |
||
| 282 | */ |
||
| 283 | public function attributeExist($attribute) { |
||
| 292 | |||
| 293 | /** |
||
| 294 | * File parsing |
||
| 295 | * @param integer $offers The final position |
||
| 296 | * @param integer $limit Step |
||
| 297 | * @param $file |
||
| 298 | * @return boolean |
||
| 299 | */ |
||
| 300 | public function parseFile($offers, $limit, $file = false) { |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Process Categories |
||
| 338 | * @access public |
||
| 339 | * @author Kaero |
||
| 340 | * @copyright ImageCMS (c) 2012, Kaero <[email protected]> |
||
| 341 | */ |
||
| 342 | public function loadCategories() { |
||
| 361 | |||
| 362 | /** |
||
| 363 | * @param string $node |
||
| 364 | * @param string $key |
||
| 365 | */ |
||
| 366 | private function content($node, $key) { |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Parse Category Name by slashes |
||
| 443 | * @param string $name |
||
| 444 | * @return array |
||
| 445 | * @access private |
||
| 446 | * @author Kaero |
||
| 447 | * @copyright ImageCMS (c) 2012, Kaero <[email protected]> |
||
| 448 | */ |
||
| 449 | private function parseCategoryName($name) { |
||
| 453 | |||
| 454 | /** |
||
| 455 | * FROM PropertiesImport |
||
| 456 | */ |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Process Properties Handling |
||
| 460 | * @access public |
||
| 461 | * @author Kaero |
||
| 462 | * @copyright ImageCMS (c) 2012, Kaero <[email protected]> |
||
| 463 | */ |
||
| 464 | public function runProperties() { |
||
| 549 | |||
| 550 | /** |
||
| 551 | * @param int $property_id |
||
| 552 | * @param int $product_id |
||
| 553 | * @param int $value_id |
||
| 554 | * @return bool |
||
| 555 | */ |
||
| 556 | private function checkPropertiesData($property_id, $product_id, $value_id) { |
||
| 573 | |||
| 574 | /** |
||
| 575 | * Set Import Type. Must be setted before import start. |
||
| 576 | * @param $type |
||
| 577 | * @return BaseImport |
||
| 578 | * @access public |
||
| 579 | * @author Kaero |
||
| 580 | * @copyright ImageCMS (c) 2012, Kaero <[email protected]> |
||
| 581 | */ |
||
| 582 | public function setImportType($type) { |
||
| 586 | |||
| 587 | /** |
||
| 588 | * FROM CategoryImport |
||
| 589 | */ |
||
| 590 | |||
| 591 | /** |
||
| 592 | * Set Import Settings. Must be setted before import start. |
||
| 593 | * @param $settings |
||
| 594 | * @return BaseImport |
||
| 595 | * @access public |
||
| 596 | * @author Kaero |
||
| 597 | * @copyright ImageCMS (c) 2012, Kaero <[email protected]> |
||
| 598 | */ |
||
| 599 | public function setSettings($settings) { |
||
| 604 | |||
| 605 | /** |
||
| 606 | * Set Import file name. Must be setted before import start. |
||
| 607 | * @param string $fileName |
||
| 608 | * @return BaseImport |
||
| 609 | * @access public |
||
| 610 | * @author Kaero |
||
| 611 | * @copyright ImageCMS (c) 2012, Kaero <[email protected]> |
||
| 612 | */ |
||
| 613 | public function setFileName($fileName) { |
||
| 628 | |||
| 629 | /** |
||
| 630 | * Add new value to custom field and save it. |
||
| 631 | * @param mixed $name |
||
| 632 | * @param mixed $value |
||
| 633 | * @access public |
||
| 634 | * @return void |
||
| 635 | * @author Kaero |
||
| 636 | * @copyright ImageCMS (c) 2012, Kaero <[email protected]> |
||
| 637 | */ |
||
| 638 | public function addCustomFieldValue($name, $value) { |
||
| 656 | |||
| 657 | } |