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 Config 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 Config, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | class Config |
||
| 29 | { |
||
| 30 | const PSR_0 = 'PSR-0'; |
||
| 31 | const PSR_4 = 'PSR-4'; |
||
| 32 | |||
| 33 | const COMMAND_CLASS = 'Symfony\Component\Console\Command\Command'; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var array config data |
||
| 37 | */ |
||
| 38 | private $config = array(); |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var array |
||
| 42 | */ |
||
| 43 | private $partialConfig = array(); |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var ConfigurationLoader |
||
| 47 | */ |
||
| 48 | private $loader; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var array |
||
| 52 | */ |
||
| 53 | private $initConfig = array(); |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var boolean |
||
| 57 | */ |
||
| 58 | private $isPharMode; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var OutputInterface |
||
| 62 | */ |
||
| 63 | private $output; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Config constructor. |
||
| 67 | * |
||
| 68 | * @param array $initConfig |
||
| 69 | * @param bool $isPharMode |
||
| 70 | * @param OutputInterface $output [optional] |
||
| 71 | */ |
||
| 72 | public function __construct(array $initConfig = array(), $isPharMode = false, OutputInterface $output = null) |
||
| 78 | |||
| 79 | /** |
||
| 80 | * alias magerun command in input from config |
||
| 81 | * |
||
| 82 | * @param InputInterface $input |
||
| 83 | * @return ArgvInput|InputInterface |
||
| 84 | */ |
||
| 85 | public function checkConfigCommandAlias(InputInterface $input) |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @param Command $command |
||
| 118 | */ |
||
| 119 | public function registerConfigCommandAlias(Command $command) |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @param Application $application |
||
| 139 | */ |
||
| 140 | public function registerCustomCommands(Application $application) |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @param string $className |
||
| 176 | * @param string|null $commandName |
||
| 177 | * @return Command |
||
| 178 | * @throws InvalidArgumentException |
||
| 179 | */ |
||
| 180 | private function newCommand($className, $commandName) |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Adds autoloader prefixes from user's config |
||
| 209 | * |
||
| 210 | * @param ClassLoader $autoloader |
||
| 211 | */ |
||
| 212 | public function registerCustomAutoloaders(ClassLoader $autoloader) |
||
| 228 | |||
| 229 | /** |
||
| 230 | * @param array $config |
||
| 231 | */ |
||
| 232 | public function setConfig(array $config) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Get config array (whole or in part) |
||
| 239 | * |
||
| 240 | * @param string|array $key |
||
| 241 | * @return array |
||
| 242 | */ |
||
| 243 | public function getConfig($key = null) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @param ConfigurationLoader $configurationLoader |
||
| 254 | */ |
||
| 255 | public function setLoader(ConfigurationLoader $configurationLoader) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @return ConfigurationLoader |
||
| 262 | */ |
||
| 263 | public function getLoader() |
||
| 272 | |||
| 273 | public function load() |
||
| 277 | |||
| 278 | /** |
||
| 279 | * @param bool $loadExternalConfig |
||
| 280 | */ |
||
| 281 | public function loadPartialConfig($loadExternalConfig) |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Get names of sub-folders to be scanned during Magento detection |
||
| 289 | * |
||
| 290 | * @return array |
||
| 291 | */ |
||
| 292 | public function getDetectSubFolders() |
||
| 300 | |||
| 301 | /** |
||
| 302 | * @param array $initConfig |
||
| 303 | * @param bool $isPharMode |
||
| 304 | * @param OutputInterface $output |
||
| 305 | * |
||
| 306 | * @return ConfigurationLoader |
||
| 307 | */ |
||
| 308 | public function createLoader(array $initConfig, $isPharMode, OutputInterface $output) |
||
| 316 | |||
| 317 | /** |
||
| 318 | * @param string $message |
||
| 319 | */ |
||
| 320 | private function debugWriteln($message) |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Get array from config, default to an empty array if not set |
||
| 330 | * |
||
| 331 | * @param string|array $key |
||
| 332 | * @param array $default [optional] |
||
| 333 | * @return array |
||
| 334 | */ |
||
| 335 | private function getArray($key, $default = array()) |
||
| 344 | |||
| 345 | private function traverse(array $keys) |
||
| 361 | } |
||
| 362 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: