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 |
||
| 81 | class Config |
||
| 82 | { |
||
| 83 | const PULI_DIR = 'puli-dir'; |
||
| 84 | |||
| 85 | const BOOTSTRAP_FILE = 'bootstrap-file'; |
||
| 86 | |||
| 87 | const FACTORY = 'factory'; |
||
| 88 | |||
| 89 | const FACTORY_AUTO_GENERATE = 'factory.auto-generate'; |
||
| 90 | |||
| 91 | const FACTORY_IN = 'factory.in'; |
||
| 92 | |||
| 93 | const FACTORY_IN_CLASS = 'factory.in.class'; |
||
| 94 | |||
| 95 | const FACTORY_IN_FILE = 'factory.in.file'; |
||
| 96 | |||
| 97 | const FACTORY_OUT = 'factory.out'; |
||
| 98 | |||
| 99 | const FACTORY_OUT_CLASS = 'factory.out.class'; |
||
| 100 | |||
| 101 | const FACTORY_OUT_FILE = 'factory.out.file'; |
||
| 102 | |||
| 103 | const REPOSITORY = 'repository'; |
||
| 104 | |||
| 105 | const REPOSITORY_TYPE = 'repository.type'; |
||
| 106 | |||
| 107 | const REPOSITORY_PATH = 'repository.path'; |
||
| 108 | |||
| 109 | const REPOSITORY_SYMLINK = 'repository.symlink'; |
||
| 110 | |||
| 111 | const REPOSITORY_OPTIMIZE = 'repository.optimize'; |
||
| 112 | |||
| 113 | const REPOSITORY_STORE = 'repository.store'; |
||
| 114 | |||
| 115 | const REPOSITORY_STORE_TYPE = 'repository.store.type'; |
||
| 116 | |||
| 117 | const REPOSITORY_STORE_PATH = 'repository.store.path'; |
||
| 118 | |||
| 119 | const REPOSITORY_STORE_HOST = 'repository.store.host'; |
||
| 120 | |||
| 121 | const REPOSITORY_STORE_PORT = 'repository.store.port'; |
||
| 122 | |||
| 123 | const REPOSITORY_STORE_BUCKET = 'repository.store.bucket'; |
||
| 124 | |||
| 125 | const REPOSITORY_STORE_CACHE = 'repository.store.cache'; |
||
| 126 | |||
| 127 | const CHANGE_STREAM = 'change-stream'; |
||
| 128 | |||
| 129 | const CHANGE_STREAM_TYPE = 'change-stream.type'; |
||
| 130 | |||
| 131 | const CHANGE_STREAM_PATH = 'change-stream.path'; |
||
| 132 | |||
| 133 | const CHANGE_STREAM_STORE = 'change-stream.store'; |
||
| 134 | |||
| 135 | const CHANGE_STREAM_STORE_TYPE = 'change-stream.store.type'; |
||
| 136 | |||
| 137 | const CHANGE_STREAM_STORE_PATH = 'change-stream.store.path'; |
||
| 138 | |||
| 139 | const CHANGE_STREAM_STORE_HOST = 'change-stream.store.host'; |
||
| 140 | |||
| 141 | const CHANGE_STREAM_STORE_PORT = 'change-stream.store.port'; |
||
| 142 | |||
| 143 | const CHANGE_STREAM_STORE_BUCKET = 'change-stream.store.bucket'; |
||
| 144 | |||
| 145 | const CHANGE_STREAM_STORE_CACHE = 'change-stream.store.cache'; |
||
| 146 | |||
| 147 | const DISCOVERY = 'discovery'; |
||
| 148 | |||
| 149 | const DISCOVERY_TYPE = 'discovery.type'; |
||
| 150 | |||
| 151 | const DISCOVERY_PATH = 'discovery.path'; |
||
| 152 | |||
| 153 | const DISCOVERY_STORE = 'discovery.store'; |
||
| 154 | |||
| 155 | const DISCOVERY_STORE_TYPE = 'discovery.store.type'; |
||
| 156 | |||
| 157 | const DISCOVERY_STORE_PATH = 'discovery.store.path'; |
||
| 158 | |||
| 159 | const DISCOVERY_STORE_HOST = 'discovery.store.host'; |
||
| 160 | |||
| 161 | const DISCOVERY_STORE_PORT = 'discovery.store.port'; |
||
| 162 | |||
| 163 | const DISCOVERY_STORE_BUCKET = 'discovery.store.bucket'; |
||
| 164 | |||
| 165 | const DISCOVERY_STORE_CACHE = 'discovery.store.cache'; |
||
| 166 | |||
| 167 | const CACHE_FILE = 'cache-file'; |
||
| 168 | |||
| 169 | /** |
||
| 170 | * The accepted config keys. |
||
| 171 | * |
||
| 172 | * @var bool[] |
||
| 173 | */ |
||
| 174 | private static $keys = array( |
||
| 175 | self::PULI_DIR => true, |
||
| 176 | self::BOOTSTRAP_FILE => true, |
||
| 177 | self::FACTORY_AUTO_GENERATE => true, |
||
| 178 | self::FACTORY_IN_CLASS => true, |
||
| 179 | self::FACTORY_IN_FILE => true, |
||
| 180 | self::FACTORY_OUT_CLASS => true, |
||
| 181 | self::FACTORY_OUT_FILE => true, |
||
| 182 | self::REPOSITORY_TYPE => true, |
||
| 183 | self::REPOSITORY_PATH => true, |
||
| 184 | self::REPOSITORY_SYMLINK => true, |
||
| 185 | self::REPOSITORY_OPTIMIZE => true, |
||
| 186 | self::REPOSITORY_STORE_TYPE => true, |
||
| 187 | self::REPOSITORY_STORE_PATH => true, |
||
| 188 | self::REPOSITORY_STORE_HOST => true, |
||
| 189 | self::REPOSITORY_STORE_PORT => true, |
||
| 190 | self::REPOSITORY_STORE_BUCKET => true, |
||
| 191 | self::REPOSITORY_STORE_CACHE => true, |
||
| 192 | self::CHANGE_STREAM_TYPE => true, |
||
| 193 | self::CHANGE_STREAM_PATH => true, |
||
| 194 | self::CHANGE_STREAM_STORE_TYPE => true, |
||
| 195 | self::CHANGE_STREAM_STORE_PATH => true, |
||
| 196 | self::CHANGE_STREAM_STORE_HOST => true, |
||
| 197 | self::CHANGE_STREAM_STORE_PORT => true, |
||
| 198 | self::CHANGE_STREAM_STORE_BUCKET => true, |
||
| 199 | self::CHANGE_STREAM_STORE_CACHE => true, |
||
| 200 | self::DISCOVERY_TYPE => true, |
||
| 201 | self::DISCOVERY_PATH => true, |
||
| 202 | self::DISCOVERY_STORE_TYPE => true, |
||
| 203 | self::DISCOVERY_STORE_PATH => true, |
||
| 204 | self::DISCOVERY_STORE_HOST => true, |
||
| 205 | self::DISCOVERY_STORE_PORT => true, |
||
| 206 | self::DISCOVERY_STORE_BUCKET => true, |
||
| 207 | self::DISCOVERY_STORE_CACHE => true, |
||
| 208 | self::CACHE_FILE => true, |
||
| 209 | ); |
||
| 210 | |||
| 211 | private static $compositeKeys = array( |
||
| 212 | self::FACTORY => true, |
||
| 213 | self::FACTORY_IN => true, |
||
| 214 | self::FACTORY_OUT => true, |
||
| 215 | self::REPOSITORY => true, |
||
| 216 | self::REPOSITORY_STORE => true, |
||
| 217 | self::CHANGE_STREAM => true, |
||
| 218 | self::CHANGE_STREAM_STORE => true, |
||
| 219 | self::DISCOVERY => true, |
||
| 220 | self::DISCOVERY_STORE => true, |
||
| 221 | ); |
||
| 222 | |||
| 223 | /** |
||
| 224 | * The configuration values. |
||
| 225 | * |
||
| 226 | * @var array |
||
| 227 | */ |
||
| 228 | private $values = array(); |
||
| 229 | |||
| 230 | /** |
||
| 231 | * The configuration to fall back to. |
||
| 232 | * |
||
| 233 | * @var Config |
||
| 234 | */ |
||
| 235 | private $baseConfig; |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Returns all valid configuration keys. |
||
| 239 | * |
||
| 240 | * @return string[] The config keys. |
||
|
|
|||
| 241 | */ |
||
| 242 | 20 | public static function getKeys() |
|
| 246 | |||
| 247 | /** |
||
| 248 | * Creates a new configuration. |
||
| 249 | * |
||
| 250 | * @param Config|null $baseConfig The configuration to fall back to if a value is |
||
| 251 | * not set in here. |
||
| 252 | * @param array $values The values to initially set in the configuration. |
||
| 253 | */ |
||
| 254 | 726 | public function __construct(Config $baseConfig = null, array $values = array()) |
|
| 260 | |||
| 261 | /** |
||
| 262 | * Returns the base configuration. |
||
| 263 | * |
||
| 264 | * @return Config The base configuration or `null` if none is set. |
||
| 265 | */ |
||
| 266 | public function getBaseConfig() |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Returns the value of a configuration key. |
||
| 273 | * |
||
| 274 | * If fallback is enabled, the value of the base configuration is returned |
||
| 275 | * if the key was not set. |
||
| 276 | * |
||
| 277 | * You can also pass a default value in the second parameter. This default |
||
| 278 | * value is returned if the configuration key was neither found in this nor |
||
| 279 | * in its fallback configuration. |
||
| 280 | * |
||
| 281 | * @param string $key The configuration key. |
||
| 282 | * @param mixed $default The value to return if the key was not set. |
||
| 283 | * @param bool $fallback Whether to return the value of the base |
||
| 284 | * configuration if the key was not set. |
||
| 285 | * |
||
| 286 | * @return mixed The value of the configuration key. |
||
| 287 | * |
||
| 288 | * @throws NoSuchConfigKeyException If the configuration key is invalid. |
||
| 289 | */ |
||
| 290 | 134 | public function get($key, $default = null, $fallback = true) |
|
| 294 | |||
| 295 | /** |
||
| 296 | * Returns the raw value of a configuration key. |
||
| 297 | * |
||
| 298 | * Unlike {@link get()}, this method does not resolve placeholders: |
||
| 299 | * |
||
| 300 | * ```php |
||
| 301 | * $config = new Config(); |
||
| 302 | * $config->set(Config::PULI_DIR, '.puli'); |
||
| 303 | * $config->set(Config::INSTALL_FILE, '{$puli-dir}/install-file.json'); |
||
| 304 | * |
||
| 305 | * echo $config->get(Config::PULI_DIR); |
||
| 306 | * // => .puli/install-file.json |
||
| 307 | * |
||
| 308 | * echo $config->getRaw(Config::PULI_DIR); |
||
| 309 | * // => {$puli-dir}/install-file.json |
||
| 310 | * ``` |
||
| 311 | * |
||
| 312 | * @param string $key The configuration key. |
||
| 313 | * @param mixed $default The value to return if the key was not set. |
||
| 314 | * @param bool $fallback Whether to return the value of the base |
||
| 315 | * configuration if the key was not set. |
||
| 316 | * |
||
| 317 | * @return mixed The value of the configuration key. |
||
| 318 | * |
||
| 319 | * @throws NoSuchConfigKeyException If the configuration key is invalid. |
||
| 320 | */ |
||
| 321 | 160 | public function getRaw($key, $default = null, $fallback = true) |
|
| 341 | |||
| 342 | /** |
||
| 343 | * Returns whether a configuration key is set. |
||
| 344 | * |
||
| 345 | * @param string $key The configuration key to search. |
||
| 346 | * @param bool $fallback Whether to check the base configuration if the |
||
| 347 | * key is not found. |
||
| 348 | * |
||
| 349 | * @return bool Returns `true` if the configuration key is set. |
||
| 350 | * |
||
| 351 | * @throws NoSuchConfigKeyException If the configuration key is invalid. |
||
| 352 | */ |
||
| 353 | 22 | public function contains($key, $fallback = true) |
|
| 373 | |||
| 374 | /** |
||
| 375 | * Sets the value of a configuration key. |
||
| 376 | * |
||
| 377 | * @param string $key The configuration key. |
||
| 378 | * @param mixed $value The value to set. |
||
| 379 | * |
||
| 380 | * @throws NoSuchConfigKeyException If the configuration key is invalid. |
||
| 381 | * @throws InvalidConfigException If the value is invalid. |
||
| 382 | */ |
||
| 383 | 448 | public function set($key, $value) |
|
| 404 | |||
| 405 | /** |
||
| 406 | * Sets a list of configuration values. |
||
| 407 | * |
||
| 408 | * @param array $values The values to set. |
||
| 409 | * |
||
| 410 | * @throws NoSuchConfigKeyException If a configuration key is invalid. |
||
| 411 | * @throws InvalidConfigException If a value is invalid. |
||
| 412 | */ |
||
| 413 | 726 | public function merge(array $values) |
|
| 419 | |||
| 420 | /** |
||
| 421 | * Replaces the configuration with a list of configuration values. |
||
| 422 | * |
||
| 423 | * @param array $values The values to set. |
||
| 424 | * |
||
| 425 | * @throws NoSuchConfigKeyException If a configuration key is invalid. |
||
| 426 | * @throws InvalidConfigException If a value is invalid. |
||
| 427 | */ |
||
| 428 | 4 | public function replace(array $values) |
|
| 433 | |||
| 434 | /** |
||
| 435 | * Removes a configuration key. |
||
| 436 | * |
||
| 437 | * If the configuration has a base configuration, the default value will |
||
| 438 | * be returned by {@link get()} after removing the key. |
||
| 439 | * |
||
| 440 | * @param string $key The configuration key to remove. |
||
| 441 | * |
||
| 442 | * @throws NoSuchConfigKeyException If the configuration key is invalid. |
||
| 443 | */ |
||
| 444 | 13 | public function remove($key) |
|
| 458 | |||
| 459 | /** |
||
| 460 | * Removes all configuration keys. |
||
| 461 | * |
||
| 462 | * If the configuration has a base configuration, the default values will |
||
| 463 | * be returned by {@link get()} after removing the keys. |
||
| 464 | */ |
||
| 465 | 5 | public function clear() |
|
| 469 | |||
| 470 | /** |
||
| 471 | * Returns all configuration values as flat array. |
||
| 472 | * |
||
| 473 | * @param bool $includeFallback Whether to include values set in the base |
||
| 474 | * configuration passed to {@link __construct()}. |
||
| 475 | * |
||
| 476 | * @return array The configuration values. |
||
| 477 | */ |
||
| 478 | 5 | public function toFlatArray($includeFallback = true) |
|
| 482 | |||
| 483 | /** |
||
| 484 | * Returns all raw configuration values as flat array. |
||
| 485 | * |
||
| 486 | * Unlike {@link toFlatArray()}, this method does not resolve placeholders: |
||
| 487 | * |
||
| 488 | * ```php |
||
| 489 | * $config = new Config(); |
||
| 490 | * $config->set(Config::PULI_DIR, '.puli'); |
||
| 491 | * $config->set(Config::REGISTRY_FILE, '{$puli-dir}/ServiceRegistry.php'); |
||
| 492 | * |
||
| 493 | * print_r($config->toFlatArray()); |
||
| 494 | * // Array( |
||
| 495 | * // 'puli-dir' => '.puli', |
||
| 496 | * // 'registry-file' => '.puli/ServiceRegistry.php', |
||
| 497 | * // ) |
||
| 498 | * |
||
| 499 | * print_r($config->toFlatRawArray()); |
||
| 500 | * // Array( |
||
| 501 | * // 'puli-dir' => '.puli', |
||
| 502 | * // 'registry-file' => '{$puli-dir}/ServiceRegistry.php', |
||
| 503 | * // ) |
||
| 504 | * ``` |
||
| 505 | * |
||
| 506 | * @param bool $includeFallback Whether to include values set in the base |
||
| 507 | * configuration passed to {@link __construct()}. |
||
| 508 | * |
||
| 509 | * @return array The raw configuration values. |
||
| 510 | */ |
||
| 511 | 34 | public function toFlatRawArray($includeFallback = true) |
|
| 517 | |||
| 518 | /** |
||
| 519 | * Returns all configuration values as nested array. |
||
| 520 | * |
||
| 521 | * @param bool $includeFallback Whether to include values set in the base |
||
| 522 | * configuration passed to {@link __construct()}. |
||
| 523 | * |
||
| 524 | * @return array The configuration values. |
||
| 525 | */ |
||
| 526 | 3 | public function toArray($includeFallback = true) |
|
| 530 | |||
| 531 | /** |
||
| 532 | * Returns all raw configuration values as nested array. |
||
| 533 | * |
||
| 534 | * Unlike {@link toArray()}, this method does not resolve placeholders: |
||
| 535 | * |
||
| 536 | * ```php |
||
| 537 | * $config = new Config(); |
||
| 538 | * $config->set(Config::PULI_DIR, '.puli'); |
||
| 539 | * $config->set(Config::REPO_STORAGE_DIR, '{$puli-dir}/repository'); |
||
| 540 | * |
||
| 541 | * print_r($config->toArray()); |
||
| 542 | * // Array( |
||
| 543 | * // 'puli-dir' => '.puli', |
||
| 544 | * // 'repository. => array( |
||
| 545 | * // 'storage-dir' => '.puli/repository', |
||
| 546 | * // ), |
||
| 547 | * // ) |
||
| 548 | * |
||
| 549 | * print_r($config->toRawArray()); |
||
| 550 | * // Array( |
||
| 551 | * // 'puli-dir' => '.puli', |
||
| 552 | * // 'repository. => array( |
||
| 553 | * // 'storage-dir' => '{$puli-dir}/repository', |
||
| 554 | * // ), |
||
| 555 | * // ) |
||
| 556 | * ``` |
||
| 557 | * |
||
| 558 | * @param bool $includeFallback Whether to include values set in the base |
||
| 559 | * configuration passed to {@link __construct()}. |
||
| 560 | * |
||
| 561 | * @return array The raw configuration values. |
||
| 562 | */ |
||
| 563 | 15 | public function toRawArray($includeFallback = true) |
|
| 575 | |||
| 576 | /** |
||
| 577 | * Returns whether the configuration is empty. |
||
| 578 | * |
||
| 579 | * @param bool $includeFallback Whether to include values set in the base |
||
| 580 | * configuration passed to {@link __construct()}. |
||
| 581 | * |
||
| 582 | * @return bool Returns `true` if no key is set and `false` otherwise. |
||
| 583 | */ |
||
| 584 | 5 | public function isEmpty($includeFallback = true) |
|
| 594 | |||
| 595 | /** |
||
| 596 | * @param string $key |
||
| 597 | * @param mixed $value |
||
| 598 | */ |
||
| 599 | 447 | private function validate($key, $value) |
|
| 638 | |||
| 639 | /** |
||
| 640 | * @param string $key |
||
| 641 | * @param mixed $value |
||
| 642 | * |
||
| 643 | * @throws InvalidConfigException If the config value isn't an array. |
||
| 644 | */ |
||
| 645 | 9 | View Code Duplication | private function assertArray($key, $value) |
| 655 | |||
| 656 | /** |
||
| 657 | * @param string $key |
||
| 658 | * @param mixed $value |
||
| 659 | * |
||
| 660 | * @throws InvalidConfigException If the config value is null. |
||
| 661 | */ |
||
| 662 | 432 | private function assertNotNull($key, $value) |
|
| 672 | |||
| 673 | /** |
||
| 674 | * @param string $key |
||
| 675 | * @param mixed $value |
||
| 676 | * |
||
| 677 | * @throws InvalidConfigException If the config value isn't a string. |
||
| 678 | */ |
||
| 679 | 418 | View Code Duplication | private function assertString($key, $value) |
| 689 | |||
| 690 | /** |
||
| 691 | * @param string $key |
||
| 692 | * @param mixed $value |
||
| 693 | * |
||
| 694 | * @throws InvalidConfigException If the config value isn't an integer. |
||
| 695 | */ |
||
| 696 | View Code Duplication | private function assertInteger($key, $value) |
|
| 706 | |||
| 707 | /** |
||
| 708 | * @param string $key |
||
| 709 | * @param mixed $value |
||
| 710 | * |
||
| 711 | * @throws InvalidConfigException If the config value isn't a boolean. |
||
| 712 | */ |
||
| 713 | 310 | View Code Duplication | private function assertBoolean($key, $value) |
| 723 | |||
| 724 | /** |
||
| 725 | * @param string $key |
||
| 726 | * @param mixed $value |
||
| 727 | * |
||
| 728 | * @throws InvalidConfigException If the config value is an empty string. |
||
| 729 | */ |
||
| 730 | 421 | private function assertNonEmpty($key, $value) |
|
| 739 | |||
| 740 | /** |
||
| 741 | * @param mixed $raw |
||
| 742 | * @param bool $fallback |
||
| 743 | * |
||
| 744 | * @return mixed |
||
| 745 | */ |
||
| 746 | 133 | private function replacePlaceholders($raw, $fallback = true) |
|
| 766 | |||
| 767 | /** |
||
| 768 | * @param string $keyPrefix |
||
| 769 | * |
||
| 770 | * @return array |
||
| 771 | */ |
||
| 772 | 47 | private function filterByKeyPrefix($keyPrefix) |
|
| 787 | |||
| 788 | /** |
||
| 789 | * @param string $keyPrefix |
||
| 790 | * |
||
| 791 | * @return bool |
||
| 792 | */ |
||
| 793 | 3 | private function containsKeyPrefix($keyPrefix) |
|
| 803 | |||
| 804 | /** |
||
| 805 | * @param string $keyPrefix |
||
| 806 | */ |
||
| 807 | 10 | private function removeByKeyPrefix($keyPrefix) |
|
| 817 | |||
| 818 | /** |
||
| 819 | * @param string $key |
||
| 820 | * @param mixed $value |
||
| 821 | * @param array $values |
||
| 822 | */ |
||
| 823 | 52 | private function addKeyValue($key, $value, array &$values) |
|
| 838 | } |
||
| 839 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.