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 ConfigurationLoader 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 ConfigurationLoader, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 42 | class ConfigurationLoader |
||
| 43 | { |
||
| 44 | |||
| 45 | /** |
||
| 46 | * The composer name => Magento Edition mappings. |
||
| 47 | * |
||
| 48 | * @var array |
||
| 49 | */ |
||
| 50 | protected $editionMappings = array( |
||
| 51 | 'magento2ce' => 'CE', |
||
| 52 | 'project-community-edition' => 'CE', |
||
| 53 | 'magento2ee' => 'EE', |
||
| 54 | 'project-enterprise-edition' => 'EE' |
||
| 55 | ); |
||
| 56 | |||
| 57 | /** |
||
| 58 | * The array with the default entity type code => import directory mappings. |
||
| 59 | * |
||
| 60 | * @var array |
||
| 61 | */ |
||
| 62 | protected $defaultDirectories = array( |
||
| 63 | EntityTypeCodes::CATALOG_PRODUCT => 'products', |
||
| 64 | EntityTypeCodes::CATALOG_CATEGORY => 'categories', |
||
| 65 | EntityTypeCodes::EAV_ATTRIBUTE => 'attributes' |
||
| 66 | ); |
||
| 67 | |||
| 68 | /** |
||
| 69 | * The array with the default entity type => configuration mapping. |
||
| 70 | * |
||
| 71 | * @var array |
||
| 72 | */ |
||
| 73 | protected $defaultConfigurations = array( |
||
| 74 | 'ce' => array( |
||
| 75 | EntityTypeCodes::EAV_ATTRIBUTE => 'techdivision/import-attribute', |
||
| 76 | EntityTypeCodes::CATALOG_PRODUCT => 'techdivision/import-product', |
||
| 77 | EntityTypeCodes::CATALOG_CATEGORY => 'techdivision/import-category' |
||
| 78 | ), |
||
| 79 | 'ee' => array( |
||
| 80 | EntityTypeCodes::EAV_ATTRIBUTE => 'techdivision/import-attribute', |
||
| 81 | EntityTypeCodes::CATALOG_PRODUCT => 'techdivision/import-product-ee', |
||
| 82 | EntityTypeCodes::CATALOG_CATEGORY => 'techdivision/import-category-ee' |
||
| 83 | ) |
||
| 84 | ); |
||
| 85 | |||
| 86 | /** |
||
| 87 | * The Magento Edition specific default libraries. |
||
| 88 | * |
||
| 89 | * @var array |
||
| 90 | */ |
||
| 91 | protected $defaultLibraries = array( |
||
| 92 | 'ce' => array( |
||
| 93 | 'techdivision/import-app-simple', |
||
| 94 | 'techdivision/import', |
||
| 95 | 'techdivision/import-attribute', |
||
| 96 | 'techdivision/import-category', |
||
| 97 | 'techdivision/import-product', |
||
| 98 | 'techdivision/import-product-bundle', |
||
| 99 | 'techdivision/import-product-link', |
||
| 100 | 'techdivision/import-product-media', |
||
| 101 | 'techdivision/import-product-variant' |
||
| 102 | ), |
||
| 103 | 'ee' => array( |
||
| 104 | 'techdivision/import-app-simple', |
||
| 105 | 'techdivision/import', |
||
| 106 | 'techdivision/import-ee', |
||
| 107 | 'techdivision/import-attribute', |
||
| 108 | 'techdivision/import-category', |
||
| 109 | 'techdivision/import-category-ee', |
||
| 110 | 'techdivision/import-product', |
||
| 111 | 'techdivision/import-product-ee', |
||
| 112 | 'techdivision/import-product-bundle', |
||
| 113 | 'techdivision/import-product-bundle-ee', |
||
| 114 | 'techdivision/import-product-link', |
||
| 115 | 'techdivision/import-product-link-ee', |
||
| 116 | 'techdivision/import-product-media', |
||
| 117 | 'techdivision/import-product-media-ee', |
||
| 118 | 'techdivision/import-product-variant', |
||
| 119 | 'techdivision/import-product-variant-ee' |
||
| 120 | ) |
||
| 121 | ); |
||
| 122 | |||
| 123 | protected $configurationClassName; |
||
| 124 | |||
| 125 | /** |
||
| 126 | * The vendor directory used to load the default configuration files from. |
||
| 127 | * |
||
| 128 | * @var string |
||
| 129 | */ |
||
| 130 | protected $vendorDir; |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Initializes the configuration loader with the configuration factory class name. |
||
| 134 | * |
||
| 135 | * @param string $vendorDir The vendor directory |
||
| 136 | * @param string $configurationFactoryClass The configuration factory class name |
||
| 137 | */ |
||
| 138 | public function __construct($vendorDir, $configurationFactoryClass) |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Return's the vendor directory used to load the default configuration files from. |
||
| 146 | * |
||
| 147 | * @return string The vendor directory |
||
| 148 | */ |
||
| 149 | public function getVendorDir() |
||
| 153 | |||
| 154 | /** |
||
| 155 | * The configuration factory class name. |
||
| 156 | * |
||
| 157 | * @return string The configuration factory class name |
||
| 158 | */ |
||
| 159 | public function getConfigurationFactoryClass() |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Factory implementation to create a new initialized configuration instance. |
||
| 166 | * |
||
| 167 | * If command line options are specified, they will always override the |
||
| 168 | * values found in the configuration file. |
||
| 169 | * |
||
| 170 | * @param \Symfony\Component\Console\Input\InputInterface $input The Symfony console input instance |
||
| 171 | * @param string $entityTypeCode The entity type code to use |
||
| 172 | * |
||
| 173 | * @return \TechDivision\Import\Cli\Configuration The configuration instance |
||
| 174 | * @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 |
||
| 175 | */ |
||
| 176 | public function load(InputInterface $input, $entityTypeCode) |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Query whether or not, the passed directory is a Magento root directory. |
||
| 380 | * |
||
| 381 | * @param string $dir The directory to query |
||
| 382 | * |
||
| 383 | * @return boolean TRUE if the directory is a Magento root directory, else FALSE |
||
| 384 | */ |
||
| 385 | public function isMagentoRootDir($dir) |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Return's the path to the Magento file with the environment configuration. |
||
| 392 | * |
||
| 393 | * @param string $dir The path to the Magento root directory |
||
| 394 | * |
||
| 395 | * @return string The path to the Magento file with the environment configuration |
||
| 396 | */ |
||
| 397 | public function getMagentoEnv($dir) |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Return's the requested Magento DB connction data. |
||
| 404 | * |
||
| 405 | * @param string $dir The path to the Magento root directory |
||
| 406 | * @param string $connectionName The connection name to return the data for |
||
| 407 | * |
||
| 408 | * @return array The connection data |
||
| 409 | * @throws \Exception Is thrown, if the requested DB connection is not available |
||
| 410 | */ |
||
| 411 | public function getMagentoDbConnection($dir, $connectionName = 'default') |
||
| 434 | |||
| 435 | /** |
||
| 436 | * Create's and return's a new database configuration instance, initialized with |
||
| 437 | * the passed values. |
||
| 438 | * |
||
| 439 | * @param string $dsn The DSN to use |
||
| 440 | * @param string $username The username to use |
||
| 441 | * @param string|null $password The passed to use |
||
| 442 | * @param boolean $default TRUE if this should be the default connection |
||
| 443 | * @param string $id The ID to use |
||
| 444 | * |
||
| 445 | * @return \TechDivision\Import\Configuration\Jms\Configuration\Database The database configuration instance |
||
| 446 | */ |
||
| 447 | public function newDatabaseConfiguration($dsn, $username = 'root', $password = null, $default = true, $id = null) |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Create's and return's a new DSN from the passed values. |
||
| 475 | * |
||
| 476 | * @param string $host The host to use |
||
| 477 | * @param string $dbName The database name to use |
||
| 478 | * @param string $charset The charset to use |
||
| 479 | * |
||
| 480 | * @return string The DSN |
||
| 481 | */ |
||
| 482 | public function newDsn($host, $dbName, $charset = 'utf8') |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Return's the default configuration for the passed Magento Edition and the actual entity type. |
||
| 489 | * |
||
| 490 | * @param string $magentoEdition The Magento Edition to return the configuration for |
||
| 491 | * @param string $entityTypeCode The entity type code to use |
||
| 492 | * |
||
| 493 | * @return string The path to the default configuration |
||
| 494 | */ |
||
| 495 | public function getDefaultConfiguration($magentoEdition, $entityTypeCode) |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Return's the Magento Edition specific default libraries. Supported Magento Editions are CE or EE. |
||
| 509 | * |
||
| 510 | * @param string $magentoEdition The Magento Edition to return the libraries for |
||
| 511 | * |
||
| 512 | * @return array The Magento Edition specific default libraries |
||
| 513 | * @throws \Exception Is thrown, if the passed Magento Edition is NOT supported |
||
| 514 | */ |
||
| 515 | public function getDefaultLibraries($magentoEdition) |
||
| 531 | |||
| 532 | /** |
||
| 533 | * Return's the Magento Edition and entity type's specific default library that contains |
||
| 534 | * the configuration file. |
||
| 535 | * |
||
| 536 | * @param string $magentoEdition The Magento Edition to return the default library for |
||
| 537 | * @param string $entityTypeCode The entity type code to return the default library file for |
||
| 538 | * |
||
| 539 | * @return string The name of the library that contains the default configuration file for the passed Magento Edition and entity type code |
||
| 540 | * @throws \Exception Is thrown, if no default configuration for the passed entity type code is available |
||
| 541 | */ |
||
| 542 | public function getDefaultConfigurationLibrary($magentoEdition, $entityTypeCode) |
||
| 569 | |||
| 570 | /** |
||
| 571 | * Return's the entity types specific default import directory. |
||
| 572 | * |
||
| 573 | * @param string $entityTypeCode The entity type code to return the default import directory for |
||
| 574 | * |
||
| 575 | * @return string The default default import directory |
||
| 576 | * @throws \Exception Is thrown, if no default import directory for the passed entity type code is available |
||
| 577 | */ |
||
| 578 | public function getDefaultDirectory($entityTypeCode) |
||
| 594 | } |
||
| 595 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: