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 |
||
43 | class SimpleConfigurationLoader implements ConfigurationLoaderInterface |
||
44 | { |
||
45 | |||
46 | /** |
||
47 | * The composer name => Magento Edition mappings. |
||
48 | * |
||
49 | * @var array |
||
50 | */ |
||
51 | protected $editionMappings = array( |
||
52 | 'magento2ce' => 'CE', |
||
53 | 'project-community-edition' => 'CE', |
||
54 | 'magento2ee' => 'EE', |
||
55 | 'project-enterprise-edition' => 'EE' |
||
56 | ); |
||
57 | |||
58 | protected $configurationFileMappings = array( |
||
59 | EntityTypeCodes::NONE => 'techdivision-import', |
||
60 | EntityTypeCodes::EAV_ATTRIBUTE => 'techdivision-import', |
||
61 | EntityTypeCodes::CATALOG_PRODUCT => 'techdivision-import', |
||
62 | EntityTypeCodes::CATALOG_PRODUCT_PRICE => 'techdivision-import-price', |
||
63 | EntityTypeCodes::CATALOG_PRODUCT_INVENTORY => 'techdivision-import-inventory', |
||
64 | EntityTypeCodes::CATALOG_CATEGORY => 'techdivision-import' |
||
65 | ); |
||
66 | |||
67 | /** |
||
68 | * The array with the default entity type => configuration mapping. |
||
69 | * |
||
70 | * @var array |
||
71 | */ |
||
72 | protected $defaultConfigurations = array( |
||
73 | 'ce' => array( |
||
74 | EntityTypeCodes::NONE => 'techdivision/import-product', |
||
75 | EntityTypeCodes::EAV_ATTRIBUTE => 'techdivision/import-attribute', |
||
76 | EntityTypeCodes::CATALOG_PRODUCT => 'techdivision/import-product', |
||
77 | EntityTypeCodes::CATALOG_PRODUCT_PRICE => 'techdivision/import-product', |
||
78 | EntityTypeCodes::CATALOG_PRODUCT_INVENTORY => 'techdivision/import-product', |
||
79 | EntityTypeCodes::CATALOG_CATEGORY => 'techdivision/import-category' |
||
80 | ), |
||
81 | 'ee' => array( |
||
82 | EntityTypeCodes::NONE => 'techdivision/import-product-ee', |
||
83 | EntityTypeCodes::EAV_ATTRIBUTE => 'techdivision/import-attribute', |
||
84 | EntityTypeCodes::CATALOG_PRODUCT => 'techdivision/import-product-ee', |
||
85 | EntityTypeCodes::CATALOG_PRODUCT_PRICE => 'techdivision/import-product-ee', |
||
86 | EntityTypeCodes::CATALOG_PRODUCT_INVENTORY => 'techdivision/import-product-ee', |
||
87 | EntityTypeCodes::CATALOG_CATEGORY => 'techdivision/import-category-ee' |
||
88 | ) |
||
89 | ); |
||
90 | |||
91 | /** |
||
92 | * The container instance. |
||
93 | * |
||
94 | * @var \Symfony\Component\DependencyInjection\ContainerInterface |
||
95 | */ |
||
96 | protected $container; |
||
97 | |||
98 | /** |
||
99 | * The actual input instance. |
||
100 | * |
||
101 | * @var \Symfony\Component\Console\Input\InputInterface |
||
102 | */ |
||
103 | protected $input; |
||
104 | |||
105 | /** |
||
106 | * The library loader instance. |
||
107 | * |
||
108 | * @param \TechDivision\Import\Cli\LibraryLoader |
||
109 | */ |
||
110 | protected $libraryLoader; |
||
111 | |||
112 | /** |
||
113 | * The configuration factory instance. |
||
114 | * |
||
115 | * @var \TechDivision\Import\ConfigurationFactoryInterface |
||
116 | */ |
||
117 | protected $configurationFactory; |
||
118 | |||
119 | /** |
||
120 | * The available command names. |
||
121 | * |
||
122 | * @var \TechDivision\Import\Utils\CommandNames |
||
123 | */ |
||
124 | protected $commandNames; |
||
125 | |||
126 | /** |
||
127 | * The mapping of the command names to the entity type codes |
||
128 | * |
||
129 | * @var \TechDivision\Import\Utils\Mappings\CommandNameToEntityTypeCode |
||
130 | */ |
||
131 | protected $commandNameToEntityTypeCode; |
||
132 | |||
133 | /** |
||
134 | * Initializes the configuration loader. |
||
135 | * |
||
136 | * @param \Symfony\Component\Console\Input\InputInterface $input The input instance |
||
137 | * @param \Symfony\Component\DependencyInjection\ContainerInterface $container The container instance |
||
138 | * @param \TechDivision\Import\Cli\Configuration\LibraryLoader $libraryLoader The configuration loader instance |
||
139 | * @param \TechDivision\Import\ConfigurationFactoryInterface $configurationFactory The configuration factory instance |
||
140 | * @param \TechDivision\Import\Utils\CommandNames $commandNames The available command names |
||
141 | * @param \TechDivision\Import\Utils\Mappings\CommandNameToEntityTypeCode $commandNameToEntityTypeCodes The mapping of the command names to the entity type codes |
||
142 | */ |
||
143 | public function __construct( |
||
160 | |||
161 | /** |
||
162 | * Factory implementation to create a new initialized configuration instance. |
||
163 | * |
||
164 | * If command line options are specified, they will always override the |
||
165 | * values found in the configuration file. |
||
166 | * |
||
167 | * @return \TechDivision\Import\ConfigurationInterface The configuration instance |
||
168 | * @throws \Exception Is thrown, if the specified configuration file doesn't exist or the mandatory arguments/options to run the requested operation are not available |
||
169 | */ |
||
170 | public function load() |
||
274 | |||
275 | /** |
||
276 | * Return's the DI container instance. |
||
277 | * |
||
278 | * @return \Symfony\Component\DependencyInjection\ContainerInterface The DI container instance |
||
279 | */ |
||
280 | protected function getContainer() |
||
284 | |||
285 | /** |
||
286 | * Return's the absolute path to the actual vendor directory. |
||
287 | * |
||
288 | * @return string The absolute path to the actual vendor directory |
||
289 | * @throws \Exception Is thrown, if none of the possible vendor directories can be found |
||
290 | */ |
||
291 | protected function getVendorDir() |
||
295 | |||
296 | /** |
||
297 | * Return's the actual command name. |
||
298 | * |
||
299 | * @return string The actual command name |
||
300 | */ |
||
301 | protected function getCommandName() |
||
305 | |||
306 | /** |
||
307 | * Return's the command's entity type code. |
||
308 | * |
||
309 | * @return string The command's entity type code |
||
310 | * @throws \Exception Is thrown, if the command name can not be mapped |
||
311 | */ |
||
312 | protected function getEntityTypeCode() |
||
323 | |||
324 | /** |
||
325 | * Return's the default configuration for the passed Magento Edition and the actual entity type. |
||
326 | * |
||
327 | * @param string $magentoEdition The Magento Edition to return the configuration for |
||
328 | * @param string $entityTypeCode The entity type code to use |
||
329 | * |
||
330 | * @return string The path to the default configuration |
||
331 | */ |
||
332 | protected function getDefaultConfiguration($magentoEdition, $entityTypeCode) |
||
344 | |||
345 | /** |
||
346 | * Return's the name of the default configuration file. |
||
347 | * |
||
348 | * @param string $entityTypeCode The entity type code to return the default configuration file for |
||
349 | * |
||
350 | * @return string The name of the entity type's default configuration file |
||
351 | * @throws \Exception |
||
352 | */ |
||
353 | protected function getDefaultConfigurationFile($entityTypeCode) |
||
369 | |||
370 | /** |
||
371 | * Return's the Magento Edition and entity type's specific default library that contains |
||
372 | * the configuration file. |
||
373 | * |
||
374 | * @param string $magentoEdition The Magento Edition to return the default library for |
||
375 | * @param string $entityTypeCode The entity type code to return the default library file for |
||
376 | * |
||
377 | * @return string The name of the library that contains the default configuration file for the passed Magento Edition and entity type code |
||
378 | * @throws \Exception Is thrown, if no default configuration for the passed entity type code is available |
||
379 | */ |
||
380 | protected function getDefaultConfigurationLibrary($magentoEdition, $entityTypeCode) |
||
407 | } |
||
408 |