Conditions | 7 |
Paths | 7 |
Total Lines | 83 |
Code Lines | 55 |
Lines | 8 |
Ratio | 9.64 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
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 | |||
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.