Complex classes like Configuration 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 Configuration, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class Configuration implements ConfigurationInterface |
||
| 20 | { |
||
| 21 | private const URL = 'https://download.maxmind.com/app/geoip_download?edition_id=%s&license_key=%s&suffix=tar.gz'; |
||
| 22 | |||
| 23 | private const PATH = '%s/%s.mmdb'; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | private $cache_dir; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @param string|null $cache_dir |
||
| 32 | */ |
||
| 33 | 62 | public function __construct(?string $cache_dir) |
|
| 37 | |||
| 38 | /** |
||
| 39 | * @return TreeBuilder |
||
| 40 | */ |
||
| 41 | 62 | public function getConfigTreeBuilder(): TreeBuilder |
|
| 69 | |||
| 70 | /** |
||
| 71 | * @return ArrayNodeDefinition |
||
| 72 | */ |
||
| 73 | 62 | private function getDatabaseNode(): ArrayNodeDefinition |
|
| 104 | |||
| 105 | /** |
||
| 106 | * @param string $name |
||
| 107 | * |
||
| 108 | * @return TreeBuilder |
||
| 109 | */ |
||
| 110 | 62 | private function createTreeBuilder(string $name): TreeBuilder |
|
| 120 | |||
| 121 | /** |
||
| 122 | * @param TreeBuilder $tree_builder |
||
| 123 | * @param string $name |
||
| 124 | * |
||
| 125 | * @return ArrayNodeDefinition |
||
| 126 | */ |
||
| 127 | 62 | private function getRootNode(TreeBuilder $tree_builder, string $name): ArrayNodeDefinition |
|
| 145 | |||
| 146 | /** |
||
| 147 | * @param ArrayNodeDefinition $root_node |
||
| 148 | * |
||
| 149 | * @return ArrayNodeDefinition |
||
| 150 | */ |
||
| 151 | 62 | private function arrayPrototype(ArrayNodeDefinition $root_node): ArrayNodeDefinition |
|
| 169 | |||
| 170 | /** |
||
| 171 | * Normalize default_database from databases. |
||
| 172 | * |
||
| 173 | * @param NodeDefinition $root_node |
||
| 174 | */ |
||
| 175 | 62 | private function normalizeDefaultDatabase(NodeDefinition $root_node): void |
|
| 193 | |||
| 194 | /** |
||
| 195 | * Normalize databases root configuration to default_database. |
||
| 196 | * |
||
| 197 | * @param NodeDefinition $root_node |
||
| 198 | */ |
||
| 199 | 62 | private function normalizeRootConfigurationToDefaultDatabase(NodeDefinition $root_node): void |
|
| 219 | |||
| 220 | /** |
||
| 221 | * Validate that the default_database exists in the list of databases. |
||
| 222 | * |
||
| 223 | * @param NodeDefinition $root_node |
||
| 224 | */ |
||
| 225 | 62 | private function validateAvailableDefaultDatabase(NodeDefinition $root_node): void |
|
| 242 | |||
| 243 | /** |
||
| 244 | * Add a license option to the databases configuration if it does not exist. |
||
| 245 | * Allow use a global license for all databases. |
||
| 246 | * |
||
| 247 | * @param NodeDefinition $root_node |
||
| 248 | */ |
||
| 249 | 62 | private function allowGlobalLicense(NodeDefinition $root_node): void |
|
| 270 | |||
| 271 | /** |
||
| 272 | * Add a locales option to the databases configuration if it does not exist. |
||
| 273 | * Allow use a global locales for all databases. |
||
| 274 | * |
||
| 275 | * @param NodeDefinition $root_node |
||
| 276 | */ |
||
| 277 | 62 | private function allowGlobalLocales(NodeDefinition $root_node): void |
|
| 298 | |||
| 299 | /** |
||
| 300 | * Validate database options. |
||
| 301 | * |
||
| 302 | * @param NodeDefinition $root_node |
||
| 303 | */ |
||
| 304 | 62 | private function validateDatabases(NodeDefinition $root_node): void |
|
| 305 | { |
||
| 306 | $root_node |
||
| 307 | 62 | ->validate() |
|
| 308 | ->ifTrue(static function ($v): bool { |
||
| 309 | 48 | return is_array($v) && array_key_exists('databases', $v) && is_array($v['databases']); |
|
| 310 | 62 | }) |
|
| 311 | ->then(static function (array $v): array { |
||
| 312 | 48 | foreach ($v['databases'] as $name => $database) { |
|
| 313 | 35 | if (empty($database['license'])) { |
|
| 314 | 8 | throw new \InvalidArgumentException(sprintf('License for downloaded databases "%s" is not specified.', $name)); |
|
| 315 | } |
||
| 316 | |||
| 317 | 27 | if (empty($database['edition'])) { |
|
| 318 | 8 | throw new \InvalidArgumentException(sprintf('Edition of downloaded databases "%s" is not selected.', $name)); |
|
| 319 | } |
||
| 320 | |||
| 321 | 19 | if (empty($database['url'])) { |
|
| 322 | 4 | throw new \InvalidArgumentException(sprintf('URL for download databases "%s" is not specified.', $name)); |
|
| 323 | } |
||
| 324 | |||
| 325 | 15 | if (empty($database['path'])) { |
|
| 326 | 4 | throw new \InvalidArgumentException(sprintf('The destination path to download database "%s" is not specified.', $name)); |
|
| 327 | } |
||
| 328 | |||
| 329 | 11 | if (empty($database['locales'])) { |
|
| 330 | 11 | throw new \InvalidArgumentException(sprintf('The list of locales for databases "%s" should not be empty.', $name)); |
|
| 331 | } |
||
| 332 | } |
||
| 333 | |||
| 334 | 24 | return $v; |
|
| 335 | 62 | }); |
|
| 336 | 62 | } |
|
| 337 | |||
| 338 | /** |
||
| 339 | * Normalize url option from license key and edition id. |
||
| 340 | * |
||
| 341 | * @param NodeDefinition $database_node |
||
| 342 | */ |
||
| 343 | 62 | private function normalizeUrl(NodeDefinition $database_node): void |
|
| 360 | |||
| 361 | /** |
||
| 362 | * Normalize path option from edition id. |
||
| 363 | * |
||
| 364 | * @param NodeDefinition $database_node |
||
| 365 | */ |
||
| 366 | 62 | private function normalizePath(NodeDefinition $database_node): void |
|
| 379 | |||
| 380 | /** |
||
| 381 | * The url option must be a valid URL. |
||
| 382 | * |
||
| 383 | * @param NodeDefinition $url |
||
| 384 | */ |
||
| 385 | 62 | private function validateURL(NodeDefinition $url): void |
|
| 396 | } |
||
| 397 |
This check looks for function calls that miss required arguments.