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 MetaCommand 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 MetaCommand, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class MetaCommand extends AbstractMagentoCommand |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * @var array |
||
| 19 | */ |
||
| 20 | protected $groups = array( |
||
| 21 | 'blocks', |
||
| 22 | 'helpers', |
||
| 23 | 'models', |
||
| 24 | 'resource models', |
||
| 25 | 'resource helpers', |
||
| 26 | ); |
||
| 27 | |||
| 28 | /** |
||
| 29 | * List of supported static factory methods |
||
| 30 | * |
||
| 31 | * @var array |
||
| 32 | */ |
||
| 33 | protected $groupFactories = array( |
||
| 34 | 'blocks' => array( |
||
| 35 | '\Mage::getBlockSingleton', |
||
| 36 | ), |
||
| 37 | 'helpers' => array( |
||
| 38 | '\Mage::helper', |
||
| 39 | ), |
||
| 40 | 'models' => array( |
||
| 41 | '\Mage::getModel', |
||
| 42 | '\Mage::getSingleton', |
||
| 43 | ), |
||
| 44 | 'resource helpers' => array( |
||
| 45 | '\Mage::getResourceHelper', |
||
| 46 | ), |
||
| 47 | 'resource models' => array( |
||
| 48 | '\Mage::getResourceModel', |
||
| 49 | '\Mage::getResourceSingleton', |
||
| 50 | ), |
||
| 51 | ); |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var array |
||
| 55 | */ |
||
| 56 | protected $missingHelperDefinitionModules = array( |
||
| 57 | 'Backup', |
||
| 58 | 'Bundle', |
||
| 59 | 'Captcha', |
||
| 60 | 'Catalog', |
||
| 61 | 'Centinel', |
||
| 62 | 'Checkout', |
||
| 63 | 'Cms', |
||
| 64 | 'Core', |
||
| 65 | 'Customer', |
||
| 66 | 'Dataflow', |
||
| 67 | 'Directory', |
||
| 68 | 'Downloadable', |
||
| 69 | 'Eav', |
||
| 70 | 'Index', |
||
| 71 | 'Install', |
||
| 72 | 'Log', |
||
| 73 | 'Media', |
||
| 74 | 'Newsletter', |
||
| 75 | 'Page', |
||
| 76 | 'Payment', |
||
| 77 | 'Paypal', |
||
| 78 | 'Persistent', |
||
| 79 | 'Poll', |
||
| 80 | 'Rating', |
||
| 81 | 'Reports', |
||
| 82 | 'Review', |
||
| 83 | 'Rss', |
||
| 84 | 'Rule', |
||
| 85 | 'Sales', |
||
| 86 | 'Shipping', |
||
| 87 | 'Sitemap', |
||
| 88 | 'Tag', |
||
| 89 | 'Tax', |
||
| 90 | 'Usa', |
||
| 91 | 'Weee', |
||
| 92 | 'Widget', |
||
| 93 | 'Wishlist', |
||
| 94 | ); |
||
| 95 | |||
| 96 | const VERSION_OLD = 'old'; |
||
| 97 | const VERSION_2017 = '2016.2+'; |
||
| 98 | |||
| 99 | protected function configure() |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @param InputInterface $input |
||
| 116 | * @param OutputInterface $output |
||
| 117 | * |
||
| 118 | * @internal param string $package |
||
| 119 | * @return void |
||
| 120 | */ |
||
| 121 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @param SplFileInfo $file |
||
| 149 | * @param string $classPrefix |
||
| 150 | * @return string |
||
| 151 | */ |
||
| 152 | protected function getRealClassname(SplFileInfo $file, $classPrefix) |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @param SplFileInfo $file |
||
| 168 | * @param string $classPrefix |
||
| 169 | * @param string $group |
||
| 170 | * @return string |
||
| 171 | */ |
||
| 172 | protected function getClassIdentifier(SplFileInfo $file, $classPrefix, $group = '') |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Verify whether given class is defined in given file because there is no sense in adding class with incorrect |
||
| 187 | * file or path. Examples: |
||
| 188 | * app/code/core/Mage/Core/Model/Mysql4/Design/Theme/Collection.php -> Mage_Core_Model_Mysql4_Design_Theme |
||
| 189 | * app/code/core/Mage/Payment/Model/Paygate/Request.php -> Mage_Paygate_Model_Authorizenet_Request |
||
| 190 | * app/code/core/Mage/Dataflow/Model/Convert/Iterator.php -> Mage_Dataflow_Model_Session_Adapter_Iterator |
||
| 191 | * |
||
| 192 | * @param SplFileInfo $file |
||
| 193 | * @param string $className |
||
| 194 | * @param OutputInterface $output |
||
| 195 | * @return bool |
||
| 196 | */ |
||
| 197 | protected function isClassDefinedInFile(SplFileInfo $file, $className, OutputInterface $output) |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Resource helper is always one per module for each db type and uses model alias |
||
| 209 | * |
||
| 210 | * @return array |
||
| 211 | */ |
||
| 212 | protected function getResourceHelperMap() |
||
| 232 | |||
| 233 | /** |
||
| 234 | * @param string $group |
||
| 235 | * @param OutputInterface $output |
||
| 236 | * |
||
| 237 | *@return array |
||
| 238 | */ |
||
| 239 | protected function getClassMapForGroup($group, OutputInterface $output) |
||
| 340 | |||
| 341 | /** |
||
| 342 | * @param InputInterface $input |
||
| 343 | * @param OutputInterface $output |
||
| 344 | * @param $classMaps |
||
| 345 | */ |
||
| 346 | protected function writeToOutputOld(InputInterface $input, OutputInterface $output, $classMaps) |
||
| 382 | |||
| 383 | /** |
||
| 384 | * @param InputInterface $input |
||
| 385 | * @param OutputInterface $output |
||
| 386 | * @param $classMaps |
||
| 387 | */ |
||
| 388 | protected function writeToOutputV2017(InputInterface $input, OutputInterface $output, $classMaps) |
||
| 437 | |||
| 438 | /** |
||
| 439 | * @param string $group |
||
| 440 | * @return \Mage_Core_Model_Config_Element |
||
| 441 | */ |
||
| 442 | protected function getGroupXmlDefinition($group) |
||
| 475 | } |
||
| 476 |
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.