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 ConfigurationFactory 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 ConfigurationFactory, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
43 | class ConfigurationFactory extends \TechDivision\Import\Configuration\Jms\ConfigurationFactory |
||
44 | { |
||
45 | |||
46 | /** |
||
47 | * The composer name => Magento Edition mappings. |
||
48 | * |
||
49 | * @var array |
||
50 | */ |
||
51 | protected static $editionMappings = array( |
||
52 | 'magento/magento2ce' => 'CE', |
||
53 | 'magento/project-communit-edition' => 'CE', |
||
54 | 'magento/magento2ee' => 'EE', |
||
55 | 'magento/project-enterprise-edition' => 'EE' |
||
56 | ); |
||
57 | |||
58 | /** |
||
59 | * The array with the default entity type code => import directory mappings. |
||
60 | * |
||
61 | * @var array |
||
62 | */ |
||
63 | protected static $defaultDirectories = array( |
||
64 | EntityTypeCodes::CATALOG_PRODUCT => 'products', |
||
65 | EntityTypeCodes::CATALOG_CATEGORY => 'categories', |
||
66 | EntityTypeCodes::EAV_ATTRIBUTE => 'attributes' |
||
67 | ); |
||
68 | |||
69 | /** |
||
70 | * The array with the default entity type => configuration mapping. |
||
71 | * |
||
72 | * @var array |
||
73 | */ |
||
74 | protected static $defaultConfigurations = array( |
||
75 | 'ce' => array( |
||
76 | EntityTypeCodes::EAV_ATTRIBUTE => 'techdivision/import-attribute', |
||
77 | EntityTypeCodes::CATALOG_PRODUCT => 'techdivision/import-product', |
||
78 | EntityTypeCodes::CATALOG_CATEGORY => 'techdivision/import-category' |
||
79 | ), |
||
80 | 'ee' => array( |
||
81 | EntityTypeCodes::EAV_ATTRIBUTE => 'techdivision/import-attribute', |
||
82 | EntityTypeCodes::CATALOG_PRODUCT => 'techdivision/import-product-ee', |
||
83 | EntityTypeCodes::CATALOG_CATEGORY => 'techdivision/import-category-ee' |
||
84 | ) |
||
85 | ); |
||
86 | |||
87 | /** |
||
88 | * The Magento Edition specific default libraries. |
||
89 | * |
||
90 | * @var array |
||
91 | */ |
||
92 | protected static $defaultLibraries = array( |
||
93 | 'ce' => array( |
||
94 | 'techdivision/import-app-simple', |
||
95 | 'techdivision/import', |
||
96 | 'techdivision/import-attribute', |
||
97 | 'techdivision/import-category', |
||
98 | 'techdivision/import-product', |
||
99 | 'techdivision/import-product-bundle', |
||
100 | 'techdivision/import-product-link', |
||
101 | 'techdivision/import-product-media', |
||
102 | 'techdivision/import-product-variant' |
||
103 | ), |
||
104 | 'ee' => array( |
||
105 | 'techdivision/import-app-simple', |
||
106 | 'techdivision/import', |
||
107 | 'techdivision/import-ee', |
||
108 | 'techdivision/import-attribute', |
||
109 | 'techdivision/import-category', |
||
110 | 'techdivision/import-category-ee', |
||
111 | 'techdivision/import-product', |
||
112 | 'techdivision/import-product-ee', |
||
113 | 'techdivision/import-product-bundle', |
||
114 | 'techdivision/import-product-bundle-ee', |
||
115 | 'techdivision/import-product-link', |
||
116 | 'techdivision/import-product-link-ee', |
||
117 | 'techdivision/import-product-media', |
||
118 | 'techdivision/import-product-media-ee', |
||
119 | 'techdivision/import-product-variant', |
||
120 | 'techdivision/import-product-variant-ee' |
||
121 | ) |
||
122 | ); |
||
123 | |||
124 | /** |
||
125 | * Factory implementation to create a new initialized configuration instance. |
||
126 | * |
||
127 | * If command line options are specified, they will always override the |
||
128 | * values found in the configuration file. |
||
129 | * |
||
130 | * @param \TechDivision\Import\Cli\Command\ImportCommandInterface $command The command that tries to create the instance |
||
131 | * @param \Symfony\Component\Console\Input\InputInterface $input The Symfony console input instance |
||
132 | * |
||
133 | * @return \TechDivision\Import\Cli\Configuration The configuration instance |
||
134 | * @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 |
||
135 | */ |
||
136 | public static function load(ImportCommandInterface $command, InputInterface $input) |
||
320 | |||
321 | /** |
||
322 | * Query whether or not, the passed directory is a Magento root directory. |
||
323 | * |
||
324 | * @param string $dir The directory to query |
||
325 | * |
||
326 | * @return boolean TRUE if the directory is a Magento root directory, else FALSE |
||
327 | */ |
||
328 | public static function isMagentoRootDir($dir) |
||
332 | |||
333 | /** |
||
334 | * Return's the path to the Magento file with the environment configuration. |
||
335 | * |
||
336 | * @param string $dir The path to the Magento root directory |
||
337 | * |
||
338 | * @return string The path to the Magento file with the environment configuration |
||
339 | */ |
||
340 | public static function getMagentoEnv($dir) |
||
344 | |||
345 | /** |
||
346 | * Return's the requested Magento DB connction data. |
||
347 | * |
||
348 | * @param string $dir The path to the Magento root directory |
||
349 | * @param string $connectionName The connection name to return the data for |
||
350 | * |
||
351 | * @return array The connection data |
||
352 | * @throws \Exception Is thrown, if the requested DB connection is not available |
||
353 | */ |
||
354 | public static function getMagentoDbConnection($dir, $connectionName = 'default') |
||
377 | |||
378 | /** |
||
379 | * Create's and return's a new database configuration instance, initialized with |
||
380 | * the passed values. |
||
381 | * |
||
382 | * @param string $dsn The DSN to use |
||
383 | * @param string $username The username to use |
||
384 | * @param string|null $password The passed to use |
||
385 | * @param boolean $default TRUE if this should be the default connection |
||
386 | * @param string $id The ID to use |
||
387 | * |
||
388 | * @return \TechDivision\Import\Configuration\Jms\Configuration\Database The database configuration instance |
||
389 | */ |
||
390 | public static function newDatabaseConfiguration($dsn, $username = 'root', $password = null, $default = true, $id = null) |
||
415 | |||
416 | /** |
||
417 | * Create's and return's a new DSN from the passed values. |
||
418 | * |
||
419 | * @param string $host The host to use |
||
420 | * @param string $dbName The database name to use |
||
421 | * @param string $charset The charset to use |
||
422 | * |
||
423 | * @return string The DSN |
||
424 | */ |
||
425 | public static function newDsn($host, $dbName, $charset = 'utf8') |
||
429 | |||
430 | /** |
||
431 | * Return's the default configuration for the passed Magento Edition and the actual entity type. |
||
432 | * |
||
433 | * @param \TechDivision\Import\Cli\Command\ImportCommandInterface $command The command instance |
||
434 | * @param string $magentoEdition The Magento Edition to return the configuration for |
||
435 | * |
||
436 | * @return string The path to the default configuration |
||
437 | */ |
||
438 | public static function getDefaultConfiguration(ImportCommandInterface $command, $magentoEdition) |
||
449 | |||
450 | /** |
||
451 | * Return's the Magento Edition specific default libraries. Supported Magento Editions are CE or EE. |
||
452 | * |
||
453 | * @param string $magentoEdition The Magento Edition to return the libraries for |
||
454 | * |
||
455 | * @return array The Magento Edition specific default libraries |
||
456 | * @throws \Exception Is thrown, if the passed Magento Edition is NOT supported |
||
457 | */ |
||
458 | public static function getDefaultLibraries($magentoEdition) |
||
474 | |||
475 | /** |
||
476 | * Return's the Magento Edition and entity type's specific default library that contains |
||
477 | * the configuration file. |
||
478 | * |
||
479 | * @param string $magentoEdition The Magento Edition to return the default library for |
||
480 | * @param string $entityTypeCode The entity type code to return the default library file for |
||
481 | * |
||
482 | * @return string The name of the library that contains the default configuration file for the passed Magento Edition and entity type code |
||
483 | * @throws \Exception Is thrown, if no default configuration for the passed entity type code is available |
||
484 | */ |
||
485 | public static function getDefaultConfigurationLibrary($magentoEdition, $entityTypeCode) |
||
512 | |||
513 | /** |
||
514 | * Return's the entity types specific default import directory. |
||
515 | * |
||
516 | * @param string $entityTypeCode The entity type code to return the default import directory for |
||
517 | * |
||
518 | * @return string The default default import directory |
||
519 | * @throws \Exception Is thrown, if no default import directory for the passed entity type code is available |
||
520 | */ |
||
521 | public static function getDefaultDirectory($entityTypeCode) |
||
537 | } |
||
538 |