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) |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @return Meta[] |
||
| 261 | */ |
||
| 262 | public function getRegisteredMeta() |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @return bool |
||
| 269 | */ |
||
| 270 | public function isPreviewMode(): bool |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @param bool $previewMode |
||
| 277 | */ |
||
| 278 | public function setPreviewMode(bool $previewMode) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * {@inheritdoc} |
||
| 285 | */ |
||
| 286 | public function offsetSet($name, $meta) |
||
| 294 | |||
| 295 | /** |
||
| 296 | * {@inheritdoc} |
||
| 297 | */ |
||
| 298 | public function offsetExists($name) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * {@inheritdoc} |
||
| 305 | */ |
||
| 306 | public function offsetUnset($name) |
||
| 312 | |||
| 313 | /** |
||
| 314 | * {@inheritdoc} |
||
| 315 | */ |
||
| 316 | public function offsetGet($name) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * @param array $keys |
||
| 327 | * |
||
| 328 | * @return string |
||
| 329 | */ |
||
| 330 | public function temporaryUnset(array $keys) |
||
| 354 | |||
| 355 | /** |
||
| 356 | * @param string $id |
||
| 357 | * |
||
| 358 | * @return null|true |
||
| 359 | */ |
||
| 360 | public function restoreTemporaryUnset($id) |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Resets context data. |
||
| 376 | */ |
||
| 377 | public function reset() |
||
| 384 | } |
||
| 385 |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: