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 |
||
| 22 | class Config implements ConfigInterface, NamespaceAwareInterface |
||
| 23 | { |
||
| 24 | use NamespaceAwareTrait; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * The value that identifies a version order by creation time. |
||
| 28 | */ |
||
| 29 | public const VERSION_ORDER_CREATION_TIME = 'creation'; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * The value that identifies a version order by execution time. |
||
| 33 | */ |
||
| 34 | public const VERSION_ORDER_EXECUTION_TIME = 'execution'; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | protected $values = []; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | protected $configFilePath; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @inheritDoc |
||
| 48 | */ |
||
| 49 | public function __construct(array $configArray, $configFilePath = null) |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Create a new instance of the config class using a Yaml file path. |
||
| 57 | * |
||
| 58 | * @param string $configFilePath Path to the Yaml File |
||
| 59 | * |
||
| 60 | * @throws \RuntimeException |
||
| 61 | * |
||
| 62 | * @return \Phinx\Config\Config |
||
| 63 | */ |
||
| 64 | public static function fromYaml($configFilePath) |
||
| 65 | { |
||
| 66 | 448 | if (!class_exists('Symfony\\Component\\Yaml\\Yaml', true)) { |
|
| 67 | throw new RuntimeException('Missing yaml parser, symfony/yaml package is not installed.'); |
||
| 68 | 448 | } |
|
| 69 | 448 | ||
| 70 | 448 | $configFile = file_get_contents($configFilePath); |
|
| 71 | $configArray = Yaml::parse($configFile); |
||
| 72 | |||
| 73 | if (!is_array($configArray)) { |
||
| 74 | throw new RuntimeException(sprintf( |
||
| 75 | 'File \'%s\' must be valid YAML', |
||
| 76 | $configFilePath |
||
| 77 | )); |
||
| 78 | } |
||
| 79 | 2 | ||
| 80 | return new static($configArray, $configFilePath); |
||
| 81 | 2 | } |
|
| 82 | 2 | ||
| 83 | /** |
||
| 84 | 2 | * Create a new instance of the config class using a JSON file path. |
|
| 85 | 1 | * |
|
| 86 | 1 | * @param string $configFilePath Path to the JSON File |
|
| 87 | * |
||
| 88 | 1 | * @throws \RuntimeException |
|
| 89 | * |
||
| 90 | 1 | * @return \Phinx\Config\Config |
|
| 91 | */ |
||
| 92 | public static function fromJson($configFilePath) |
||
| 93 | { |
||
| 94 | if (!function_exists('json_decode')) { |
||
| 95 | throw new RuntimeException("Need to install JSON PHP extension to use JSON config"); |
||
| 96 | } |
||
| 97 | |||
| 98 | $configArray = json_decode(file_get_contents($configFilePath), true); |
||
| 99 | if (!is_array($configArray)) { |
||
| 100 | 2 | throw new RuntimeException(sprintf( |
|
| 101 | 'File \'%s\' must be valid JSON', |
||
| 102 | 2 | $configFilePath |
|
| 103 | 2 | )); |
|
| 104 | 1 | } |
|
| 105 | 1 | ||
| 106 | return new static($configArray, $configFilePath); |
||
| 107 | 1 | } |
|
| 108 | |||
| 109 | 1 | /** |
|
| 110 | * Create a new instance of the config class using a PHP file path. |
||
| 111 | * |
||
| 112 | * @param string $configFilePath Path to the PHP File |
||
| 113 | * |
||
| 114 | * @throws \RuntimeException |
||
| 115 | * |
||
| 116 | * @return \Phinx\Config\Config |
||
| 117 | */ |
||
| 118 | public static function fromPhp($configFilePath) |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @inheritDoc |
||
| 139 | */ |
||
| 140 | public function getEnvironments() |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @inheritDoc |
||
| 158 | */ |
||
| 159 | public function getEnvironment($name) |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @inheritDoc |
||
| 177 | */ |
||
| 178 | public function hasEnvironment($name) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @inheritDoc |
||
| 185 | */ |
||
| 186 | public function getDefaultEnvironment() |
||
| 228 | |||
| 229 | 7 | /** |
|
| 230 | * @inheritDoc |
||
| 231 | */ |
||
| 232 | public function getAlias($alias) |
||
| 236 | |||
| 237 | 448 | /** |
|
| 238 | * @inheritDoc |
||
| 239 | */ |
||
| 240 | public function getAliases() |
||
| 244 | |||
| 245 | 423 | /** |
|
| 246 | 1 | * @inheritDoc |
|
| 247 | */ |
||
| 248 | public function getConfigFilePath() |
||
| 252 | |||
| 253 | 422 | /** |
|
| 254 | * @inheritDoc |
||
| 255 | * |
||
| 256 | * @throws \UnexpectedValueException |
||
| 257 | */ |
||
| 258 | public function getMigrationPaths() |
||
| 259 | { |
||
| 260 | if (!isset($this->values['paths']['migrations'])) { |
||
| 261 | throw new UnexpectedValueException('Migrations path missing from config file'); |
||
| 262 | 14 | } |
|
| 263 | |||
| 264 | 14 | if (is_string($this->values['paths']['migrations'])) { |
|
| 265 | $this->values['paths']['migrations'] = [$this->values['paths']['migrations']]; |
||
| 266 | 14 | } |
|
| 267 | |||
| 268 | return $this->values['paths']['migrations']; |
||
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | 48 | * Gets the base class name for migrations. |
|
| 273 | * |
||
| 274 | 48 | * @param bool $dropNamespace Return the base migration class name without the namespace. |
|
| 275 | 28 | * |
|
| 276 | * @return string |
||
| 277 | */ |
||
| 278 | 20 | public function getMigrationBaseClassName($dropNamespace = true) |
|
| 284 | |||
| 285 | /** |
||
| 286 | * @inheritDoc |
||
| 287 | * |
||
| 288 | * @throws \UnexpectedValueException |
||
| 289 | */ |
||
| 290 | 14 | public function getSeedPaths() |
|
| 291 | { |
||
| 292 | 14 | if (!isset($this->values['paths']['seeds'])) { |
|
| 293 | 13 | throw new UnexpectedValueException('Seeds path missing from config file'); |
|
| 294 | } |
||
| 295 | |||
| 296 | 1 | if (is_string($this->values['paths']['seeds'])) { |
|
| 297 | $this->values['paths']['seeds'] = [$this->values['paths']['seeds']]; |
||
| 298 | } |
||
| 299 | |||
| 300 | return $this->values['paths']['seeds']; |
||
| 301 | } |
||
| 302 | |||
| 303 | /** |
||
| 304 | 14 | * Gets the base class name for seeders. |
|
| 305 | * |
||
| 306 | 14 | * @param bool $dropNamespace Return the base seeder class name without the namespace. |
|
| 307 | 10 | * @return string |
|
| 308 | */ |
||
| 309 | public function getSeedBaseClassName($dropNamespace = true) |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Get the template file name. |
||
| 318 | 384 | * |
|
| 319 | * @return string|false |
||
| 320 | 384 | */ |
|
| 321 | 162 | public function getTemplateFile() |
|
| 329 | |||
| 330 | /** |
||
| 331 | * Get the template class name. |
||
| 332 | 357 | * |
|
| 333 | * @return string|false |
||
| 334 | 357 | */ |
|
| 335 | public function getTemplateClass() |
||
| 343 | |||
| 344 | /** |
||
| 345 | * {@inheritdoc} |
||
| 346 | */ |
||
| 347 | 448 | public function getDataDomain() |
|
| 355 | 2 | ||
| 356 | 448 | /** |
|
| 357 | * @inheritDoc |
||
| 358 | */ |
||
| 359 | 448 | public function getContainer() |
|
| 367 | |||
| 368 | /** |
||
| 369 | * Get the version order. |
||
| 370 | * |
||
| 371 | * @return string |
||
| 372 | */ |
||
| 373 | 448 | public function getVersionOrder() |
|
| 374 | { |
||
| 375 | 448 | if (!isset($this->values['version_order'])) { |
|
| 376 | 448 | return self::VERSION_ORDER_CREATION_TIME; |
|
| 377 | 447 | } |
|
| 378 | 446 | ||
| 379 | 446 | return $this->values['version_order']; |
|
| 380 | } |
||
| 381 | 446 | ||
| 382 | 446 | /** |
|
| 383 | 446 | * Is version order creation time? |
|
| 384 | 446 | * |
|
| 385 | 446 | * @return bool |
|
| 386 | 446 | */ |
|
| 387 | public function isVersionOrderCreationTime() |
||
| 388 | 43 | { |
|
| 389 | 448 | $versionOrder = $this->getVersionOrder(); |
|
| 390 | 448 | ||
| 391 | return $versionOrder == self::VERSION_ORDER_CREATION_TIME; |
||
| 392 | } |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Get the bootstrap file path |
||
| 396 | 213 | * |
|
| 397 | * @return string|false |
||
| 398 | 213 | */ |
|
| 399 | 213 | public function getBootstrapFile() |
|
| 407 | 1 | ||
| 408 | /** |
||
| 409 | * Replace tokens in the specified array. |
||
| 410 | 1 | * |
|
| 411 | * @param array $arr Array to replace |
||
| 412 | * |
||
| 413 | * @return array |
||
| 414 | */ |
||
| 415 | protected function replaceTokens(array $arr) |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Recurse an array for the specified tokens and replace them. |
||
| 438 | * |
||
| 439 | * @param array $arr Array to recurse |
||
| 440 | * @param string[] $tokens Array of tokens to search for |
||
| 441 | * |
||
| 442 | * @return array |
||
| 443 | */ |
||
| 444 | protected function recurseArrayForTokens($arr, $tokens) |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Parse a database-agnostic DSN into individual options. |
||
| 467 | * |
||
| 468 | * @param array $options Options |
||
| 469 | * |
||
| 470 | * @return array |
||
| 471 | */ |
||
| 472 | protected function parseAgnosticDsn(array $options) |
||
| 491 | |||
| 492 | /** |
||
| 493 | * @inheritDoc |
||
| 494 | */ |
||
| 495 | public function offsetSet($id, $value) |
||
| 499 | |||
| 500 | /** |
||
| 501 | * @inheritDoc |
||
| 502 | * |
||
| 503 | * @throws \InvalidArgumentException |
||
| 504 | */ |
||
| 505 | public function offsetGet($id) |
||
| 513 | |||
| 514 | /** |
||
| 515 | * @inheritDoc |
||
| 516 | */ |
||
| 517 | public function offsetExists($id) |
||
| 521 | |||
| 522 | /** |
||
| 523 | * @inheritDoc |
||
| 524 | */ |
||
| 525 | public function offsetUnset($id) |
||
| 529 | } |
||
| 530 |