Complex classes like ConfigManifest 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 ConfigManifest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class ConfigManifest { |
||
| 19 | |||
| 20 | /** @var string - The base path used when building the manifest */ |
||
| 21 | protected $base; |
||
| 22 | |||
| 23 | /** @var string - A string to prepend to all cache keys to ensure all keys are unique to just this $base */ |
||
| 24 | protected $key; |
||
| 25 | |||
| 26 | /** @var bool - Whether `test` directories should be searched when searching for configuration */ |
||
| 27 | protected $includeTests; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var Zend_Cache_Core |
||
| 31 | */ |
||
| 32 | protected $cache; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * All the values needed to be collected to determine the correct combination of fragements for |
||
| 36 | * the current environment. |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | protected $variantKeySpec = false; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * All the _config.php files. Need to be included every request & can't be cached. Not variant specific. |
||
| 43 | * @var array |
||
| 44 | */ |
||
| 45 | protected $phpConfigSources = array(); |
||
| 46 | |||
| 47 | /** |
||
| 48 | * All the _config/*.yml fragments pre-parsed and sorted in ascending include order. Not variant specific. |
||
| 49 | * @var array |
||
| 50 | */ |
||
| 51 | protected $yamlConfigFragments = array(); |
||
| 52 | |||
| 53 | /** |
||
| 54 | * The calculated config from _config/*.yml, sorted, filtered and merged. Variant specific. |
||
| 55 | * @var array |
||
| 56 | */ |
||
| 57 | public $yamlConfig = array(); |
||
| 58 | |||
| 59 | /** |
||
| 60 | * The variant key state as when yamlConfig was loaded |
||
| 61 | * @var string |
||
| 62 | */ |
||
| 63 | protected $yamlConfigVariantKey = null; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var [callback] A list of callbacks to be called whenever the content of yamlConfig changes |
||
| 67 | */ |
||
| 68 | protected $configChangeCallbacks = array(); |
||
| 69 | |||
| 70 | /** |
||
| 71 | * A side-effect of collecting the _config fragments is the calculation of all module directories, since |
||
| 72 | * the definition of a module is "a directory that contains either a _config.php file or a _config directory |
||
| 73 | * @var array |
||
| 74 | */ |
||
| 75 | public $modules = array(); |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Adds a path as a module |
||
| 79 | * |
||
| 80 | * @param string $path |
||
| 81 | */ |
||
| 82 | public function addModule($path) { |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Returns true if the passed module exists |
||
| 92 | * |
||
| 93 | * @param string $module |
||
| 94 | * @return bool |
||
| 95 | */ |
||
| 96 | public function moduleExists($module) { |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Constructs and initialises a new configuration object, either loading |
||
| 102 | * from the cache or re-scanning for classes. |
||
| 103 | * |
||
| 104 | * @param string $base The project base path. |
||
| 105 | * @param bool $includeTests |
||
| 106 | * @param bool $forceRegen Force the manifest to be regenerated. |
||
| 107 | */ |
||
| 108 | public function __construct($base, $includeTests = false, $forceRegen = false ) { |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Provides a hook for mock unit tests despite no DI |
||
| 135 | * @return Zend_Cache_Core |
||
| 136 | */ |
||
| 137 | protected function getCache() |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Register a callback to be called whenever the calculated merged config changes |
||
| 147 | * |
||
| 148 | * In some situations the merged config can change - for instance, code in _config.php can cause which Only |
||
| 149 | * and Except fragments match. Registering a callback with this function allows code to be called when |
||
| 150 | * this happens. |
||
| 151 | * |
||
| 152 | * @param callback $callback |
||
| 153 | */ |
||
| 154 | public function registerChangeCallback($callback) { |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Includes all of the php _config.php files found by this manifest. Called by SS_Config when adding this manifest |
||
| 160 | * @return void |
||
| 161 | */ |
||
| 162 | public function activateConfig() { |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Gets the (merged) config value for the given class and config property name |
||
| 172 | * |
||
| 173 | * @param string $class - The class to get the config property value for |
||
| 174 | * @param string $name - The config property to get the value for |
||
| 175 | * @param mixed $default - What to return if no value was contained in any YAML file for the passed $class and $name |
||
| 176 | * @return mixed The merged set of all values contained in all the YAML configuration files for the passed |
||
| 177 | * $class and $name, or $default if there are none |
||
| 178 | */ |
||
| 179 | public function get($class, $name, $default=null) { |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Returns the string that uniquely identifies this variant. The variant is the combination of classes, modules, |
||
| 188 | * environment, environment variables and constants that selects which yaml fragments actually make it into the |
||
| 189 | * configuration because of "only" |
||
| 190 | * and "except" rules. |
||
| 191 | * |
||
| 192 | * @return string |
||
| 193 | */ |
||
| 194 | public function variantKey() { |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Completely regenerates the manifest file. Scans through finding all php _config.php and yaml _config/*.ya?ml |
||
| 214 | * files,parses the yaml files into fragments, sorts them and figures out what values need to be checked to pick |
||
| 215 | * the correct variant. |
||
| 216 | * |
||
| 217 | * Does _not_ build the actual variant |
||
| 218 | * |
||
| 219 | * @param bool $includeTests |
||
| 220 | * @param bool $cache Cache the result. |
||
| 221 | */ |
||
| 222 | public function regenerate($includeTests = false, $cache = true) { |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Handle finding a php file. We just keep a record of all php files found, we don't include them |
||
| 256 | * at this stage |
||
| 257 | * |
||
| 258 | * Public so that ManifestFileFinder can call it. Not for general use. |
||
| 259 | * |
||
| 260 | * @param string $basename |
||
| 261 | * @param string $pathname |
||
| 262 | * @param int $depth |
||
| 263 | */ |
||
| 264 | public function addSourceConfigFile($basename, $pathname, $depth) { |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Handle finding a yml file. Parse the file by spliting it into header/fragment pairs, |
||
| 272 | * and normalising some of the header values (especially: give anonymous name if none assigned, |
||
| 273 | * splt/complete before and after matchers) |
||
| 274 | * |
||
| 275 | * Public so that ManifestFileFinder can call it. Not for general use. |
||
| 276 | * |
||
| 277 | * @param string $basename |
||
| 278 | * @param string $pathname |
||
| 279 | * @param int $depth |
||
| 280 | */ |
||
| 281 | public function addYAMLConfigFile($basename, $pathname, $depth) { |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Sorts the YAML fragments so that the "before" and "after" rules are met. |
||
| 356 | * Throws an error if there's a loop |
||
| 357 | * |
||
| 358 | * We can't use regular sorts here - we need a topological sort. Easiest |
||
| 359 | * way is with a DAG, so build up a DAG based on the before/after rules, then |
||
| 360 | * sort that. |
||
| 361 | * |
||
| 362 | * @return void |
||
| 363 | */ |
||
| 364 | protected function sortYamlFragments() { |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Return a string "after", "before" or "undefined" depending on whether the YAML fragment array element passed |
||
| 411 | * as $a should be positioned after, before, or either compared to the YAML fragment array element passed as $b |
||
| 412 | * |
||
| 413 | * @param array $a A YAML config fragment as loaded by addYAMLConfigFile |
||
| 414 | * @param array $b A YAML config fragment as loaded by addYAMLConfigFile |
||
| 415 | * @return string "after", "before" or "undefined" |
||
| 416 | */ |
||
| 417 | protected function relativeOrder($a, $b) { |
||
| 470 | |||
| 471 | /** |
||
| 472 | * This function filters the loaded yaml fragments, removing any that can't ever have their "only" and "except" |
||
| 473 | * rules match. |
||
| 474 | * |
||
| 475 | * Some tests in "only" and "except" rules need to be checked per request, but some are manifest based - |
||
| 476 | * these are invariant over requests and only need checking on manifest rebuild. So we can prefilter these before |
||
| 477 | * saving yamlConfigFragments to speed up the process of checking the per-request variant/ |
||
| 478 | */ |
||
| 479 | public function prefilterYamlFragments() { |
||
| 498 | |||
| 499 | /** |
||
| 500 | * Returns false if the prefilterable parts of the rule aren't met, and true if they are |
||
| 501 | * |
||
| 502 | * @param $rules array - A hash of rules as allowed in the only or except portion of a config fragment header |
||
| 503 | * @return bool - True if the rules are met, false if not. (Note that depending on whether we were passed an |
||
| 504 | * only or an except rule, |
||
| 505 | * which values means accept or reject a fragment |
||
| 506 | */ |
||
| 507 | public function matchesPrefilterVariantRules($rules) { |
||
| 529 | |||
| 530 | /** |
||
| 531 | * Builds the variant key spec - the list of values that need to be build to give a key that uniquely identifies |
||
| 532 | * this variant. |
||
| 533 | */ |
||
| 534 | public function buildVariantKeySpec() { |
||
| 542 | |||
| 543 | /** |
||
| 544 | * Adds any variables referenced in the passed rules to the $this->variantKeySpec array |
||
| 545 | * |
||
| 546 | * @param array $rules |
||
| 547 | */ |
||
| 548 | public function addVariantKeySpecRules($rules) { |
||
| 578 | |||
| 579 | /** |
||
| 580 | * Calculates which yaml config fragments are applicable in this variant, and merge those all together into |
||
| 581 | * the $this->yamlConfig propperty |
||
| 582 | * |
||
| 583 | * Checks cache and takes care of loading yamlConfigFragments if they aren't already present, but expects |
||
| 584 | * $variantKeySpec to already be set |
||
| 585 | * |
||
| 586 | * @param bool $cache |
||
| 587 | */ |
||
| 588 | public function buildYamlConfigVariant($cache = true) { |
||
| 632 | |||
| 633 | /** |
||
| 634 | * Returns false if the non-prefilterable parts of the rule aren't met, and true if they are |
||
| 635 | * |
||
| 636 | * @param array $rules |
||
| 637 | * @return bool|string |
||
| 638 | */ |
||
| 639 | public function matchesVariantRules($rules) { |
||
| 685 | |||
| 686 | /** |
||
| 687 | * Recursively merge a yaml fragment's configuration array into the primary merged configuration array. |
||
| 688 | * @param $into |
||
| 689 | * @param $fragment |
||
| 690 | * @return void |
||
| 691 | */ |
||
| 692 | public function mergeInYamlFragment(&$into, $fragment) { |
||
| 699 | |||
| 700 | } |
||
| 701 |
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..