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 | * @return array |
||
| 239 | */ |
||
| 240 | public function getConfig() |
||
| 244 | |||
| 245 | /** |
||
| 246 | * @param ConfigurationLoader $configurationLoader |
||
| 247 | */ |
||
| 248 | public function setLoader(ConfigurationLoader $configurationLoader) |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @return ConfigurationLoader |
||
| 255 | */ |
||
| 256 | public function getLoader() |
||
| 265 | |||
| 266 | public function load() |
||
| 270 | |||
| 271 | /** |
||
| 272 | * @param bool $loadExternalConfig |
||
| 273 | */ |
||
| 274 | public function loadPartialConfig($loadExternalConfig) |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Get names of sub-folders to be scanned during Magento detection |
||
| 282 | * |
||
| 283 | * @return array |
||
| 284 | */ |
||
| 285 | public function getDetectSubFolders() |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @param array $initConfig |
||
| 296 | * @param bool $isPharMode |
||
| 297 | * @param OutputInterface $output |
||
| 298 | * |
||
| 299 | * @return ConfigurationLoader |
||
| 300 | */ |
||
| 301 | public function createLoader(array $initConfig, $isPharMode, OutputInterface $output) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * @param string $message |
||
| 312 | */ |
||
| 313 | private function debugWriteln($message) |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Get array from config, default to an empty array if not set |
||
| 323 | * |
||
| 324 | * @param string|array $key |
||
| 325 | * @param array $default [optional] |
||
| 326 | * @return array |
||
| 327 | */ |
||
| 328 | private function getArray($key, $default = array()) |
||
| 337 | |||
| 338 | private function traverse(array $keys) |
||
| 354 | } |
||
| 355 |
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: