Complex classes like Context 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 Context, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class Context implements \ArrayAccess |
||
23 | { |
||
24 | /** |
||
25 | * Array with current page information's. |
||
26 | * |
||
27 | * @var Meta |
||
28 | */ |
||
29 | protected $currentPage; |
||
30 | |||
31 | /** |
||
32 | * Array will all registered meta types. |
||
33 | * |
||
34 | * @var Meta[] |
||
35 | */ |
||
36 | protected $registeredMeta = []; |
||
37 | |||
38 | /** |
||
39 | * Array with available meta configs. |
||
40 | * |
||
41 | * @var array |
||
42 | */ |
||
43 | protected $availableConfigs = []; |
||
44 | |||
45 | /** |
||
46 | * @var Cache |
||
47 | */ |
||
48 | protected $metadataCache; |
||
49 | |||
50 | /** |
||
51 | * @var string |
||
52 | */ |
||
53 | protected $configsPath; |
||
54 | |||
55 | /** |
||
56 | * @var array |
||
57 | */ |
||
58 | private $temporaryMeta = []; |
||
59 | |||
60 | /** |
||
61 | * @var bool |
||
62 | */ |
||
63 | private $previewMode = false; |
||
64 | |||
65 | /** |
||
66 | * @var array |
||
67 | */ |
||
68 | private $supportedCache = []; |
||
69 | |||
70 | /** |
||
71 | * @var array |
||
72 | */ |
||
73 | private $configurationCache = []; |
||
74 | |||
75 | /** |
||
76 | * Context constructor. |
||
77 | * |
||
78 | * @param Cache $metadataCache |
||
79 | * @param string $configsPath |
||
80 | */ |
||
81 | public function __construct(Cache $metadataCache, $configsPath = null) |
||
86 | |||
87 | /** |
||
88 | * @return array |
||
89 | */ |
||
90 | public function getAvailableConfigs() |
||
98 | |||
99 | /** |
||
100 | * @param array $configuration |
||
101 | * |
||
102 | * @return bool |
||
103 | */ |
||
104 | public function addAvailableConfig(array $configuration) |
||
114 | |||
115 | /** |
||
116 | * @param array $availableConfigs |
||
117 | * |
||
118 | * @return Context |
||
119 | */ |
||
120 | public function setAvailableConfigs(array $availableConfigs) |
||
126 | |||
127 | /** |
||
128 | * @param string $configsPath |
||
129 | */ |
||
130 | public function loadConfigsFromPath($configsPath) |
||
149 | |||
150 | /** |
||
151 | * @param mixed $value |
||
152 | * |
||
153 | * @return array |
||
154 | * |
||
155 | * @throws \Exception |
||
156 | */ |
||
157 | public function getConfigurationForValue($value) |
||
178 | |||
179 | /** |
||
180 | * @param mixed $value |
||
181 | * |
||
182 | * @return Meta |
||
183 | */ |
||
184 | public function getMetaForValue($value) |
||
188 | |||
189 | /** |
||
190 | * @param mixed $value |
||
191 | * |
||
192 | * @return bool |
||
193 | */ |
||
194 | public function isSupported($value) |
||
210 | |||
211 | /** |
||
212 | * @param string $filePath |
||
213 | * |
||
214 | * @return $this |
||
215 | */ |
||
216 | public function addNewConfig($filePath) |
||
235 | |||
236 | /** |
||
237 | * Set current context page information's. |
||
238 | * |
||
239 | * @param Meta $currentPage |
||
240 | * |
||
241 | * @return self |
||
242 | */ |
||
243 | public function setCurrentPage(Meta $currentPage) |
||
249 | |||
250 | /** |
||
251 | * Get current context page information's. |
||
252 | * |
||
253 | * @return Meta |
||
254 | */ |
||
255 | public function getCurrentPage() |
||
259 | |||
260 | /** |
||
261 | * Register new meta type, registration is required before setting new value for meta. |
||
262 | * |
||
263 | * @param Meta|null $meta Meta object |
||
264 | * |
||
265 | * @throws \Exception if already registered |
||
266 | * |
||
267 | * @return bool if registered successfully |
||
268 | */ |
||
269 | public function registerMeta(Meta $meta = null) |
||
284 | |||
285 | /** |
||
286 | * @return Meta[] |
||
287 | */ |
||
288 | public function getRegisteredMeta() |
||
292 | |||
293 | /** |
||
294 | * @return bool |
||
295 | */ |
||
296 | public function isPreviewMode(): bool |
||
300 | |||
301 | /** |
||
302 | * @param bool $previewMode |
||
303 | */ |
||
304 | public function setPreviewMode(bool $previewMode) |
||
308 | |||
309 | /** |
||
310 | * {@inheritdoc} |
||
311 | */ |
||
312 | public function offsetSet($name, $meta) |
||
320 | |||
321 | /** |
||
322 | * {@inheritdoc} |
||
323 | */ |
||
324 | public function offsetExists($name) |
||
328 | |||
329 | /** |
||
330 | * {@inheritdoc} |
||
331 | */ |
||
332 | public function offsetUnset($name) |
||
338 | |||
339 | /** |
||
340 | * {@inheritdoc} |
||
341 | */ |
||
342 | public function offsetGet($name) |
||
350 | |||
351 | /** |
||
352 | * @param array $keys |
||
353 | * |
||
354 | * @return string |
||
355 | */ |
||
356 | public function temporaryUnset(array $keys) |
||
380 | |||
381 | /** |
||
382 | * @param string $id |
||
383 | * |
||
384 | * @return null|true |
||
385 | */ |
||
386 | public function restoreTemporaryUnset($id) |
||
399 | |||
400 | /** |
||
401 | * Resets context data. |
||
402 | */ |
||
403 | public function reset() |
||
410 | } |
||
411 |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: