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:
1 | <?php |
||
13 | class DummyCommand extends \N98\Magento\Command\AbstractMagentoCommand |
||
14 | { |
||
15 | const DEFAULT_CATEGORY_NAME = "My Awesome Category"; |
||
16 | const DEFAULT_CATEGORY_STATUS = 1; // enabled |
||
17 | const DEFAULT_CATEGORY_ANCHOR = 1; // enabled |
||
18 | const DEFAULT_STORE_ID = 1; // Default Store ID |
||
19 | |||
20 | protected function configure() |
||
21 | { |
||
22 | $this |
||
23 | ->setName('category:create:dummy') |
||
24 | ->addArgument('store-id', InputArgument::OPTIONAL, 'Id of Store to create categories (default: 1)') |
||
25 | ->addArgument('category-number', InputArgument::OPTIONAL, 'Number of categories to create (default: 1)') |
||
26 | ->addArgument( |
||
27 | 'children-categories-number', |
||
28 | InputArgument::OPTIONAL, |
||
29 | "Number of children for each category created (default: 0 - use '-1' for random from 0 to 5)" |
||
30 | ) |
||
31 | ->addArgument( |
||
32 | 'category-name-prefix', |
||
33 | InputArgument::OPTIONAL, |
||
34 | "Category Name Prefix (default: 'My Awesome Category')" |
||
35 | ) |
||
36 | ->setDescription('Create a dummy category'); |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * @param InputInterface $input |
||
41 | * @param OutputInterface $output |
||
42 | * |
||
43 | * @return int|void |
||
44 | */ |
||
45 | protected function execute(InputInterface $input, OutputInterface $output) |
||
46 | { |
||
47 | $this->detectMagento($output, true); |
||
48 | $this->initMagento(); |
||
49 | |||
50 | $output->writeln("<warning>This only create sample categories, do not use on production environment</warning>"); |
||
51 | |||
52 | // Ask for Arguments |
||
53 | $_argument = $this->askForArguments($input, $output); |
||
54 | |||
55 | /** |
||
56 | * Loop to create categories |
||
57 | */ |
||
58 | for ($i = 0; $i < $_argument['category-number']; $i++) { |
||
59 | if (!is_null($_argument['category-name-prefix'])) { |
||
60 | $name = $_argument['category-name-prefix'] . " " . $i; |
||
61 | } else { |
||
62 | $name = self::DEFAULT_CATEGORY_NAME . " " . $i; |
||
63 | } |
||
64 | |||
65 | // Check if product exists |
||
66 | $collection = Mage::getModel('catalog/category')->getCollection() |
||
67 | ->addAttributeToSelect('name') |
||
68 | ->addAttributeToFilter('name', array('eq' => $name)); |
||
69 | $_size = $collection->getSize(); |
||
70 | if ($_size > 0) { |
||
71 | $output->writeln("<comment>CATEGORY: WITH NAME: '" . $name . "' EXISTS! Skip</comment>\r"); |
||
72 | $_argument['category-number']++; |
||
73 | continue; |
||
74 | } |
||
75 | unset($collection); |
||
76 | |||
77 | $_category_root_id = Mage::app()->getStore($_argument['store-id'])->getRootCategoryId(); |
||
78 | |||
79 | $category = Mage::getModel('catalog/category'); |
||
80 | $category->setName($name); |
||
81 | $category->setIsActive(self::DEFAULT_CATEGORY_STATUS); |
||
82 | $category->setDisplayMode('PRODUCTS'); |
||
83 | $category->setIsAnchor(self::DEFAULT_CATEGORY_ANCHOR); |
||
84 | |||
85 | View Code Duplication | if (Mage::getVersion() === "1.5.1.0") { |
|
|
|||
86 | $category->setStoreId(array(0, $_argument['store-id'])); |
||
87 | } else { |
||
88 | $category->setStoreId($_argument['store-id']); |
||
89 | } |
||
90 | $parentCategory = Mage::getModel('catalog/category')->load($_category_root_id); |
||
91 | $category->setPath($parentCategory->getPath()); |
||
92 | |||
93 | $category->save(); |
||
94 | $_parent_id = $category->getId(); |
||
95 | $output->writeln( |
||
96 | "<comment>CATEGORY: '" . $category->getName() . "' WITH ID: '" . $category->getId() . |
||
97 | "' CREATED!</comment>" |
||
98 | ); |
||
99 | unset($category); |
||
100 | |||
101 | // Create children Categories |
||
102 | for ($j = 0; $j < $_argument['children-categories-number']; $j++) { |
||
103 | $name_child = $name . " child " . $j; |
||
104 | |||
105 | $category = Mage::getModel('catalog/category'); |
||
106 | $category->setName($name_child); |
||
107 | $category->setIsActive(self::DEFAULT_CATEGORY_STATUS); |
||
108 | $category->setDisplayMode('PRODUCTS'); |
||
109 | $category->setIsAnchor(self::DEFAULT_CATEGORY_ANCHOR); |
||
110 | |||
111 | View Code Duplication | if (Mage::getVersion() === "1.5.1.0") { |
|
112 | $category->setStoreId(array(0, $_argument['store-id'])); |
||
113 | } else { |
||
114 | $category->setStoreId($_argument['store-id']); |
||
115 | } |
||
116 | $parentCategory = Mage::getModel('catalog/category')->load($_parent_id); |
||
117 | $category->setPath($parentCategory->getPath()); |
||
118 | |||
119 | $category->save(); |
||
120 | $output->writeln( |
||
121 | "<comment>CATEGORY CHILD: '" . $category->getName() . "' WITH ID: '" . $category->getId() . |
||
122 | "' CREATED!</comment>" |
||
123 | ); |
||
124 | unset($category); |
||
125 | } |
||
126 | } |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * Ask for command arguments |
||
131 | * |
||
132 | * @param InputInterface $input |
||
133 | * @param OutputInterface $output |
||
134 | * |
||
135 | * @return array |
||
136 | */ |
||
137 | private function askForArguments($input, $output) |
||
219 | } |
||
220 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.