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 | |||
| 21 | /** @var string - The base path used when building the manifest */ |
||
| 22 | protected $base; |
||
| 23 | |||
| 24 | /** @var string - A string to prepend to all cache keys to ensure all keys are unique to just this $base */ |
||
| 25 | protected $key; |
||
| 26 | |||
| 27 | /** @var bool - Whether `test` directories should be searched when searching for configuration */ |
||
| 28 | protected $includeTests; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var Zend_Cache_Core |
||
| 32 | */ |
||
| 33 | protected $cache; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * All the values needed to be collected to determine the correct combination of fragements for |
||
| 37 | * the current environment. |
||
| 38 | * @var array |
||
| 39 | */ |
||
| 40 | protected $variantKeySpec = false; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * All the _config.php files. Need to be included every request & can't be cached. Not variant specific. |
||
| 44 | * @var array |
||
| 45 | */ |
||
| 46 | protected $phpConfigSources = array(); |
||
| 47 | |||
| 48 | /** |
||
| 49 | * All the _config/*.yml fragments pre-parsed and sorted in ascending include order. Not variant specific. |
||
| 50 | * @var array |
||
| 51 | */ |
||
| 52 | protected $yamlConfigFragments = array(); |
||
| 53 | |||
| 54 | /** |
||
| 55 | * The calculated config from _config/*.yml, sorted, filtered and merged. Variant specific. |
||
| 56 | * @var array |
||
| 57 | */ |
||
| 58 | public $yamlConfig = array(); |
||
| 59 | |||
| 60 | /** |
||
| 61 | * The variant key state as when yamlConfig was loaded |
||
| 62 | * @var string |
||
| 63 | */ |
||
| 64 | protected $yamlConfigVariantKey = null; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var [callback] A list of callbacks to be called whenever the content of yamlConfig changes |
||
| 68 | */ |
||
| 69 | protected $configChangeCallbacks = array(); |
||
| 70 | |||
| 71 | /** |
||
| 72 | * A side-effect of collecting the _config fragments is the calculation of all module directories, since |
||
| 73 | * the definition of a module is "a directory that contains either a _config.php file or a _config directory |
||
| 74 | * @var array |
||
| 75 | */ |
||
| 76 | public $modules = array(); |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Adds a path as a module |
||
| 80 | * |
||
| 81 | * @param string $path |
||
| 82 | */ |
||
| 83 | public function addModule($path) |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Returns true if the passed module exists |
||
| 94 | * |
||
| 95 | * @param string $module |
||
| 96 | * @return bool |
||
| 97 | */ |
||
| 98 | public function moduleExists($module) |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Constructs and initialises a new configuration object, either loading |
||
| 105 | * from the cache or re-scanning for classes. |
||
| 106 | * |
||
| 107 | * @param string $base The project base path. |
||
| 108 | * @param bool $includeTests |
||
| 109 | * @param bool $forceRegen Force the manifest to be regenerated. |
||
| 110 | */ |
||
| 111 | public function __construct($base, $includeTests = false, $forceRegen = false) |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Provides a hook for mock unit tests despite no DI |
||
| 139 | * @return Zend_Cache_Core |
||
| 140 | */ |
||
| 141 | protected function getCache() |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Register a callback to be called whenever the calculated merged config changes |
||
| 151 | * |
||
| 152 | * In some situations the merged config can change - for instance, code in _config.php can cause which Only |
||
| 153 | * and Except fragments match. Registering a callback with this function allows code to be called when |
||
| 154 | * this happens. |
||
| 155 | * |
||
| 156 | * @param callback $callback |
||
| 157 | */ |
||
| 158 | public function registerChangeCallback($callback) |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Includes all of the php _config.php files found by this manifest. Called by SS_Config when adding this manifest |
||
| 165 | * @return void |
||
| 166 | */ |
||
| 167 | public function activateConfig() |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Gets the (merged) config value for the given class and config property name |
||
| 180 | * |
||
| 181 | * @param string $class - The class to get the config property value for |
||
| 182 | * @param string $name - The config property to get the value for |
||
| 183 | * @param mixed $default - What to return if no value was contained in any YAML file for the passed $class and $name |
||
| 184 | * @return mixed The merged set of all values contained in all the YAML configuration files for the passed |
||
| 185 | * $class and $name, or $default if there are none |
||
| 186 | */ |
||
| 187 | public function get($class, $name, $default = null) |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Returns the string that uniquely identifies this variant. The variant is the combination of classes, modules, |
||
| 197 | * environment, environment variables and constants that selects which yaml fragments actually make it into the |
||
| 198 | * configuration because of "only" |
||
| 199 | * and "except" rules. |
||
| 200 | * |
||
| 201 | * @return string |
||
| 202 | */ |
||
| 203 | public function variantKey() |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Completely regenerates the manifest file. Scans through finding all php _config.php and yaml _config/*.ya?ml |
||
| 228 | * files,parses the yaml files into fragments, sorts them and figures out what values need to be checked to pick |
||
| 229 | * the correct variant. |
||
| 230 | * |
||
| 231 | * Does _not_ build the actual variant |
||
| 232 | * |
||
| 233 | * @param bool $includeTests |
||
| 234 | * @param bool $cache Cache the result. |
||
| 235 | */ |
||
| 236 | public function regenerate($includeTests = false, $cache = true) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Handle finding a php file. We just keep a record of all php files found, we don't include them |
||
| 272 | * at this stage |
||
| 273 | * |
||
| 274 | * Public so that ManifestFileFinder can call it. Not for general use. |
||
| 275 | * |
||
| 276 | * @param string $basename |
||
| 277 | * @param string $pathname |
||
| 278 | * @param int $depth |
||
| 279 | */ |
||
| 280 | public function addSourceConfigFile($basename, $pathname, $depth) |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Handle finding a yml file. Parse the file by spliting it into header/fragment pairs, |
||
| 289 | * and normalising some of the header values (especially: give anonymous name if none assigned, |
||
| 290 | * splt/complete before and after matchers) |
||
| 291 | * |
||
| 292 | * Public so that ManifestFileFinder can call it. Not for general use. |
||
| 293 | * |
||
| 294 | * @param string $basename |
||
| 295 | * @param string $pathname |
||
| 296 | * @param int $depth |
||
| 297 | */ |
||
| 298 | public function addYAMLConfigFile($basename, $pathname, $depth) |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Sorts the YAML fragments so that the "before" and "after" rules are met. |
||
| 383 | * Throws an error if there's a loop |
||
| 384 | * |
||
| 385 | * We can't use regular sorts here - we need a topological sort. Easiest |
||
| 386 | * way is with a DAG, so build up a DAG based on the before/after rules, then |
||
| 387 | * sort that. |
||
| 388 | * |
||
| 389 | * @return void |
||
| 390 | */ |
||
| 391 | protected function sortYamlFragments() |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Return a string "after", "before" or "undefined" depending on whether the YAML fragment array element passed |
||
| 443 | * as $a should be positioned after, before, or either compared to the YAML fragment array element passed as $b |
||
| 444 | * |
||
| 445 | * @param array $a A YAML config fragment as loaded by addYAMLConfigFile |
||
| 446 | * @param array $b A YAML config fragment as loaded by addYAMLConfigFile |
||
| 447 | * @return string "after", "before" or "undefined" |
||
| 448 | */ |
||
| 449 | protected function relativeOrder($a, $b) |
||
| 506 | |||
| 507 | /** |
||
| 508 | * This function filters the loaded yaml fragments, removing any that can't ever have their "only" and "except" |
||
| 509 | * rules match. |
||
| 510 | * |
||
| 511 | * Some tests in "only" and "except" rules need to be checked per request, but some are manifest based - |
||
| 512 | * these are invariant over requests and only need checking on manifest rebuild. So we can prefilter these before |
||
| 513 | * saving yamlConfigFragments to speed up the process of checking the per-request variant/ |
||
| 514 | */ |
||
| 515 | public function prefilterYamlFragments() |
||
| 537 | |||
| 538 | /** |
||
| 539 | * Returns false if the prefilterable parts of the rule aren't met, and true if they are |
||
| 540 | * |
||
| 541 | * @param $rules array - A hash of rules as allowed in the only or except portion of a config fragment header |
||
| 542 | * @return bool - True if the rules are met, false if not. (Note that depending on whether we were passed an |
||
| 543 | * only or an except rule, |
||
| 544 | * which values means accept or reject a fragment |
||
| 545 | */ |
||
| 546 | public function matchesPrefilterVariantRules($rules) |
||
| 571 | |||
| 572 | /** |
||
| 573 | * Builds the variant key spec - the list of values that need to be build to give a key that uniquely identifies |
||
| 574 | * this variant. |
||
| 575 | */ |
||
| 576 | public function buildVariantKeySpec() |
||
| 589 | |||
| 590 | /** |
||
| 591 | * Adds any variables referenced in the passed rules to the $this->variantKeySpec array |
||
| 592 | * |
||
| 593 | * @param array $rules |
||
| 594 | */ |
||
| 595 | public function addVariantKeySpecRules($rules) |
||
| 634 | |||
| 635 | /** |
||
| 636 | * Calculates which yaml config fragments are applicable in this variant, and merge those all together into |
||
| 637 | * the $this->yamlConfig propperty |
||
| 638 | * |
||
| 639 | * Checks cache and takes care of loading yamlConfigFragments if they aren't already present, but expects |
||
| 640 | * $variantKeySpec to already be set |
||
| 641 | * |
||
| 642 | * @param bool $cache |
||
| 643 | */ |
||
| 644 | public function buildYamlConfigVariant($cache = true) |
||
| 692 | |||
| 693 | /** |
||
| 694 | * Returns false if the non-prefilterable parts of the rule aren't met, and true if they are |
||
| 695 | * |
||
| 696 | * @param array $rules |
||
| 697 | * @return bool|string |
||
| 698 | */ |
||
| 699 | public function matchesVariantRules($rules) |
||
| 748 | |||
| 749 | /** |
||
| 750 | * Recursively merge a yaml fragment's configuration array into the primary merged configuration array. |
||
| 751 | * @param $into |
||
| 752 | * @param $fragment |
||
| 753 | * @return void |
||
| 754 | */ |
||
| 755 | public function mergeInYamlFragment(&$into, $fragment) |
||
| 763 | } |
||
| 764 |
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..