Complex classes like MemoryConfigCollection 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 MemoryConfigCollection, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class MemoryConfigCollection implements MutableConfigCollectionInterface, Serializable |
||
| 14 | { |
||
| 15 | use MiddlewareAware; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Stores a list of key/value config prior to middleware being applied |
||
| 19 | * |
||
| 20 | * @var array |
||
| 21 | */ |
||
| 22 | protected $config = []; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Call cache for non-trivial config calls including middleware |
||
| 26 | * |
||
| 27 | * @var array |
||
| 28 | */ |
||
| 29 | protected $callCache = []; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var array |
||
| 33 | */ |
||
| 34 | protected $metadata = []; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | protected $history = []; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var boolean |
||
| 43 | */ |
||
| 44 | protected $trackMetadata = false; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * ConfigCollection constructor. |
||
| 48 | * |
||
| 49 | * @param bool $trackMetadata |
||
| 50 | */ |
||
| 51 | 25 | public function __construct($trackMetadata = false) |
|
| 55 | |||
| 56 | /** |
||
| 57 | * @return static |
||
| 58 | */ |
||
| 59 | public static function create() |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Trigger transformers to load into this store |
||
| 66 | * |
||
| 67 | * @param TransformerInterface[] $transformers |
||
| 68 | * @return $this |
||
| 69 | */ |
||
| 70 | 16 | public function transform($transformers) |
|
| 77 | |||
| 78 | 22 | public function set($class, $name, $data, $metadata = []) |
|
| 101 | |||
| 102 | 23 | public function get($class, $name = null, $excludeMiddleware = 0) |
|
| 118 | |||
| 119 | /** |
||
| 120 | * Retrieve config for an entire class |
||
| 121 | * |
||
| 122 | * @param string $class Name of class |
||
| 123 | * @param int|true $excludeMiddleware Optional flag of middleware to disable. |
||
| 124 | * Passing in `true` disables all middleware. |
||
| 125 | * Can also pass in int flags to specify specific middlewares. |
||
| 126 | * @return array|null |
||
| 127 | */ |
||
| 128 | 23 | protected function getClassConfig($class, $excludeMiddleware = 0) |
|
| 162 | |||
| 163 | 2 | public function exists($class, $name = null, $excludeMiddleware = 0) |
|
| 174 | |||
| 175 | 1 | public function remove($class, $name = null) |
|
| 187 | |||
| 188 | 1 | public function removeAll() |
|
| 195 | |||
| 196 | /** |
||
| 197 | * Get complete config (excludes middleware-applied config) |
||
| 198 | * |
||
| 199 | * @return array |
||
| 200 | */ |
||
| 201 | public function getAll() |
||
| 205 | |||
| 206 | /** |
||
| 207 | * @deprecated 4.0...5.0 |
||
| 208 | * |
||
| 209 | * Synonym for merge() |
||
| 210 | * |
||
| 211 | * @param string $class |
||
| 212 | * @param string $name |
||
| 213 | * @param mixed $value |
||
| 214 | * @return $this |
||
| 215 | */ |
||
| 216 | public function update($class, $name, $value) |
||
| 225 | |||
| 226 | public function merge($class, $name, $value) |
||
| 238 | |||
| 239 | 8 | public function getMetadata() |
|
| 247 | |||
| 248 | 2 | public function getHistory() |
|
| 256 | |||
| 257 | public function serialize() |
||
| 269 | |||
| 270 | public function unserialize($serialized) |
||
| 281 | |||
| 282 | public function nest() |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Save metadata for the given class |
||
| 289 | * |
||
| 290 | * @param string $class |
||
| 291 | * @param array $metadata |
||
| 292 | */ |
||
| 293 | 22 | protected function saveMetadata($class, $metadata) |
|
| 314 | } |
||
| 315 |