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 |
||
| 27 | class Config |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * @var array config data |
||
| 31 | */ |
||
| 32 | private $config = array(); |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var array |
||
| 36 | */ |
||
| 37 | private $partialConfig = array(); |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var ConfigurationLoader |
||
| 41 | */ |
||
| 42 | private $loader; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var array |
||
| 46 | */ |
||
| 47 | private $initConfig; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var boolean |
||
| 51 | */ |
||
| 52 | private $isPharMode; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var OutputInterface |
||
| 56 | */ |
||
| 57 | private $output; |
||
| 58 | |||
| 59 | |||
| 60 | /** |
||
| 61 | * Config constructor. |
||
| 62 | * @param array $initConfig |
||
| 63 | * @param bool $isPharMode |
||
| 64 | * @param OutputInterface $output [optional] |
||
|
|
|||
| 65 | */ |
||
| 66 | public function __construct(array $initConfig = array(), $isPharMode = false, OutputInterface $output = null) |
||
| 72 | |||
| 73 | /** |
||
| 74 | * alias magerun command in input from config |
||
| 75 | * |
||
| 76 | * @param InputInterface $input |
||
| 77 | * @return ArgvInput|InputInterface |
||
| 78 | */ |
||
| 79 | public function checkConfigCommandAlias(InputInterface $input) |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @param Command $command |
||
| 109 | */ |
||
| 110 | public function registerConfigCommandAlias(Command $command) |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @return bool |
||
| 133 | */ |
||
| 134 | private function hasConfigCommandAliases() |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @param Application $application |
||
| 141 | */ |
||
| 142 | public function registerCustomCommands(Application $application) |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Adds autoloader prefixes from user's config |
||
| 171 | * |
||
| 172 | * @param ClassLoader $autoloader |
||
| 173 | */ |
||
| 174 | public function registerCustomAutoloaders(ClassLoader $autoloader) |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @param array $config |
||
| 207 | */ |
||
| 208 | public function setConfig(array $config) |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @return array |
||
| 215 | */ |
||
| 216 | public function getConfig() |
||
| 220 | |||
| 221 | /** |
||
| 222 | * @param ConfigurationLoader $configurationLoader |
||
| 223 | */ |
||
| 224 | public function setConfigurationLoader(ConfigurationLoader $configurationLoader) |
||
| 228 | |||
| 229 | /** |
||
| 230 | * @return ConfigurationLoader |
||
| 231 | */ |
||
| 232 | public function getLoader() |
||
| 241 | |||
| 242 | public function load() |
||
| 246 | |||
| 247 | /** |
||
| 248 | * @param bool $loadExternalConfig |
||
| 249 | */ |
||
| 250 | public function loadPartialConfig($loadExternalConfig) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Get names of sub-folders to be scanned during Magento detection |
||
| 258 | * @return array |
||
| 259 | */ |
||
| 260 | public function getDetectSubFolders() |
||
| 268 | |||
| 269 | /** |
||
| 270 | * @param array $initConfig |
||
| 271 | * @param bool $isPharMode |
||
| 272 | * @param OutputInterface $output |
||
| 273 | * |
||
| 274 | * @return ConfigurationLoader |
||
| 275 | */ |
||
| 276 | public function createLoader(array $initConfig, $isPharMode, OutputInterface $output) |
||
| 284 | } |
||
| 285 |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.