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 | 117 | private function __construct(array $config) |
|
| 20 | |||
| 21 | 117 | public static function from(array $config) |
|
| 25 | |||
| 26 | 98 | public function get($key, $default = null) |
|
| 34 | |||
| 35 | 1 | public function all(): array |
|
| 39 | |||
| 40 | 105 | private function sanitize(array $config): array |
|
| 54 | |||
| 55 | 105 | private function computeCanonicals(array $config): array |
|
| 75 | |||
| 76 | /** |
||
| 77 | * @param array $locales |
||
| 78 | * |
||
| 79 | * @return array |
||
| 80 | */ |
||
| 81 | 105 | 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 | 105 | private function removeTrailingDomainSlashes(array $locales) |
|
| 114 | |||
| 115 | /** |
||
| 116 | * @param array $locales |
||
| 117 | * |
||
| 118 | * @return array |
||
| 119 | */ |
||
| 120 | 105 | private function convertSingleEntryToDefault(array $locales): array |
|
| 131 | |||
| 132 | /** |
||
| 133 | * @param array $config |
||
| 134 | */ |
||
| 135 | 117 | private function validate(array $config) |
|
| 136 | { |
||
| 137 | 117 | if (!isset($config['locales'])) { |
|
| 138 | 2 | throw new InvalidConfig('Value [Locales] is missing for config structure.'); |
|
| 139 | } |
||
| 140 | |||
| 141 | 115 | $locales = $config['locales']; |
|
| 142 | |||
| 143 | 115 | if (!isset($locales['*'])) { |
|
| 144 | 8 | throw new InvalidConfig('Default group [*] is missing for locales structure.'); |
|
| 145 | } |
||
| 146 | |||
| 147 | 107 | if (is_array($locales['*']) && !isset($locales['*']['/'])) { |
|
| 148 | 1 | throw new InvalidConfig('Group [default] is missing the default locale. e.g. ["/" => "en"]'); |
|
| 149 | } |
||
| 150 | |||
| 151 | 106 | foreach ($locales as $group => $segments) { |
|
| 152 | 106 | if (!is_string($group)) { |
|
| 153 | throw new InvalidConfig('Invalid config structure for locales group ['.$group.']'); |
||
| 154 | } |
||
| 155 | } |
||
| 156 | |||
| 157 | 106 | $this->validateEachCanonicalLocaleExists($config); |
|
| 158 | 105 | } |
|
| 159 | |||
| 160 | /** |
||
| 161 | * Each custom canonical entry should point to an existing locale. |
||
| 162 | * |
||
| 163 | * @param array $config |
||
| 164 | */ |
||
| 165 | 106 | private function validateEachCanonicalLocaleExists(array $config) |
|
| 174 | |||
| 175 | 85 | private function existsAsLocale($existing_locales, $locale): bool |
|
| 195 | |||
| 196 | 73 | public function offsetExists($offset) |
|
| 204 | |||
| 205 | 72 | public function offsetGet($offset) |
|
| 209 | |||
| 210 | 1 | public function offsetSet($offset, $value) |
|
| 218 | |||
| 219 | 1 | public function offsetUnset($offset) |
|
| 223 | |||
| 224 | 82 | public function toArray(): array |
|
| 228 | } |
||
| 229 |