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 |
||
46 | class SimpleConfigurationLoader implements ConfigurationLoaderInterface |
||
47 | { |
||
48 | |||
49 | /** |
||
50 | * The composer name => Magento Edition mappings. |
||
51 | * |
||
52 | * @var array |
||
53 | */ |
||
54 | protected $editionMappings = array( |
||
55 | 'magento2ce' => 'CE', |
||
56 | 'project-community-edition' => 'CE', |
||
57 | 'magento2ee' => 'EE', |
||
58 | 'project-enterprise-edition' => 'EE' |
||
59 | ); |
||
60 | |||
61 | /** |
||
62 | * The array with the default entity type => configuration mapping. |
||
63 | * |
||
64 | * @var array |
||
65 | */ |
||
66 | protected $defaultConfigurations = array( |
||
67 | 'ce' => array( |
||
68 | EntityTypeCodes::NONE => 'techdivision/import-product', |
||
69 | EntityTypeCodes::EAV_ATTRIBUTE => 'techdivision/import-attribute', |
||
70 | EntityTypeCodes::CATALOG_PRODUCT => 'techdivision/import-product', |
||
71 | EntityTypeCodes::CATALOG_CATEGORY => 'techdivision/import-category' |
||
72 | ), |
||
73 | 'ee' => array( |
||
74 | EntityTypeCodes::NONE => 'techdivision/import-product-ee', |
||
75 | EntityTypeCodes::EAV_ATTRIBUTE => 'techdivision/import-attribute', |
||
76 | EntityTypeCodes::CATALOG_PRODUCT => 'techdivision/import-product-ee', |
||
77 | EntityTypeCodes::CATALOG_CATEGORY => 'techdivision/import-category-ee' |
||
78 | ) |
||
79 | ); |
||
80 | |||
81 | /** |
||
82 | * The container instance. |
||
83 | * |
||
84 | * @var \Symfony\Component\DependencyInjection\ContainerInterface |
||
85 | */ |
||
86 | protected $container; |
||
87 | |||
88 | /** |
||
89 | * The actual input instance. |
||
90 | * |
||
91 | * @var \Symfony\Component\Console\Input\InputInterface |
||
92 | */ |
||
93 | protected $input; |
||
94 | |||
95 | /** |
||
96 | * The library loader instance. |
||
97 | * |
||
98 | * @param \TechDivision\Import\Cli\LibraryLoader |
||
99 | */ |
||
100 | protected $libraryLoader; |
||
101 | |||
102 | /** |
||
103 | * The configuration factory instance. |
||
104 | * |
||
105 | * @var \TechDivision\Import\ConfigurationFactoryInterface |
||
106 | */ |
||
107 | protected $configurationFactory; |
||
108 | |||
109 | /** |
||
110 | * The available command names. |
||
111 | * |
||
112 | * @var \TechDivision\Import\Utils\CommandNames |
||
113 | */ |
||
114 | protected $commandNames; |
||
115 | |||
116 | /** |
||
117 | * The mapping of the command names to the entity type codes |
||
118 | * |
||
119 | * @var \TechDivision\Import\Utils\Mappings\CommandNameToEntityTypeCode |
||
120 | */ |
||
121 | protected $commandNameToEntityTypeCode; |
||
122 | |||
123 | /** |
||
124 | * Initializes the configuration loader. |
||
125 | * |
||
126 | * @param \Symfony\Component\Console\Input\InputInterface $input The input instance |
||
127 | * @param \Symfony\Component\DependencyInjection\ContainerInterface $container The container instance |
||
128 | * @param \TechDivision\Import\Cli\LibraryLoader $libraryLoader The configuration loader instance |
||
129 | * @param \TechDivision\Import\ConfigurationFactoryInterface $configurationFactory The configuration factory instance |
||
130 | * @param \TechDivision\Import\Utils\CommandNames $commandNames The available command names |
||
131 | * @param \TechDivision\Import\Utils\Mappings\CommandNameToEntityTypeCode $commandNameToEntityTypeCodes The mapping of the command names to the entity type codes |
||
132 | */ |
||
133 | public function __construct( |
||
150 | |||
151 | /** |
||
152 | * Factory implementation to create a new initialized configuration instance. |
||
153 | * |
||
154 | * If command line options are specified, they will always override the |
||
155 | * values found in the configuration file. |
||
156 | * |
||
157 | * @return \TechDivision\Import\Cli\Configuration The configuration instance |
||
158 | * @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 |
||
159 | */ |
||
160 | public function load() |
||
264 | |||
265 | /** |
||
266 | * Return's the DI container instance. |
||
267 | * |
||
268 | * @return \Symfony\Component\DependencyInjection\ContainerInterface The DI container instance |
||
269 | */ |
||
270 | protected function getContainer() |
||
274 | |||
275 | /** |
||
276 | * Return's the absolute path to the actual vendor directory. |
||
277 | * |
||
278 | * @return string The absolute path to the actual vendor directory |
||
279 | * @throws \Exception Is thrown, if none of the possible vendor directories can be found |
||
280 | */ |
||
281 | protected function getVendorDir() |
||
285 | |||
286 | /** |
||
287 | * Return's the actual command name. |
||
288 | * |
||
289 | * @return string The actual command name |
||
290 | */ |
||
291 | protected function getCommandName() |
||
295 | |||
296 | /** |
||
297 | * Return's the command's entity type code. |
||
298 | * |
||
299 | * @return string The command's entity type code |
||
300 | * @throws \Exception Is thrown, if the command name can not be mapped |
||
301 | */ |
||
302 | protected function getEntityTypeCode() |
||
313 | |||
314 | /** |
||
315 | * Return's the default configuration for the passed Magento Edition and the actual entity type. |
||
316 | * |
||
317 | * @param string $magentoEdition The Magento Edition to return the configuration for |
||
318 | * @param string $entityTypeCode The entity type code to use |
||
319 | * |
||
320 | * @return string The path to the default configuration |
||
321 | */ |
||
322 | protected function getDefaultConfiguration($magentoEdition, $entityTypeCode) |
||
333 | |||
334 | /** |
||
335 | * Return's the Magento Edition and entity type's specific default library that contains |
||
336 | * the configuration file. |
||
337 | * |
||
338 | * @param string $magentoEdition The Magento Edition to return the default library for |
||
339 | * @param string $entityTypeCode The entity type code to return the default library file for |
||
340 | * |
||
341 | * @return string The name of the library that contains the default configuration file for the passed Magento Edition and entity type code |
||
342 | * @throws \Exception Is thrown, if no default configuration for the passed entity type code is available |
||
343 | */ |
||
344 | protected function getDefaultConfigurationLibrary($magentoEdition, $entityTypeCode) |
||
371 | } |
||
372 |