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 |
||
| 74 | class Config { |
||
| 75 | |||
| 76 | /** |
||
| 77 | * A marker instance for the "anything" singleton value. Don't access |
||
| 78 | * directly, even in-class, always use self::anything() |
||
| 79 | * |
||
| 80 | * @var SS_Object |
||
| 81 | */ |
||
| 82 | private static $_anything = null; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Get a marker class instance that is used to do a "remove anything with |
||
| 86 | * this key" by adding $key => Config::anything() to the suppress array |
||
| 87 | * |
||
| 88 | * @return SS_Object |
||
| 89 | */ |
||
| 90 | public static function anything() { |
||
| 97 | |||
| 98 | // -- Source options bitmask -- |
||
| 99 | |||
| 100 | /** |
||
| 101 | * source options bitmask value - merge all parent configuration in as |
||
| 102 | * lowest priority. |
||
| 103 | * |
||
| 104 | * @const |
||
| 105 | */ |
||
| 106 | const INHERITED = 0; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * source options bitmask value - only get configuration set for this |
||
| 110 | * specific class, not any of it's parents. |
||
| 111 | * |
||
| 112 | * @const |
||
| 113 | */ |
||
| 114 | const UNINHERITED = 1; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * source options bitmask value - inherit, but stop on the first class |
||
| 118 | * that actually provides a value (event an empty value). |
||
| 119 | * |
||
| 120 | * @const |
||
| 121 | */ |
||
| 122 | const FIRST_SET = 2; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @const source options bitmask value - do not use additional statics |
||
| 126 | * sources (such as extension) |
||
| 127 | */ |
||
| 128 | const EXCLUDE_EXTRA_SOURCES = 4; |
||
| 129 | |||
| 130 | // -- get_value_type response enum -- |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Return flag for get_value_type indicating value is a scalar (or really |
||
| 134 | * just not-an-array, at least ATM) |
||
| 135 | * |
||
| 136 | * @const |
||
| 137 | */ |
||
| 138 | const ISNT_ARRAY = 1; |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Return flag for get_value_type indicating value is an array. |
||
| 142 | * @const |
||
| 143 | */ |
||
| 144 | const IS_ARRAY = 2; |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Get whether the value is an array or not. Used to be more complicated, |
||
| 148 | * but still nice sugar to have an enum to compare and not just a true / |
||
| 149 | * false value. |
||
| 150 | * |
||
| 151 | * @param $val any - The value |
||
| 152 | * |
||
| 153 | * @return int - One of ISNT_ARRAY or IS_ARRAY |
||
| 154 | */ |
||
| 155 | protected static function get_value_type($val) { |
||
| 162 | |||
| 163 | /** |
||
| 164 | * What to do if there's a type mismatch. |
||
| 165 | * |
||
| 166 | * @throws UnexpectedValueException |
||
| 167 | */ |
||
| 168 | protected static function type_mismatch() { |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @todo If we can, replace next static & static methods with DI once that's in |
||
| 175 | */ |
||
| 176 | protected static $instance; |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Get the current active Config instance. |
||
| 180 | * |
||
| 181 | * Configs should not normally be manually created. |
||
| 182 | * |
||
| 183 | * In general use you will use this method to obtain the current Config |
||
| 184 | * instance. |
||
| 185 | * |
||
| 186 | * @return Config |
||
| 187 | */ |
||
| 188 | public static function inst() { |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Set the current active {@link Config} instance. |
||
| 198 | * |
||
| 199 | * {@link Config} objects should not normally be manually created. |
||
| 200 | * |
||
| 201 | * A use case for replacing the active configuration set would be for |
||
| 202 | * creating an isolated environment for unit tests. |
||
| 203 | * |
||
| 204 | * @param Config $instance New instance of Config to assign |
||
| 205 | * @return Config Reference to new active Config instance |
||
| 206 | */ |
||
| 207 | public static function set_instance($instance) { |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Make the newly active {@link Config} be a copy of the current active |
||
| 217 | * {@link Config} instance. |
||
| 218 | * |
||
| 219 | * You can then make changes to the configuration by calling update and |
||
| 220 | * remove on the new value returned by {@link Config::inst()}, and then discard |
||
| 221 | * those changes later by calling unnest. |
||
| 222 | * |
||
| 223 | * @return Config Reference to new active Config instance |
||
| 224 | */ |
||
| 225 | public static function nest() { |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Change the active Config back to the Config instance the current active |
||
| 235 | * Config object was copied from. |
||
| 236 | * |
||
| 237 | * @return Config Reference to new active Config instance |
||
| 238 | */ |
||
| 239 | public static function unnest() { |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @var array |
||
| 254 | */ |
||
| 255 | protected $cache; |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Each copy of the Config object need's it's own cache, so changes don't |
||
| 259 | * leak through to other instances. |
||
| 260 | */ |
||
| 261 | public function __construct() { |
||
| 264 | |||
| 265 | public function __clone() { |
||
| 268 | |||
| 269 | /** |
||
| 270 | * @var Config - The config instance this one was copied from when |
||
| 271 | * Config::nest() was called. |
||
| 272 | */ |
||
| 273 | protected $nestedFrom = null; |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @var array - Array of arrays. Each member is an nested array keyed as |
||
| 277 | * $class => $name => $value, where value is a config value to treat as |
||
| 278 | * the highest priority item. |
||
| 279 | */ |
||
| 280 | protected $overrides = array(); |
||
| 281 | |||
| 282 | /** |
||
| 283 | * @var array $suppresses Array of arrays. Each member is an nested array |
||
| 284 | * keyed as $class => $name => $value, where value is a config value suppress |
||
| 285 | * from any lower priority item. |
||
| 286 | */ |
||
| 287 | protected $suppresses = array(); |
||
| 288 | |||
| 289 | /** |
||
| 290 | * @var array |
||
| 291 | */ |
||
| 292 | protected $staticManifests = array(); |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @param SS_ConfigStaticManifest |
||
| 296 | */ |
||
| 297 | public function pushConfigStaticManifest(SS_ConfigStaticManifest $manifest) { |
||
| 302 | |||
| 303 | /** @var [array] - The list of settings pulled from config files to search through */ |
||
| 304 | protected $manifests = array(); |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Add another manifest to the list of config manifests to search through. |
||
| 308 | * |
||
| 309 | * WARNING: Config manifests to not merge entries, and do not solve before/after rules inter-manifest - |
||
| 310 | * instead, the last manifest to be added always wins |
||
| 311 | */ |
||
| 312 | public function pushConfigYamlManifest(SS_ConfigManifest $manifest) { |
||
| 328 | |||
| 329 | /** @var [Config_ForClass] - The list of Config_ForClass instances, keyed off class */ |
||
| 330 | static protected $for_class_instances = array(); |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Get an accessor that returns results by class by default. |
||
| 334 | * |
||
| 335 | * Shouldn't be overridden, since there might be many Config_ForClass instances already held in the wild. Each |
||
| 336 | * Config_ForClass instance asks the current_instance of Config for the actual result, so override that instead |
||
| 337 | * |
||
| 338 | * @param $class |
||
| 339 | * @return Config_ForClass |
||
| 340 | */ |
||
| 341 | public function forClass($class) { |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Merge a lower priority associative array into an existing higher priority associative array, as per the class |
||
| 352 | * docblock rules |
||
| 353 | * |
||
| 354 | * It is assumed you've already checked that you've got two associative arrays, not scalars or sequential arrays |
||
| 355 | * |
||
| 356 | * @param $dest array - The existing high priority associative array |
||
| 357 | * @param $src array - The low priority associative array to merge in |
||
| 358 | */ |
||
| 359 | public static function merge_array_low_into_high(&$dest, $src) { |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Merge a higher priority assocative array into an existing lower priority associative array, as per the class |
||
| 385 | * docblock rules. |
||
| 386 | * |
||
| 387 | * Much more expensive that the other way around, as there's no way to insert an associative k/v pair into an |
||
| 388 | * array at the top of the array |
||
| 389 | * |
||
| 390 | * @static |
||
| 391 | * @param $dest array - The existing low priority associative array |
||
| 392 | * @param $src array - The high priority array to merge in |
||
| 393 | */ |
||
| 394 | public static function merge_array_high_into_low(&$dest, $src) { |
||
| 399 | |||
| 400 | public static function merge_high_into_low(&$result, $value) { |
||
| 414 | |||
| 415 | public static function merge_low_into_high(&$result, $value, $suppress) { |
||
| 439 | |||
| 440 | public static function check_value_contained_in_suppress_array($v, $suppresses) { |
||
| 447 | |||
| 448 | static protected function check_key_or_value_contained_in_suppress_array($k, $v, $suppresses) { |
||
| 455 | |||
| 456 | static protected function filter_array_by_suppress_array($array, $suppress) { |
||
| 470 | |||
| 471 | protected $extraConfigSources = array(); |
||
| 472 | |||
| 473 | public function extraConfigSourcesChanged($class) { |
||
| 477 | |||
| 478 | protected function getUncached($class, $name, $sourceOptions, &$result, $suppress, &$tags) { |
||
| 556 | |||
| 557 | /** |
||
| 558 | * Get the config value associated for a given class and property |
||
| 559 | * |
||
| 560 | * This merges all current sources and overrides together to give final value |
||
| 561 | * todo: Currently this is done every time. This function is an inner loop function, so we really need to be |
||
| 562 | * caching heavily here. |
||
| 563 | * |
||
| 564 | * @param $class string - The name of the class to get the value for |
||
| 565 | * @param $name string - The property to get the value for |
||
| 566 | * @param int $sourceOptions Bitmask which can be set to some combintain of Config::UNINHERITED, |
||
| 567 | * Config::FIRST_SET, and Config::EXCLUDE_EXTENSIONS. |
||
| 568 | * |
||
| 569 | * Config::UNINHERITED does not include parent classes when merging configuration fragments |
||
| 570 | * Config::FIRST_SET stops inheriting once the first class that sets a value (even an empty value) is encoutered |
||
| 571 | * Config::EXCLUDE_EXTRA_SOURCES does not include any additional static sources (such as extensions) |
||
| 572 | * |
||
| 573 | * Config::INHERITED is a utility constant that can be used to mean "none of the above", equvilient to 0 |
||
| 574 | * Setting both Config::UNINHERITED and Config::FIRST_SET behaves the same as just Config::UNINHERITED |
||
| 575 | * |
||
| 576 | * should the parent classes value be merged in as the lowest priority source? |
||
| 577 | * @param $result array|scalar Reference to a variable to put the result in. Also returned, so this can be left |
||
| 578 | * as null safely. If you do pass a value, it will be treated as the highest priority |
||
| 579 | * value in the result chain |
||
| 580 | * @param $suppress array Internal use when called by child classes. Array of mask pairs to filter value by |
||
| 581 | * @return array|scalar The value of the config item, or null if no value set. Could be an associative array, |
||
| 582 | * sequential array or scalar depending on value (see class docblock) |
||
| 583 | */ |
||
| 584 | public function get($class, $name, $sourceOptions = 0, &$result = null, $suppress = null) { |
||
| 598 | |||
| 599 | /** |
||
| 600 | * Update a configuration value |
||
| 601 | * |
||
| 602 | * Configuration is modify only. The value passed is merged into the existing configuration. If you want to |
||
| 603 | * replace the current array value, you'll need to call remove first. |
||
| 604 | * |
||
| 605 | * @param string $class The class to update a configuration value for |
||
| 606 | * @param string $name The configuration property name to update |
||
| 607 | * @param mixed $value The value to update with |
||
| 608 | * |
||
| 609 | * Arrays are recursively merged into current configuration as "latest" - for associative arrays the passed value |
||
| 610 | * replaces any item with the same key, for sequential arrays the items are placed at the end of the array, for |
||
| 611 | * non-array values, this value replaces any existing value |
||
| 612 | * |
||
| 613 | * You will get an error if you try and override array values with non-array values or vice-versa |
||
| 614 | */ |
||
| 615 | public function update($class, $name, $val) { |
||
| 630 | |||
| 631 | /** |
||
| 632 | * Remove a configuration value |
||
| 633 | * |
||
| 634 | * You can specify a key, a key and a value, or neither. Either argument can be Config::anything(), which is |
||
| 635 | * what is defaulted to if you don't specify something |
||
| 636 | * |
||
| 637 | * This removes any current configuration value that matches the key and/or value specified |
||
| 638 | * |
||
| 639 | * Works like this: |
||
| 640 | * - Check the current override array, and remove any values that match the arguments provided |
||
| 641 | * - Keeps track of the arguments passed to this method, and in get filters everything _except_ the current |
||
| 642 | * override array to exclude any match |
||
| 643 | * |
||
| 644 | * This way we can re-set anything removed by a call to this function by calling set. Because the current override |
||
| 645 | * array is only filtered immediately on calling this remove method, that value will then be exposed. However, |
||
| 646 | * every other source is filtered on request, so no amount of changes to parent's configuration etc can override a |
||
| 647 | * remove call. |
||
| 648 | * |
||
| 649 | * @param string $class The class to remove a configuration value from |
||
| 650 | * @param string $name The configuration name |
||
| 651 | * |
||
| 652 | * Matching is always by "==", not by "===" |
||
| 653 | */ |
||
| 654 | public function remove($class, $name /*,$key = null*/ /*,$value = null*/) { |
||
| 681 | |||
| 682 | } |
||
| 683 | |||
| 950 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..