Complex classes like Config 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 Config, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | class Config implements \ArrayAccess |
||
|
|
|||
| 8 | { |
||
| 9 | /** |
||
| 10 | * @var array |
||
| 11 | */ |
||
| 12 | private $config; |
||
| 13 | |||
| 14 | 113 | private function __construct(array $config) |
|
| 20 | |||
| 21 | 113 | public static function from(array $config) |
|
| 25 | |||
| 26 | 94 | public function get($key, $default = null) |
|
| 27 | { |
||
| 28 | 94 | if (!isset($this->config[$key])) { |
|
| 29 | 2 | return $default; |
|
| 30 | } |
||
| 31 | |||
| 32 | 92 | return $this->config[$key]; |
|
| 33 | } |
||
| 34 | |||
| 35 | 1 | public function all(): array |
|
| 39 | |||
| 40 | 101 | private function sanitize(array $config): array |
|
| 54 | |||
| 55 | 101 | private function computeCanonicals(array $config): array |
|
| 75 | |||
| 76 | /** |
||
| 77 | * @param array $locales |
||
| 78 | * |
||
| 79 | * @return array |
||
| 80 | */ |
||
| 81 | 101 | private function removeSlashes(array $locales): array |
|
| 97 | |||
| 98 | /** |
||
| 99 | * e.g. example.com/ will be sanitized to example.com. |
||
| 100 | * |
||
| 101 | * @param array $locales |
||
| 102 | * |
||
| 103 | * @return array |
||
| 104 | */ |
||
| 105 | 101 | private function removeTrailingDomainSlashes(array $locales) |
|
| 114 | |||
| 115 | /** |
||
| 116 | * @param array $locales |
||
| 117 | * |
||
| 118 | * @return array |
||
| 119 | */ |
||
| 120 | 101 | private function convertSingleEntryToDefault(array $locales): array |
|
| 131 | |||
| 132 | /** |
||
| 133 | * @param array $config |
||
| 134 | */ |
||
| 135 | 113 | private function validate(array $config) |
|
| 159 | |||
| 160 | /** |
||
| 161 | * Each custom canonical entry should point to an existing locale. |
||
| 162 | * |
||
| 163 | * @param array $config |
||
| 164 | */ |
||
| 165 | 102 | private function validateEachCanonicalLocaleExists(array $config) |
|
| 174 | |||
| 175 | 81 | private function existsAsLocale($existing_locales, $locale): bool |
|
| 195 | |||
| 196 | 69 | public function offsetExists($offset) |
|
| 204 | |||
| 205 | 68 | public function offsetGet($offset) |
|
| 209 | |||
| 210 | 1 | public function offsetSet($offset, $value) |
|
| 218 | |||
| 219 | 1 | public function offsetUnset($offset) |
|
| 223 | |||
| 224 | 78 | public function toArray(): array |
|
| 228 | } |
||
| 229 |