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 | * Context constructor. |
||
67 | * |
||
68 | * @param Cache $metadataCache |
||
69 | * @param string $configsPath |
||
70 | */ |
||
71 | public function __construct(Cache $metadataCache, $configsPath = null) |
||
76 | |||
77 | /** |
||
78 | * @return array |
||
79 | */ |
||
80 | public function getAvailableConfigs() |
||
88 | |||
89 | /** |
||
90 | * @param array $configuration |
||
91 | * |
||
92 | * @return bool |
||
93 | */ |
||
94 | public function addAvailableConfig(array $configuration) |
||
104 | |||
105 | /** |
||
106 | * @param array $availableConfigs |
||
107 | * |
||
108 | * @return Context |
||
109 | */ |
||
110 | public function setAvailableConfigs(array $availableConfigs) |
||
116 | |||
117 | /** |
||
118 | * @param string $configsPath |
||
119 | */ |
||
120 | public function loadConfigsFromPath($configsPath) |
||
131 | |||
132 | /** |
||
133 | * @param mixed $value |
||
134 | * |
||
135 | * @return array |
||
136 | * |
||
137 | * @throws \Exception |
||
138 | */ |
||
139 | public function getConfigurationForValue($value) |
||
153 | |||
154 | /** |
||
155 | * @param mixed $value |
||
156 | * |
||
157 | * @return Meta |
||
158 | */ |
||
159 | public function getMetaForValue($value) |
||
163 | |||
164 | /** |
||
165 | * @param mixed $value |
||
166 | * |
||
167 | * @return bool |
||
168 | */ |
||
169 | public function isSupported($value) |
||
177 | |||
178 | /** |
||
179 | * @param string $filePath |
||
180 | * |
||
181 | * @return $this |
||
182 | */ |
||
183 | public function addNewConfig($filePath) |
||
201 | |||
202 | /** |
||
203 | * Set current context page information's. |
||
204 | * |
||
205 | * @param Meta $currentPage |
||
206 | * |
||
207 | * @return self |
||
208 | */ |
||
209 | public function setCurrentPage(Meta $currentPage) |
||
215 | |||
216 | /** |
||
217 | * Get current context page information's. |
||
218 | * |
||
219 | * @return Meta |
||
220 | */ |
||
221 | public function getCurrentPage() |
||
225 | |||
226 | /** |
||
227 | * Register new meta type, registration is required before setting new value for meta. |
||
228 | * |
||
229 | * @param Meta|null $meta Meta object |
||
230 | * |
||
231 | * @throws \Exception if already registered |
||
232 | * |
||
233 | * @return bool if registered successfully |
||
234 | */ |
||
235 | public function registerMeta(Meta $meta = null) |
||
251 | |||
252 | /** |
||
253 | * @return Meta[] |
||
254 | */ |
||
255 | public function getRegisteredMeta() |
||
259 | |||
260 | /** |
||
261 | * @return bool |
||
262 | */ |
||
263 | public function isPreviewMode(): bool |
||
267 | |||
268 | /** |
||
269 | * @param bool $previewMode |
||
270 | */ |
||
271 | public function setPreviewMode(bool $previewMode) |
||
275 | |||
276 | /** |
||
277 | * {@inheritdoc} |
||
278 | */ |
||
279 | public function offsetSet($name, $meta) |
||
287 | |||
288 | /** |
||
289 | * {@inheritdoc} |
||
290 | */ |
||
291 | public function offsetExists($name) |
||
295 | |||
296 | /** |
||
297 | * {@inheritdoc} |
||
298 | */ |
||
299 | public function offsetUnset($name) |
||
305 | |||
306 | /** |
||
307 | * {@inheritdoc} |
||
308 | */ |
||
309 | public function offsetGet($name) |
||
317 | |||
318 | /** |
||
319 | * @param array $keys |
||
320 | * |
||
321 | * @return string |
||
322 | */ |
||
323 | public function temporaryUnset(array $keys) |
||
347 | |||
348 | /** |
||
349 | * @param string $id |
||
350 | * |
||
351 | * @return null|true |
||
352 | */ |
||
353 | public function restoreTemporaryUnset($id) |
||
366 | } |
||
367 |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: