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) |
||
139 | |||
140 | /** |
||
141 | * @param mixed $value |
||
142 | * |
||
143 | * @return array |
||
144 | * |
||
145 | * @throws \Exception |
||
146 | */ |
||
147 | public function getConfigurationForValue($value) |
||
161 | |||
162 | /** |
||
163 | * @param mixed $value |
||
164 | * |
||
165 | * @return Meta |
||
166 | */ |
||
167 | public function getMetaForValue($value) |
||
171 | |||
172 | /** |
||
173 | * @param mixed $value |
||
174 | * |
||
175 | * @return bool |
||
176 | */ |
||
177 | public function isSupported($value) |
||
185 | |||
186 | /** |
||
187 | * @param string $filePath |
||
188 | * |
||
189 | * @return $this |
||
190 | */ |
||
191 | public function addNewConfig($filePath) |
||
209 | |||
210 | /** |
||
211 | * Set current context page information's. |
||
212 | * |
||
213 | * @param Meta $currentPage |
||
214 | * |
||
215 | * @return self |
||
216 | */ |
||
217 | public function setCurrentPage(Meta $currentPage) |
||
223 | |||
224 | /** |
||
225 | * Get current context page information's. |
||
226 | * |
||
227 | * @return Meta |
||
228 | */ |
||
229 | public function getCurrentPage() |
||
233 | |||
234 | /** |
||
235 | * Register new meta type, registration is required before setting new value for meta. |
||
236 | * |
||
237 | * @param Meta|null $meta Meta object |
||
238 | * |
||
239 | * @throws \Exception if already registered |
||
240 | * |
||
241 | * @return bool if registered successfully |
||
242 | */ |
||
243 | public function registerMeta(Meta $meta = null) |
||
259 | |||
260 | /** |
||
261 | * @return Meta[] |
||
262 | */ |
||
263 | public function getRegisteredMeta() |
||
267 | |||
268 | /** |
||
269 | * @return bool |
||
270 | */ |
||
271 | public function isPreviewMode(): bool |
||
275 | |||
276 | /** |
||
277 | * @param bool $previewMode |
||
278 | */ |
||
279 | public function setPreviewMode(bool $previewMode) |
||
283 | |||
284 | /** |
||
285 | * {@inheritdoc} |
||
286 | */ |
||
287 | public function offsetSet($name, $meta) |
||
295 | |||
296 | /** |
||
297 | * {@inheritdoc} |
||
298 | */ |
||
299 | public function offsetExists($name) |
||
303 | |||
304 | /** |
||
305 | * {@inheritdoc} |
||
306 | */ |
||
307 | public function offsetUnset($name) |
||
313 | |||
314 | /** |
||
315 | * {@inheritdoc} |
||
316 | */ |
||
317 | public function offsetGet($name) |
||
325 | |||
326 | /** |
||
327 | * @param array $keys |
||
328 | * |
||
329 | * @return string |
||
330 | */ |
||
331 | public function temporaryUnset(array $keys) |
||
355 | |||
356 | /** |
||
357 | * @param string $id |
||
358 | * |
||
359 | * @return null|true |
||
360 | */ |
||
361 | public function restoreTemporaryUnset($id) |
||
374 | } |
||
375 |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: