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 | * Context constructor. |
||
62 | * |
||
63 | * @param Cache $metadataCache |
||
64 | * @param string $configsPath |
||
65 | */ |
||
66 | 126 | public function __construct(Cache $metadataCache, $configsPath = null) |
|
71 | |||
72 | /** |
||
73 | * @return array |
||
74 | */ |
||
75 | 20 | public function getAvailableConfigs() |
|
83 | |||
84 | /** |
||
85 | * @param array $configuration |
||
86 | * |
||
87 | * @return bool |
||
88 | */ |
||
89 | 20 | public function addAvailableConfig(array $configuration) |
|
99 | |||
100 | /** |
||
101 | * @param array $availableConfigs |
||
102 | * |
||
103 | * @return Context |
||
104 | */ |
||
105 | public function setAvailableConfigs(array $availableConfigs) |
||
111 | |||
112 | /** |
||
113 | * @param string $configsPath |
||
114 | */ |
||
115 | 19 | public function loadConfigsFromPath($configsPath) |
|
126 | |||
127 | /** |
||
128 | * @param mixed $value |
||
129 | * |
||
130 | * @return array |
||
131 | * |
||
132 | * @throws \Exception |
||
133 | */ |
||
134 | 18 | public function getConfigurationForValue($value) |
|
148 | |||
149 | /** |
||
150 | * @param mixed $value |
||
151 | * |
||
152 | * @return Meta |
||
153 | */ |
||
154 | 10 | public function getMetaForValue($value) |
|
158 | |||
159 | /** |
||
160 | * @param mixed $value |
||
161 | * |
||
162 | * @return bool |
||
163 | */ |
||
164 | 20 | public function isSupported($value) |
|
172 | |||
173 | /** |
||
174 | * @param string $filePath |
||
175 | * |
||
176 | * @return $this |
||
177 | */ |
||
178 | 20 | public function addNewConfig($filePath) |
|
196 | |||
197 | /** |
||
198 | * Set current context page information's. |
||
199 | * |
||
200 | * @param Meta $currentPage |
||
201 | * |
||
202 | * @return self |
||
203 | */ |
||
204 | 13 | public function setCurrentPage(Meta $currentPage) |
|
210 | |||
211 | /** |
||
212 | * Get current context page information's. |
||
213 | * |
||
214 | * @return Meta |
||
215 | */ |
||
216 | 2 | public function getCurrentPage() |
|
220 | |||
221 | /** |
||
222 | * Register new meta type, registration is required before setting new value for meta. |
||
223 | * |
||
224 | * @param Meta|null $meta Meta object |
||
225 | * |
||
226 | * @throws \Exception if already registered |
||
227 | * |
||
228 | * @return bool if registered successfully |
||
229 | */ |
||
230 | 20 | public function registerMeta(Meta $meta = null) |
|
246 | |||
247 | /** |
||
248 | * @return Meta[] |
||
249 | */ |
||
250 | 3 | public function getRegisteredMeta() |
|
254 | |||
255 | /** |
||
256 | * {@inheritdoc} |
||
257 | */ |
||
258 | 20 | public function offsetSet($name, $meta) |
|
266 | |||
267 | /** |
||
268 | * {@inheritdoc} |
||
269 | */ |
||
270 | 11 | public function offsetExists($name) |
|
274 | |||
275 | /** |
||
276 | * {@inheritdoc} |
||
277 | */ |
||
278 | 1 | public function offsetUnset($name) |
|
284 | |||
285 | /** |
||
286 | * {@inheritdoc} |
||
287 | */ |
||
288 | 10 | public function offsetGet($name) |
|
296 | |||
297 | /** |
||
298 | * @param array $keys |
||
299 | * |
||
300 | * @return string |
||
301 | */ |
||
302 | 1 | public function temporaryUnset(array $keys) |
|
326 | |||
327 | /** |
||
328 | * @param $id |
||
329 | * |
||
330 | * @return null|true |
||
331 | */ |
||
332 | 1 | public function restoreTemporaryUnset($id) |
|
345 | } |
||
346 |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: