Complex classes like ClassRegistry 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 ClassRegistry, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | class ClassRegistry { |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Names of classes with their objects. |
||
| 37 | * |
||
| 38 | * @var array |
||
| 39 | */ |
||
| 40 | protected $_objects = array(); |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Names of class names mapped to the object in the registry. |
||
| 44 | * |
||
| 45 | * @var array |
||
| 46 | */ |
||
| 47 | protected $_map = array(); |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Default constructor parameter settings, indexed by type |
||
| 51 | * |
||
| 52 | * @var array |
||
| 53 | */ |
||
| 54 | protected $_config = array(); |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Return a singleton instance of the ClassRegistry. |
||
| 58 | * |
||
| 59 | * @return ClassRegistry instance |
||
| 60 | */ |
||
| 61 | public static function getInstance() { |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Loads a class, registers the object in the registry and returns instance of the object. ClassRegistry::init() |
||
| 71 | * is used as a factory for models, and handle correct injecting of settings, that assist in testing. |
||
| 72 | * |
||
| 73 | * Examples |
||
| 74 | * Simple Use: Get a Post model instance ```ClassRegistry::init('Post');``` |
||
| 75 | * |
||
| 76 | * Expanded: ```array('class' => 'ClassName', 'alias' => 'AliasNameStoredInTheRegistry');``` |
||
| 77 | * |
||
| 78 | * Model Classes can accept optional ```array('id' => $id, 'table' => $table, 'ds' => $ds, 'alias' => $alias);``` |
||
| 79 | * |
||
| 80 | * When $class is a numeric keyed array, multiple class instances will be stored in the registry, |
||
| 81 | * no instance of the object will be returned |
||
| 82 | * {{{ |
||
| 83 | * array( |
||
| 84 | * array('class' => 'ClassName', 'alias' => 'AliasNameStoredInTheRegistry'), |
||
| 85 | * array('class' => 'ClassName', 'alias' => 'AliasNameStoredInTheRegistry'), |
||
| 86 | * array('class' => 'ClassName', 'alias' => 'AliasNameStoredInTheRegistry') |
||
| 87 | * ); |
||
| 88 | * }}} |
||
| 89 | * @param string|array $class as a string or a single key => value array instance will be created, |
||
| 90 | * stored in the registry and returned. |
||
| 91 | * @param boolean $strict if set to true it will return false if the class was not found instead |
||
| 92 | * of trying to create an AppModel |
||
| 93 | * @return object instance of ClassName. |
||
| 94 | * @throws CakeException when you try to construct an interface or abstract class. |
||
| 95 | */ |
||
| 96 | public static function init($class, $strict = false) { |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Add $object to the registry, associating it with the name $key. |
||
| 205 | * |
||
| 206 | * @param string $key Key for the object in registry |
||
| 207 | * @param object $object Object to store |
||
| 208 | * @return boolean True if the object was written, false if $key already exists |
||
| 209 | */ |
||
| 210 | public static function addObject($key, $object) { |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Remove object which corresponds to given key. |
||
| 222 | * |
||
| 223 | * @param string $key Key of object to remove from registry |
||
| 224 | * @return void |
||
| 225 | */ |
||
| 226 | public static function removeObject($key) { |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Returns true if given key is present in the ClassRegistry. |
||
| 236 | * |
||
| 237 | * @param string $key Key to look for |
||
| 238 | * @return boolean true if key exists in registry, false otherwise |
||
| 239 | */ |
||
| 240 | public static function isKeySet($key) { |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Get all keys from the registry. |
||
| 249 | * |
||
| 250 | * @return array Set of keys stored in registry |
||
| 251 | */ |
||
| 252 | public static function keys() { |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Return object which corresponds to given key. |
||
| 258 | * |
||
| 259 | * @param string $key Key of object to look for |
||
| 260 | * @return mixed Object stored in registry or boolean false if the object does not exist. |
||
| 261 | */ |
||
| 262 | public static function getObject($key) { |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Sets the default constructor parameter for an object type |
||
| 279 | * |
||
| 280 | * @param string $type Type of object. If this parameter is omitted, defaults to "Model" |
||
| 281 | * @param array $param The parameter that will be passed to object constructors when objects |
||
| 282 | * of $type are created |
||
| 283 | * @return mixed Void if $param is being set. Otherwise, if only $type is passed, returns |
||
| 284 | * the previously-set value of $param, or null if not set. |
||
| 285 | */ |
||
| 286 | public static function config($type, $param = array()) { |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Checks to see if $alias is a duplicate $class Object |
||
| 305 | * |
||
| 306 | * @param string $alias |
||
| 307 | * @param string $class |
||
| 308 | * @return boolean |
||
| 309 | */ |
||
| 310 | protected function &_duplicate($alias, $class) { |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Add a key name pair to the registry to map name to class in the registry. |
||
| 324 | * |
||
| 325 | * @param string $key Key to include in map |
||
| 326 | * @param string $name Key that is being mapped |
||
| 327 | * @return void |
||
| 328 | */ |
||
| 329 | public static function map($key, $name) { |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Get all keys from the map in the registry. |
||
| 340 | * |
||
| 341 | * @return array Keys of registry's map |
||
| 342 | */ |
||
| 343 | public static function mapKeys() { |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Return the name of a class in the registry. |
||
| 349 | * |
||
| 350 | * @param string $key Key to find in map |
||
| 351 | * @return string Mapped value |
||
| 352 | */ |
||
| 353 | protected function _getMap($key) { |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Flushes all objects from the ClassRegistry. |
||
| 361 | * |
||
| 362 | * @return void |
||
| 363 | */ |
||
| 364 | public static function flush() { |
||
| 369 | |||
| 370 | } |
||
| 371 |
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.