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) |
||
151 | |||
152 | /** |
||
153 | * @param SplFileInfo $file |
||
154 | * @param string $classPrefix |
||
155 | * @return string |
||
156 | */ |
||
157 | protected function getRealClassname(SplFileInfo $file, $classPrefix) |
||
170 | |||
171 | /** |
||
172 | * @param SplFileInfo $file |
||
173 | * @param string $classPrefix |
||
174 | * @param string $group |
||
175 | * @return string |
||
176 | */ |
||
177 | protected function getClassIdentifier(SplFileInfo $file, $classPrefix, $group = '') |
||
189 | |||
190 | /** |
||
191 | * Verify whether given class is defined in given file because there is no sense in adding class with incorrect |
||
192 | * file or path. Examples: |
||
193 | * app/code/core/Mage/Core/Model/Mysql4/Design/Theme/Collection.php -> Mage_Core_Model_Mysql4_Design_Theme |
||
194 | * app/code/core/Mage/Payment/Model/Paygate/Request.php -> Mage_Paygate_Model_Authorizenet_Request |
||
195 | * app/code/core/Mage/Dataflow/Model/Convert/Iterator.php -> Mage_Dataflow_Model_Session_Adapter_Iterator |
||
196 | * |
||
197 | * @param SplFileInfo $file |
||
198 | * @param string $className |
||
199 | * @param OutputInterface $output |
||
200 | * @return bool |
||
201 | */ |
||
202 | protected function isClassDefinedInFile(SplFileInfo $file, $className, OutputInterface $output) |
||
211 | |||
212 | /** |
||
213 | * Resource helper is always one per module for each db type and uses model alias |
||
214 | * |
||
215 | * @return array |
||
216 | */ |
||
217 | protected function getResourceHelperMap() |
||
237 | |||
238 | /** |
||
239 | * @param string $group |
||
240 | * @param OutputInterface $output |
||
241 | * |
||
242 | *@return array |
||
243 | */ |
||
244 | protected function getClassMapForGroup($group, OutputInterface $output) |
||
345 | |||
346 | /** |
||
347 | * @param InputInterface $input |
||
348 | * @param OutputInterface $output |
||
349 | * @param $classMaps |
||
350 | */ |
||
351 | protected function writeToOutputOld(InputInterface $input, OutputInterface $output, $classMaps) |
||
387 | |||
388 | /** |
||
389 | * @param InputInterface $input |
||
390 | * @param OutputInterface $output |
||
391 | * @param $classMaps |
||
392 | */ |
||
393 | protected function writeToOutputV2017(InputInterface $input, OutputInterface $output, $classMaps) |
||
442 | |||
443 | /** |
||
444 | * @param string $group |
||
445 | * @return \Mage_Core_Model_Config_Element |
||
446 | */ |
||
447 | protected function getGroupXmlDefinition($group) |
||
480 | } |
||
481 |
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.