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 | 128 | * @var array |
|
| 67 | */ |
||
| 68 | 128 | private $supportedCache = []; |
|
| 69 | 128 | ||
| 70 | 128 | /** |
|
| 71 | * @var array |
||
| 72 | */ |
||
| 73 | private $configurationCache = []; |
||
| 74 | |||
| 75 | 20 | /** |
|
| 76 | * Context constructor. |
||
| 77 | 20 | * |
|
| 78 | 19 | * @param Cache $metadataCache |
|
| 79 | * @param string $configsPath |
||
| 80 | */ |
||
| 81 | 20 | public function __construct(Cache $metadataCache, $configsPath = null) |
|
| 86 | |||
| 87 | /** |
||
| 88 | * @return array |
||
| 89 | 20 | */ |
|
| 90 | public function getAvailableConfigs() |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @param array $configuration |
||
| 101 | * |
||
| 102 | * @return bool |
||
| 103 | */ |
||
| 104 | public function addAvailableConfig(array $configuration) |
||
| 114 | |||
| 115 | 19 | /** |
|
| 116 | * @param array $availableConfigs |
||
| 117 | 19 | * |
|
| 118 | 18 | * @return Context |
|
| 119 | 18 | */ |
|
| 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 | 10 | * |
|
| 155 | * @throws \Exception |
||
| 156 | 10 | */ |
|
| 157 | public function getConfigurationForValue($value) |
||
| 178 | 20 | ||
| 179 | /** |
||
| 180 | 20 | * @param mixed $value |
|
| 181 | 20 | * |
|
| 182 | 20 | * @return Meta |
|
| 183 | */ |
||
| 184 | public function getMetaForValue($value) |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @param mixed $value |
||
| 191 | * |
||
| 192 | 20 | * @return bool |
|
| 193 | */ |
||
| 194 | 20 | public function isSupported($value) |
|
| 210 | |||
| 211 | /** |
||
| 212 | * @param string $filePath |
||
| 213 | * |
||
| 214 | * @return $this |
||
| 215 | */ |
||
| 216 | 2 | public function addNewConfig($filePath) |
|
| 235 | 20 | ||
| 236 | /** |
||
| 237 | 20 | * Set current context page information's. |
|
| 238 | 20 | * |
|
| 239 | * @param Meta $currentPage |
||
| 240 | * |
||
| 241 | 20 | * @return self |
|
| 242 | */ |
||
| 243 | public function setCurrentPage(Meta $currentPage) |
||
| 249 | |||
| 250 | 3 | /** |
|
| 251 | * Get current context page information's. |
||
| 252 | 3 | * |
|
| 253 | * @return Meta |
||
| 254 | */ |
||
| 255 | public function getCurrentPage() |
||
| 259 | |||
| 260 | 20 | /** |
|
| 261 | 20 | * Register new meta type, registration is required before setting new value for meta. |
|
| 262 | * |
||
| 263 | * @param Meta|null $meta Meta object |
||
| 264 | 20 | * |
|
| 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 | 10 | public function getRegisteredMeta() |
|
| 292 | |||
| 293 | /** |
||
| 294 | * @return bool |
||
| 295 | */ |
||
| 296 | public function isPreviewMode(): bool |
||
| 300 | |||
| 301 | /** |
||
| 302 | 1 | * @param bool $previewMode |
|
| 303 | */ |
||
| 304 | 1 | public function setPreviewMode(bool $previewMode) |
|
| 308 | |||
| 309 | /** |
||
| 310 | * {@inheritdoc} |
||
| 311 | */ |
||
| 312 | public function offsetSet($name, $meta) |
||
| 320 | |||
| 321 | /** |
||
| 322 | 1 | * {@inheritdoc} |
|
| 323 | */ |
||
| 324 | 1 | public function offsetExists($name) |
|
| 328 | |||
| 329 | /** |
||
| 330 | * {@inheritdoc} |
||
| 331 | */ |
||
| 332 | 1 | public function offsetUnset($name) |
|
| 338 | |||
| 339 | 1 | /** |
|
| 340 | 1 | * {@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: