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 |
||
| 24 | class Config implements ConfigInterface, NamespaceAwareInterface |
||
| 25 | { |
||
| 26 | use NamespaceAwareTrait; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * The value that identifies a version order by creation time. |
||
| 30 | */ |
||
| 31 | public const VERSION_ORDER_CREATION_TIME = 'creation'; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * The value that identifies a version order by execution time. |
||
| 35 | */ |
||
| 36 | public const VERSION_ORDER_EXECUTION_TIME = 'execution'; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var array |
||
| 40 | */ |
||
| 41 | protected $values = []; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | protected $configFilePath; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @inheritDoc |
||
| 50 | */ |
||
| 51 | public function __construct(array $configArray, $configFilePath = null) |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Create a new instance of the config class using a Yaml file path. |
||
| 59 | * |
||
| 60 | * @param string $configFilePath Path to the Yaml File |
||
| 61 | * |
||
| 62 | * @throws \RuntimeException |
||
| 63 | * |
||
| 64 | * @return \Phinx\Config\Config |
||
| 65 | */ |
||
| 66 | 448 | public static function fromYaml($configFilePath) |
|
| 86 | 1 | ||
| 87 | /** |
||
| 88 | 1 | * Create a new instance of the config class using a JSON file path. |
|
| 89 | * |
||
| 90 | 1 | * @param string $configFilePath Path to the JSON File |
|
| 91 | * |
||
| 92 | * @throws \RuntimeException |
||
| 93 | * |
||
| 94 | * @return \Phinx\Config\Config |
||
| 95 | */ |
||
| 96 | public static function fromJson($configFilePath) |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Create a new instance of the config class using a PHP file path. |
||
| 117 | * |
||
| 118 | * @param string $configFilePath Path to the PHP File |
||
| 119 | 3 | * |
|
| 120 | * @throws \RuntimeException |
||
| 121 | 3 | * |
|
| 122 | * @return \Phinx\Config\Config |
||
| 123 | 3 | */ |
|
| 124 | public static function fromPhp($configFilePath) |
||
| 142 | |||
| 143 | 25 | /** |
|
| 144 | 24 | * @inheritDoc |
|
| 145 | 24 | */ |
|
| 146 | 24 | public function getEnvironments() |
|
| 161 | |||
| 162 | 25 | /** |
|
| 163 | * @inheritDoc |
||
| 164 | 25 | */ |
|
| 165 | 21 | public function getEnvironment($name) |
|
| 188 | |||
| 189 | /** |
||
| 190 | 20 | * @inheritDoc |
|
| 191 | 20 | */ |
|
| 192 | 2 | public function hasEnvironment($name) |
|
| 196 | 1 | ||
| 197 | 1 | /** |
|
| 198 | * @inheritDoc |
||
| 199 | 1 | */ |
|
| 200 | public function getDefaultEnvironment() |
||
| 242 | |||
| 243 | 423 | /** |
|
| 244 | * @inheritDoc |
||
| 245 | 423 | */ |
|
| 246 | 1 | public function getAlias($alias) |
|
| 250 | 219 | ||
| 251 | 219 | /** |
|
| 252 | * @inheritDoc |
||
| 253 | 422 | */ |
|
| 254 | public function getAliases() |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @inheritDoc |
||
| 261 | */ |
||
| 262 | 14 | public function getConfigFilePath() |
|
| 266 | 14 | ||
| 267 | /** |
||
| 268 | * @inheritDoc |
||
| 269 | * |
||
| 270 | * @throws \UnexpectedValueException |
||
| 271 | */ |
||
| 272 | 48 | public function getMigrationPaths() |
|
| 273 | { |
||
| 274 | 48 | if (!isset($this->values['paths']['migrations'])) { |
|
| 275 | 28 | throw new UnexpectedValueException('Migrations path missing from config file'); |
|
| 276 | } |
||
| 277 | |||
| 278 | 20 | if (is_string($this->values['paths']['migrations'])) { |
|
| 279 | 13 | $this->values['paths']['migrations'] = [$this->values['paths']['migrations']]; |
|
| 280 | 13 | } |
|
| 281 | |||
| 282 | 20 | return $this->values['paths']['migrations']; |
|
| 283 | } |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Gets the base class name for migrations. |
||
| 287 | * |
||
| 288 | * @param bool $dropNamespace Return the base migration class name without the namespace. |
||
| 289 | * |
||
| 290 | 14 | * @return string |
|
| 291 | */ |
||
| 292 | 14 | public function getMigrationBaseClassName($dropNamespace = true) |
|
| 298 | |||
| 299 | /** |
||
| 300 | * @inheritDoc |
||
| 301 | * |
||
| 302 | * @throws \UnexpectedValueException |
||
| 303 | */ |
||
| 304 | 14 | public function getSeedPaths() |
|
| 305 | { |
||
| 306 | 14 | if (!isset($this->values['paths']['seeds'])) { |
|
| 307 | 10 | throw new UnexpectedValueException('Seeds path missing from config file'); |
|
| 308 | } |
||
| 309 | |||
| 310 | 4 | if (is_string($this->values['paths']['seeds'])) { |
|
| 311 | $this->values['paths']['seeds'] = [$this->values['paths']['seeds']]; |
||
| 312 | } |
||
| 313 | |||
| 314 | return $this->values['paths']['seeds']; |
||
| 315 | } |
||
| 316 | |||
| 317 | /** |
||
| 318 | 384 | * Gets the base class name for seeders. |
|
| 319 | * |
||
| 320 | 384 | * @param bool $dropNamespace Return the base seeder class name without the namespace. |
|
| 321 | 162 | * @return string |
|
| 322 | */ |
||
| 323 | public function getSeedBaseClassName($dropNamespace = true) |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Get the template file name. |
||
| 332 | 357 | * |
|
| 333 | * @return string|false |
||
| 334 | 357 | */ |
|
| 335 | public function getTemplateFile() |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Get the template class name. |
||
| 346 | * |
||
| 347 | 448 | * @return string|false |
|
| 348 | */ |
||
| 349 | public function getTemplateClass() |
||
| 357 | |||
| 358 | /** |
||
| 359 | 448 | * {@inheritdoc} |
|
| 360 | 448 | */ |
|
| 361 | public function getDataDomain() |
||
| 369 | |||
| 370 | /** |
||
| 371 | * @inheritDoc |
||
| 372 | */ |
||
| 373 | 448 | public function getContainer() |
|
| 381 | 446 | ||
| 382 | 446 | /** |
|
| 383 | 446 | * Get the version order. |
|
| 384 | 446 | * |
|
| 385 | 446 | * @return string |
|
| 386 | 446 | */ |
|
| 387 | public function getVersionOrder() |
||
| 388 | 43 | { |
|
| 389 | 448 | if (!isset($this->values['version_order'])) { |
|
| 390 | 448 | return self::VERSION_ORDER_CREATION_TIME; |
|
| 391 | } |
||
| 392 | |||
| 393 | return $this->values['version_order']; |
||
| 394 | } |
||
| 395 | |||
| 396 | 213 | /** |
|
| 397 | * Is version order creation time? |
||
| 398 | 213 | * |
|
| 399 | 213 | * @return bool |
|
| 400 | */ |
||
| 401 | public function isVersionOrderCreationTime() |
||
| 402 | { |
||
| 403 | $versionOrder = $this->getVersionOrder(); |
||
| 404 | 2 | ||
| 405 | return $versionOrder == self::VERSION_ORDER_CREATION_TIME; |
||
| 406 | 2 | } |
|
| 407 | 1 | ||
| 408 | /** |
||
| 409 | * Get the bootstrap file path |
||
| 410 | 1 | * |
|
| 411 | * @return string|false |
||
| 412 | */ |
||
| 413 | public function getBootstrapFile() |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Replace tokens in the specified array. |
||
| 424 | 1 | * |
|
| 425 | * @param array $arr Array to replace |
||
| 426 | 1 | * |
|
| 427 | 1 | * @return array |
|
| 428 | */ |
||
| 429 | protected function replaceTokens(array $arr) |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Recurse an array for the specified tokens and replace them. |
||
| 452 | * |
||
| 453 | * @param array $arr Array to recurse |
||
| 454 | * @param string[] $tokens Array of tokens to search for |
||
| 455 | * |
||
| 456 | * @return array |
||
| 457 | */ |
||
| 458 | protected function recurseArrayForTokens($arr, $tokens) |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Parse a database-agnostic DSN into individual options. |
||
| 481 | * |
||
| 482 | * @param array $options Options |
||
| 483 | * |
||
| 484 | * @return array |
||
| 485 | */ |
||
| 486 | protected function parseAgnosticDsn(array $options) |
||
| 497 | |||
| 498 | /** |
||
| 499 | * @inheritDoc |
||
| 500 | */ |
||
| 501 | public function offsetSet($id, $value) |
||
| 505 | |||
| 506 | /** |
||
| 507 | * @inheritDoc |
||
| 508 | * |
||
| 509 | * @throws \InvalidArgumentException |
||
| 510 | */ |
||
| 511 | public function offsetGet($id) |
||
| 519 | |||
| 520 | /** |
||
| 521 | * @inheritDoc |
||
| 522 | */ |
||
| 523 | public function offsetExists($id) |
||
| 527 | |||
| 528 | /** |
||
| 529 | * @inheritDoc |
||
| 530 | */ |
||
| 531 | public function offsetUnset($id) |
||
| 535 | } |
||
| 536 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.