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 | 31 | 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 | 18 | public function transform($transformers) |
|
77 | |||
78 | 28 | public function set($class, $name, $data, $metadata = []) |
|
96 | |||
97 | 28 | public function get($class, $name = null, $excludeMiddleware = 0) |
|
112 | |||
113 | /** |
||
114 | * Retrieve config for an entire class |
||
115 | * |
||
116 | * @param string $class Name of class |
||
117 | * @param int|true $excludeMiddleware Optional flag of middleware to disable. |
||
118 | * Passing in `true` disables all middleware. |
||
119 | * Can also pass in int flags to specify specific middlewares. |
||
120 | * @return array |
||
121 | */ |
||
122 | 28 | protected function getClassConfig($class, $excludeMiddleware = 0) |
|
151 | |||
152 | 2 | public function exists($class, $name = null, $excludeMiddleware = 0) |
|
163 | |||
164 | 1 | public function remove($class, $name = null) |
|
176 | |||
177 | 1 | public function removeAll() |
|
184 | |||
185 | /** |
||
186 | * Get complete config (excludes middleware-applied config) |
||
187 | * |
||
188 | * @return array |
||
189 | */ |
||
190 | public function getAll() |
||
194 | |||
195 | /** |
||
196 | * @deprecated 4.0...5.0 |
||
197 | * |
||
198 | * Synonym for merge() |
||
199 | * |
||
200 | * @param string $class |
||
201 | * @param string $name |
||
202 | * @param mixed $value |
||
203 | * @return $this |
||
204 | */ |
||
205 | public function update($class, $name, $value) |
||
210 | |||
211 | public function merge($class, $name, $value) |
||
223 | |||
224 | 8 | public function getMetadata() |
|
232 | |||
233 | 2 | public function getHistory() |
|
241 | |||
242 | /** |
||
243 | * Get list of serialized properties |
||
244 | * |
||
245 | * @return array |
||
246 | */ |
||
247 | protected function getSerializedMembers() |
||
254 | |||
255 | public function serialize() |
||
264 | |||
265 | public function unserialize($serialized) |
||
272 | |||
273 | public function nest() |
||
277 | |||
278 | /** |
||
279 | * Save metadata for the given class |
||
280 | * |
||
281 | * @param string $class |
||
282 | * @param array $metadata |
||
283 | */ |
||
284 | 28 | protected function saveMetadata($class, $metadata) |
|
307 | } |
||
308 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: