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 | /** |
||
| 31 | * @var array config data |
||
| 32 | */ |
||
| 33 | private $config = array(); |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var array |
||
| 37 | */ |
||
| 38 | private $partialConfig = array(); |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var ConfigurationLoader |
||
| 42 | */ |
||
| 43 | private $loader; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var array |
||
| 47 | */ |
||
| 48 | private $initConfig; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var boolean |
||
| 52 | */ |
||
| 53 | private $isPharMode; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var OutputInterface |
||
| 57 | */ |
||
| 58 | private $output; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Config constructor. |
||
| 62 | * |
||
| 63 | * @param array $initConfig |
||
| 64 | * @param bool $isPharMode |
||
| 65 | * @param OutputInterface $output [optional] |
||
|
|
|||
| 66 | */ |
||
| 67 | public function __construct(array $initConfig = array(), $isPharMode = false, OutputInterface $output = null) |
||
| 68 | { |
||
| 69 | $this->initConfig = $initConfig; |
||
| 70 | $this->isPharMode = (bool) $isPharMode; |
||
| 71 | $this->output = $output ?: new NullOutput(); |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * alias magerun command in input from config |
||
| 76 | * |
||
| 77 | * @param InputInterface $input |
||
| 78 | * @return ArgvInput|InputInterface |
||
| 79 | */ |
||
| 80 | public function checkConfigCommandAlias(InputInterface $input) |
||
| 81 | { |
||
| 82 | foreach ($this->getArray(array('commands', 'aliases')) as $alias) { |
||
| 83 | if (!is_array($alias)) { |
||
| 84 | continue; |
||
| 85 | } |
||
| 86 | $aliasCommandName = key($alias); |
||
| 87 | if ($input->getFirstArgument() !== $aliasCommandName) { |
||
| 88 | continue; |
||
| 89 | } |
||
| 90 | $aliasCommandParams = array_slice( |
||
| 91 | BinaryString::trimExplodeEmpty(' ', $alias[$aliasCommandName]), |
||
| 92 | 1 |
||
| 93 | ); |
||
| 94 | if (count($aliasCommandParams) > 0) { |
||
| 95 | // replace with aliased data |
||
| 96 | $mergedParams = array_merge( |
||
| 97 | array_slice($_SERVER['argv'], 0, 2), |
||
| 98 | $aliasCommandParams, |
||
| 99 | array_slice($_SERVER['argv'], 2) |
||
| 100 | ); |
||
| 101 | $input = new ArgvInput($mergedParams); |
||
| 102 | } |
||
| 103 | } |
||
| 104 | |||
| 105 | return $input; |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @param Command $command |
||
| 110 | */ |
||
| 111 | public function registerConfigCommandAlias(Command $command) |
||
| 112 | { |
||
| 113 | foreach ($this->getArray(array('commands', 'aliases')) as $alias) { |
||
| 114 | if (!is_array($alias)) { |
||
| 115 | continue; |
||
| 116 | } |
||
| 117 | |||
| 118 | $aliasCommandName = key($alias); |
||
| 119 | $commandString = $alias[$aliasCommandName]; |
||
| 120 | list($originalCommand) = explode(' ', $commandString, 2); |
||
| 121 | if ($command->getName() !== $originalCommand) { |
||
| 122 | continue; |
||
| 123 | } |
||
| 124 | |||
| 125 | $command->setAliases(array_merge($command->getAliases(), array($aliasCommandName))); |
||
| 126 | } |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @param Application $application |
||
| 131 | */ |
||
| 132 | public function registerCustomCommands(Application $application) |
||
| 133 | { |
||
| 134 | foreach ($this->getArray(array('commands', 'customCommands')) as $commandClass) { |
||
| 135 | $commandName = null; |
||
| 136 | if (is_array($commandClass)) { |
||
| 137 | // Support for key => value (name -> class) |
||
| 138 | $commandName = key($commandClass); |
||
| 139 | $commandClass = current($commandClass); |
||
| 140 | } |
||
| 141 | $command = $this->newCommand($commandClass, $commandName); |
||
| 142 | $this->debugWriteln( |
||
| 143 | sprintf( |
||
| 144 | '<debug>Add command </debug> <info>%s</info> -> <comment>%s</comment>', |
||
| 145 | $command->getName(), |
||
| 146 | get_class($command) |
||
| 147 | ) |
||
| 148 | ); |
||
| 149 | $application->add($command); |
||
| 150 | } |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @param string $className |
||
| 155 | * @param string|null $commandName |
||
| 156 | * @return Command |
||
| 157 | * @throws InvalidArgumentException |
||
| 158 | */ |
||
| 159 | private function newCommand($className, $commandName) |
||
| 160 | { |
||
| 161 | /** @var Command $command */ |
||
| 162 | if (!(is_string($className) || is_object($className))) { |
||
| 163 | throw new InvalidArgumentException( |
||
| 164 | sprintf('Command classname must be string, %s given', gettype($className)) |
||
| 165 | ); |
||
| 166 | } |
||
| 167 | |||
| 168 | $command = new $className(); |
||
| 169 | if (null !== $commandName) { |
||
| 170 | $command->setName($commandName); |
||
| 171 | } |
||
| 172 | |||
| 173 | return $command; |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Adds autoloader prefixes from user's config |
||
| 178 | * |
||
| 179 | * @param ClassLoader $autoloader |
||
| 180 | */ |
||
| 181 | public function registerCustomAutoloaders(ClassLoader $autoloader) |
||
| 182 | { |
||
| 183 | $mask = '<debug>Registered %s autoloader </debug> <info>%s</info> -> <comment>%s</comment>'; |
||
| 184 | |||
| 185 | foreach ($this->getArray('autoloaders') as $prefix => $path) { |
||
| 186 | $autoloader->add($prefix, $path); |
||
| 187 | $this->debugWriteln(sprintf($mask, 'PSR-2', $prefix, $path)); |
||
| 188 | } |
||
| 189 | |||
| 190 | foreach ($this->getArray('autoloaders_psr4') as $prefix => $path) { |
||
| 191 | $autoloader->addPsr4($prefix, $path); |
||
| 192 | $this->debugWriteln(sprintf($mask, 'PSR-4', OutputFormatter::escape($prefix), $path)); |
||
| 193 | } |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @param array $config |
||
| 198 | */ |
||
| 199 | public function setConfig(array $config) |
||
| 200 | { |
||
| 201 | $this->config = $config; |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @return array |
||
| 206 | */ |
||
| 207 | public function getConfig() |
||
| 208 | { |
||
| 209 | return $this->config; |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @param ConfigurationLoader $configurationLoader |
||
| 214 | */ |
||
| 215 | public function setConfigurationLoader(ConfigurationLoader $configurationLoader) |
||
| 216 | { |
||
| 217 | trigger_error(__METHOD__ . ' use setLoader() instead', E_USER_DEPRECATED); |
||
| 218 | |||
| 219 | $this->setLoader($configurationLoader); |
||
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * @param ConfigurationLoader $configurationLoader |
||
| 224 | */ |
||
| 225 | public function setLoader(ConfigurationLoader $configurationLoader) |
||
| 226 | { |
||
| 227 | $this->loader = $configurationLoader; |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @return ConfigurationLoader |
||
| 232 | */ |
||
| 233 | public function getLoader() |
||
| 234 | { |
||
| 235 | if (!$this->loader) { |
||
| 236 | $this->loader = $this->createLoader($this->initConfig, $this->isPharMode, $this->output); |
||
| 237 | $this->initConfig = null; |
||
| 238 | } |
||
| 239 | |||
| 240 | return $this->loader; |
||
| 241 | } |
||
| 242 | |||
| 243 | public function load() |
||
| 247 | |||
| 248 | /** |
||
| 249 | * @param bool $loadExternalConfig |
||
| 250 | */ |
||
| 251 | public function loadPartialConfig($loadExternalConfig) |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Get names of sub-folders to be scanned during Magento detection |
||
| 259 | * |
||
| 260 | * @return array |
||
| 261 | */ |
||
| 262 | public function getDetectSubFolders() |
||
| 263 | { |
||
| 264 | if (isset($this->partialConfig['detect']['subFolders'])) { |
||
| 265 | return $this->partialConfig['detect']['subFolders']; |
||
| 266 | } |
||
| 267 | |||
| 270 | |||
| 271 | /** |
||
| 272 | * @param array $initConfig |
||
| 273 | * @param bool $isPharMode |
||
| 274 | * @param OutputInterface $output |
||
| 275 | * |
||
| 276 | * @return ConfigurationLoader |
||
| 277 | */ |
||
| 278 | public function createLoader(array $initConfig, $isPharMode, OutputInterface $output) |
||
| 286 | |||
| 287 | /** |
||
| 288 | * @param string $message |
||
| 289 | */ |
||
| 290 | private function debugWriteln($message) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Get array from config, default to an empty array if not set |
||
| 300 | * |
||
| 301 | * @param string|array $key |
||
| 302 | * @param array $default [optional] |
||
| 303 | * @return array |
||
| 304 | */ |
||
| 305 | private function getArray($key, $default = array()) |
||
| 314 | |||
| 315 | private function traverse(array $keys) |
||
| 331 | } |
||
| 332 |
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.