| Conditions | 7 |
| Paths | 7 |
| Total Lines | 80 |
| Code Lines | 51 |
| Lines | 8 |
| Ratio | 10 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 36 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 37 | { |
||
| 38 | $this->detectMagento($output, true); |
||
| 39 | $this->initMagento(); |
||
| 40 | |||
| 41 | $output->writeln("<warning>This only create sample categories, do not use on production environment</warning>"); |
||
| 42 | |||
| 43 | // Ask for Arguments |
||
| 44 | $_argument = $this->askForArguments($input, $output); |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Loop to create categories |
||
| 48 | */ |
||
| 49 | for($i = 0; $i < $_argument['category-number']; $i++) { |
||
| 50 | if(!is_null($_argument['category-name-prefix'])) { |
||
| 51 | $name = $_argument['category-name-prefix']." ".$i; |
||
| 52 | } |
||
| 53 | else { |
||
| 54 | $name = self::DEFAULT_CATEGORY_NAME." ".$i; |
||
| 55 | } |
||
| 56 | |||
| 57 | // Check if product exists |
||
| 58 | $collection = Mage::getModel('catalog/category')->getCollection() |
||
| 59 | ->addAttributeToSelect('name') |
||
| 60 | ->addAttributeToFilter('name', array('eq' => $name)); |
||
| 61 | $_size = $collection->getSize(); |
||
| 62 | if($_size > 0) { |
||
| 63 | $output->writeln("<comment>CATEGORY: WITH NAME: '".$name."' EXISTS! Skip</comment>"); |
||
| 64 | $_argument['category-number']++; |
||
| 65 | continue; |
||
| 66 | } |
||
| 67 | unset($collection); |
||
| 68 | |||
| 69 | $_category_root_id = Mage::app()->getStore($_argument['store-id'])->getRootCategoryId(); |
||
| 70 | |||
| 71 | $category = Mage::getModel('catalog/category'); |
||
| 72 | $category->setName($name); |
||
| 73 | $category->setIsActive(self::DEFAULT_CATEGORY_STATUS); |
||
| 74 | $category->setDisplayMode('PRODUCTS'); |
||
| 75 | $category->setIsAnchor(self::DEFAULT_CATEGORY_ANCHOR); |
||
| 76 | |||
| 77 | View Code Duplication | if(Mage::getVersion() === "1.5.1.0") { |
|
| 78 | $category->setStoreId(array(0, $_argument['store-id'])); |
||
| 79 | } |
||
| 80 | else { |
||
| 81 | $category->setStoreId($_argument['store-id']); |
||
| 82 | } |
||
| 83 | $parentCategory = Mage::getModel('catalog/category')->load($_category_root_id); |
||
| 84 | $category->setPath($parentCategory->getPath()); |
||
| 85 | |||
| 86 | $category->save(); |
||
| 87 | $_parent_id = $category->getId(); |
||
| 88 | $output->writeln("<comment>CATEGORY: '".$category->getName()."' WITH ID: '".$category->getId()."' CREATED!</comment>"); |
||
| 89 | unset($category); |
||
| 90 | |||
| 91 | // Create children Categories |
||
| 92 | for($j = 0; $j < $_argument['children-categories-number']; $j++) { |
||
| 93 | $name_child = $name." child ".$j; |
||
| 94 | |||
| 95 | $category = Mage::getModel('catalog/category'); |
||
| 96 | $category->setName($name_child); |
||
| 97 | $category->setIsActive(self::DEFAULT_CATEGORY_STATUS); |
||
| 98 | $category->setDisplayMode('PRODUCTS'); |
||
| 99 | $category->setIsAnchor(self::DEFAULT_CATEGORY_ANCHOR); |
||
| 100 | |||
| 101 | View Code Duplication | if(Mage::getVersion() === "1.5.1.0") { |
|
| 102 | $category->setStoreId(array(0, $_argument['store-id'])); |
||
| 103 | } |
||
| 104 | else { |
||
| 105 | $category->setStoreId($_argument['store-id']); |
||
| 106 | } |
||
| 107 | $parentCategory = Mage::getModel('catalog/category')->load($_parent_id); |
||
| 108 | $category->setPath($parentCategory->getPath()); |
||
| 109 | |||
| 110 | $category->save(); |
||
| 111 | $output->writeln("<comment>CATEGORY CHILD: '".$category->getName()."' WITH ID: '".$category->getId()."' CREATED!</comment>"); |
||
| 112 | unset($category); |
||
| 113 | } |
||
| 114 | } |
||
| 115 | } |
||
| 116 | |||
| 196 |
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.