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 | 101 | private function __construct(array $config) |
|
| 20 | |||
| 21 | 101 | public static function from(array $config) |
|
| 25 | |||
| 26 | 83 | public function get($key) |
|
| 34 | |||
| 35 | 1 | public function all(): array |
|
| 39 | |||
| 40 | 89 | private function sanitize(array $config): array |
|
| 54 | |||
| 55 | 89 | private function computeCanonicals(array $config): array |
|
| 75 | |||
| 76 | /** |
||
| 77 | * @param array $locales |
||
| 78 | * |
||
| 79 | * @return array |
||
| 80 | */ |
||
| 81 | 89 | 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 | 89 | private function removeTrailingDomainSlashes(array $locales) |
|
| 114 | |||
| 115 | /** |
||
| 116 | * @param array $locales |
||
| 117 | * |
||
| 118 | * @return array |
||
| 119 | */ |
||
| 120 | 89 | private function convertSingleEntryToDefault(array $locales): array |
|
| 131 | |||
| 132 | /** |
||
| 133 | * @param array $config |
||
| 134 | */ |
||
| 135 | 101 | 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 | 90 | private function validateEachCanonicalLocaleExists(array $config) |
|
| 174 | |||
| 175 | 18 | private function existsAsLocale($existing_locales, $locale): bool |
|
| 189 | |||
| 190 | 65 | public function offsetExists($offset) |
|
| 198 | |||
| 199 | 64 | public function offsetGet($offset) |
|
| 203 | |||
| 204 | 1 | public function offsetSet($offset, $value) |
|
| 212 | |||
| 213 | 1 | public function offsetUnset($offset) |
|
| 217 | |||
| 218 | 1 | public function toArray(): array |
|
| 222 | } |
||
| 223 |