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) |
||
| 67 | { |
||
| 68 | $this->initConfig = $initConfig; |
||
| 69 | $this->isPharMode = (bool) $isPharMode; |
||
| 70 | $this->output = $output ?: new NullOutput(); |
||
| 71 | } |
||
| 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) |
||
| 80 | { |
||
| 81 | if ($this->hasConfigCommandAliases()) { |
||
| 82 | foreach ($this->config['commands']['aliases'] as $alias) { |
||
| 83 | if (is_array($alias)) { |
||
| 84 | $aliasCommandName = key($alias); |
||
| 85 | if ($input->getFirstArgument() == $aliasCommandName) { |
||
| 86 | $aliasCommandParams = array_slice(BinaryString::trimExplodeEmpty(' ', |
||
| 87 | $alias[$aliasCommandName]), 1); |
||
| 88 | if (count($aliasCommandParams) > 0) { |
||
| 89 | // replace with aliased data |
||
| 90 | $mergedParams = array_merge( |
||
| 91 | array_slice($_SERVER['argv'], 0, 2), |
||
| 92 | $aliasCommandParams, |
||
| 93 | array_slice($_SERVER['argv'], 2) |
||
| 94 | ); |
||
| 95 | $input = new ArgvInput($mergedParams); |
||
| 96 | } |
||
| 97 | } |
||
| 98 | } |
||
| 99 | } |
||
| 100 | |||
| 101 | return $input; |
||
| 102 | } |
||
| 103 | |||
| 104 | return $input; |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @param Command $command |
||
| 109 | */ |
||
| 110 | public function registerConfigCommandAlias(Command $command) |
||
| 111 | { |
||
| 112 | if ($this->hasConfigCommandAliases()) { |
||
| 113 | foreach ($this->config['commands']['aliases'] as $alias) { |
||
| 114 | if (!is_array($alias)) { |
||
| 115 | continue; |
||
| 116 | } |
||
| 117 | |||
| 118 | $aliasCommandName = key($alias); |
||
| 119 | $commandString = $alias[$aliasCommandName]; |
||
| 120 | |||
| 121 | list($originalCommand) = explode(' ', $commandString); |
||
| 122 | if ($command->getName() == $originalCommand) { |
||
| 123 | $currentCommandAliases = $command->getAliases(); |
||
| 124 | $currentCommandAliases[] = $aliasCommandName; |
||
| 125 | $command->setAliases($currentCommandAliases); |
||
| 126 | } |
||
| 127 | } |
||
| 128 | } |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @return bool |
||
| 133 | */ |
||
| 134 | private function hasConfigCommandAliases() |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @param Application $application |
||
| 141 | */ |
||
| 142 | public function registerCustomCommands(Application $application) |
||
| 143 | { |
||
| 144 | if (isset($this->config['commands']['customCommands']) |
||
| 145 | && is_array($this->config['commands']['customCommands']) |
||
| 146 | ) { |
||
| 147 | foreach ($this->config['commands']['customCommands'] as $commandClass) { |
||
| 148 | if (is_array($commandClass)) { // Support for key => value (name -> class) |
||
| 149 | $resolvedCommandClass = current($commandClass); |
||
| 150 | /** @var Command $command */ |
||
| 151 | $command = new $resolvedCommandClass(); |
||
| 152 | $command->setName(key($commandClass)); |
||
| 153 | } else { |
||
| 154 | /** @var Command $command */ |
||
| 155 | $command = new $commandClass(); |
||
| 156 | } |
||
| 157 | $application->add($command); |
||
| 158 | |||
| 159 | $output = $this->output; |
||
| 160 | if (OutputInterface::VERBOSITY_DEBUG <= $output->getVerbosity()) { |
||
| 161 | $output->writeln( |
||
| 162 | '<debug>Added command </debug><comment>' . get_class($command) . '</comment>' |
||
| 163 | ); |
||
| 164 | } |
||
| 165 | } |
||
| 166 | } |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Adds autoloader prefixes from user's config |
||
| 171 | * |
||
| 172 | * @param ClassLoader $autoloader |
||
| 173 | */ |
||
| 174 | public function registerCustomAutoloaders(ClassLoader $autoloader) |
||
| 175 | { |
||
| 176 | $output = $this->output; |
||
| 177 | |||
| 178 | View Code Duplication | if (isset($this->config['autoloaders']) && is_array($this->config['autoloaders'])) { |
|
| 179 | foreach ($this->config['autoloaders'] as $prefix => $path) { |
||
| 180 | $autoloader->add($prefix, $path); |
||
| 181 | |||
| 182 | if (OutputInterface::VERBOSITY_DEBUG <= $output->getVerbosity()) { |
||
| 183 | $output->writeln( |
||
| 184 | '<debug>Registrered PSR-2 autoloader </debug> <info>' |
||
| 185 | . $prefix . '</info> -> <comment>' . $path . '</comment>' |
||
| 186 | ); |
||
| 187 | } |
||
| 188 | } |
||
| 189 | } |
||
| 190 | |||
| 191 | View Code Duplication | if (isset($this->config['autoloaders_psr4']) && is_array($this->config['autoloaders_psr4'])) { |
|
| 192 | foreach ($this->config['autoloaders_psr4'] as $prefix => $path) { |
||
| 193 | $autoloader->addPsr4($prefix, $path); |
||
| 194 | |||
| 195 | if (OutputInterface::VERBOSITY_DEBUG <= $output->getVerbosity()) { |
||
| 196 | $output->writeln( |
||
| 197 | '<debug>Registrered PSR-4 autoloader </debug> <info>' |
||
| 198 | . $prefix . ' </info> -> <comment>' . $path . '</comment>' |
||
| 199 | ); |
||
| 200 | } |
||
| 201 | } |
||
| 202 | } |
||
| 203 | } |
||
| 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() |
||
| 233 | { |
||
| 234 | if (!$this->loader) { |
||
| 235 | $this->loader = $this->createLoader($this->initConfig, $this->isPharMode, $this->output); |
||
| 236 | $this->initConfig = null; |
||
| 237 | } |
||
| 238 | |||
| 239 | return $this->loader; |
||
| 240 | } |
||
| 241 | |||
| 242 | public function load() |
||
| 246 | |||
| 247 | /** |
||
| 248 | * @param bool $loadExternalConfig |
||
| 249 | */ |
||
| 250 | public function loadPartialConfig($loadExternalConfig) |
||
| 251 | { |
||
| 252 | $loader = $this->getLoader(); |
||
| 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.