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 |
||
| 24 | class Context implements \ArrayAccess |
||
| 25 | { |
||
| 26 | const META_EVENT_NAME = 'swp.templates_system.meta.load'; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Array with current page information's. |
||
| 30 | * |
||
| 31 | * @var Meta |
||
| 32 | */ |
||
| 33 | protected $currentPage; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Array will all registered meta types. |
||
| 37 | * |
||
| 38 | * @var Meta[] |
||
| 39 | */ |
||
| 40 | protected $registeredMeta = []; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Array with available meta configs. |
||
| 44 | * |
||
| 45 | * @var array |
||
| 46 | */ |
||
| 47 | protected $availableConfigs = []; |
||
| 48 | |||
| 49 | protected $dispatcher; |
||
| 50 | |||
| 51 | protected $metadataCache; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | protected $configsPath; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var array |
||
| 60 | */ |
||
| 61 | private $temporaryMeta = []; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var bool |
||
| 65 | */ |
||
| 66 | private $previewMode = false; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var array |
||
| 70 | */ |
||
| 71 | private $supportedCache = []; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var array |
||
| 75 | */ |
||
| 76 | private $configurationCache = []; |
||
| 77 | |||
| 78 | public function __construct(EventDispatcherInterface $dispatcher, Cache $metadataCache, $configsPath = null) |
||
| 79 | { |
||
| 80 | $this->metadataCache = $metadataCache; |
||
| 81 | $this->configsPath = $configsPath; |
||
| 82 | $this->dispatcher = $dispatcher; |
||
| 83 | } |
||
| 84 | |||
| 85 | public function dispatchMetaEvent(MetaEvent $event): void |
||
| 86 | { |
||
| 87 | $this->dispatcher->dispatch(self::META_EVENT_NAME, $event); |
||
| 88 | } |
||
| 89 | |||
| 90 | public function getAvailableConfigs(): array |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @param array $configuration |
||
| 101 | * |
||
| 102 | * @return bool |
||
| 103 | */ |
||
| 104 | public function addAvailableConfig(array $configuration) |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @param array $availableConfigs |
||
| 117 | * |
||
| 118 | * @return Context |
||
| 119 | */ |
||
| 120 | public function setAvailableConfigs(array $availableConfigs) |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @param string $configsPath |
||
| 129 | */ |
||
| 130 | public function loadConfigsFromPath($configsPath) |
||
| 150 | |||
| 151 | public function getConfigurationForValue($value): array |
||
| 152 | { |
||
| 172 | |||
| 173 | public function getMetaForValue($value): Meta |
||
| 177 | |||
| 178 | public function isSupported($value) |
||
| 194 | |||
| 195 | public function addNewConfig(string $filePath) |
||
| 214 | |||
| 215 | public function setCurrentPage(Meta $currentPage): self |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Get current context page information's. |
||
| 224 | * |
||
| 225 | * @return Meta |
||
| 226 | */ |
||
| 227 | public function getCurrentPage() |
||
| 231 | |||
| 232 | public function registerMeta(Meta $meta = null) |
||
| 247 | |||
| 248 | /** |
||
| 249 | * @return Meta[] |
||
| 250 | */ |
||
| 251 | public function getRegisteredMeta() |
||
| 255 | |||
| 256 | /** |
||
| 257 | * @return bool |
||
| 258 | */ |
||
| 259 | public function isPreviewMode(): bool |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @param bool $previewMode |
||
| 266 | */ |
||
| 267 | public function setPreviewMode(bool $previewMode) |
||
| 271 | |||
| 272 | /** |
||
| 273 | * {@inheritdoc} |
||
| 274 | */ |
||
| 275 | public function offsetSet($name, $meta) |
||
| 283 | |||
| 284 | /** |
||
| 285 | * {@inheritdoc} |
||
| 286 | */ |
||
| 287 | public function offsetExists($name) |
||
| 291 | |||
| 292 | /** |
||
| 293 | * {@inheritdoc} |
||
| 294 | */ |
||
| 295 | public function offsetUnset($name) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * {@inheritdoc} |
||
| 304 | */ |
||
| 305 | public function offsetGet($name) |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @param array $keys |
||
| 316 | * |
||
| 317 | * @return string |
||
| 318 | */ |
||
| 319 | public function temporaryUnset(array $keys) |
||
| 343 | |||
| 344 | /** |
||
| 345 | * @param string $id |
||
| 346 | * |
||
| 347 | * @return null|true |
||
| 348 | */ |
||
| 349 | public function restoreTemporaryUnset($id) |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Resets context data. |
||
| 365 | */ |
||
| 366 | public function reset() |
||
| 373 | } |
||
| 374 |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: