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 | private const LICENSE_DIRTY_HACK = 'YOUR-LICENSE-KEY'; |
||
26 | |||
27 | private const DATABASE_EDITION_IDS = [ |
||
28 | 'GeoLite2-ASN', |
||
29 | 'GeoLite2-City', |
||
30 | 'GeoLite2-Country', |
||
31 | 'GeoIP2-City', |
||
32 | 'GeoIP2-Country', |
||
33 | 'GeoIP2-Anonymous-IP', |
||
34 | 'GeoIP2-Domain', |
||
35 | 'GeoIP2-ISP', |
||
36 | ]; |
||
37 | |||
38 | /** |
||
39 | * @var string |
||
40 | */ |
||
41 | private $cache_dir; |
||
42 | |||
43 | /** |
||
44 | * @param string|null $cache_dir |
||
45 | */ |
||
46 | 96 | public function __construct(?string $cache_dir) |
|
50 | |||
51 | /** |
||
52 | * @return TreeBuilder |
||
53 | */ |
||
54 | 96 | public function getConfigTreeBuilder(): TreeBuilder |
|
83 | |||
84 | /** |
||
85 | * @return ArrayNodeDefinition |
||
86 | */ |
||
87 | 96 | private function getDatabaseNode(): ArrayNodeDefinition |
|
118 | |||
119 | /** |
||
120 | * @param string $name |
||
121 | * |
||
122 | * @return TreeBuilder |
||
123 | */ |
||
124 | 96 | private function createTreeBuilder(string $name): TreeBuilder |
|
134 | |||
135 | /** |
||
136 | * @param TreeBuilder $tree_builder |
||
137 | * @param string $name |
||
138 | * |
||
139 | * @return ArrayNodeDefinition |
||
140 | */ |
||
141 | 96 | private function getRootNode(TreeBuilder $tree_builder, string $name): ArrayNodeDefinition |
|
159 | |||
160 | /** |
||
161 | * @param ArrayNodeDefinition $root_node |
||
162 | * |
||
163 | * @return ArrayNodeDefinition |
||
164 | */ |
||
165 | 96 | private function arrayPrototype(ArrayNodeDefinition $root_node): ArrayNodeDefinition |
|
166 | { |
||
167 | // Symfony 3.3 + |
||
168 | 96 | if (method_exists($root_node, 'arrayPrototype')) { |
|
169 | 96 | return $root_node->arrayPrototype(); |
|
170 | } |
||
171 | |||
172 | // Symfony 3.2 and below |
||
173 | $node = $root_node->prototype('array'); |
||
174 | |||
175 | // @codeCoverageIgnoreStart |
||
176 | if (!($node instanceof ArrayNodeDefinition)) { // should be always false |
||
177 | throw new \RuntimeException(sprintf('The "array" prototype should be instance of %s, got %s instead.', ArrayNodeDefinition::class, get_class($node))); |
||
178 | } |
||
179 | // @codeCoverageIgnoreEnd |
||
180 | |||
181 | return $node; |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * Normalize default_database from databases. |
||
186 | * |
||
187 | * @param NodeDefinition $root_node |
||
188 | */ |
||
189 | 96 | private function normalizeDefaultDatabase(NodeDefinition $root_node): void |
|
207 | |||
208 | /** |
||
209 | * Normalize databases root configuration to default_database. |
||
210 | * |
||
211 | * @param NodeDefinition $root_node |
||
212 | */ |
||
213 | 96 | private function normalizeRootConfigurationToDefaultDatabase(NodeDefinition $root_node): void |
|
233 | |||
234 | /** |
||
235 | * Dirty hack for Symfony Flex. |
||
236 | * |
||
237 | * @see https://github.com/symfony/recipes-contrib/pull/837 |
||
238 | * |
||
239 | * @param NodeDefinition $root_node |
||
240 | */ |
||
241 | 96 | private function normalizeLicenseDirtyHack(NodeDefinition $root_node): void |
|
259 | |||
260 | /** |
||
261 | * Validate that the default_database exists in the list of databases. |
||
262 | * |
||
263 | * @param NodeDefinition $root_node |
||
264 | */ |
||
265 | 96 | private function validateAvailableDefaultDatabase(NodeDefinition $root_node): void |
|
282 | |||
283 | /** |
||
284 | * Add a license option to the databases configuration if it does not exist. |
||
285 | * Allow use a global license for all databases. |
||
286 | * |
||
287 | * @param NodeDefinition $root_node |
||
288 | */ |
||
289 | 96 | private function allowGlobalLicense(NodeDefinition $root_node): void |
|
310 | |||
311 | /** |
||
312 | * Add a locales option to the databases configuration if it does not exist. |
||
313 | * Allow use a global locales for all databases. |
||
314 | * |
||
315 | * @param NodeDefinition $root_node |
||
316 | */ |
||
317 | 96 | private function allowGlobalLocales(NodeDefinition $root_node): void |
|
338 | |||
339 | /** |
||
340 | * Validate database options. |
||
341 | * |
||
342 | * @param NodeDefinition $root_node |
||
343 | */ |
||
344 | 96 | private function validateDatabases(NodeDefinition $root_node): void |
|
345 | { |
||
346 | $root_node |
||
347 | 96 | ->validate() |
|
348 | ->ifTrue(static function ($v): bool { |
||
349 | 76 | return is_array($v) && array_key_exists('databases', $v) && is_array($v['databases']); |
|
350 | 96 | }) |
|
351 | ->then(static function (array $v): array { |
||
352 | 76 | foreach ($v['databases'] as $name => $database) { |
|
353 | 59 | if (empty($database['license'])) { |
|
354 | 8 | throw new \InvalidArgumentException(sprintf('License for downloaded database "%s" is not specified.', $name)); |
|
355 | } |
||
356 | |||
357 | 51 | if (empty($database['edition'])) { |
|
358 | 4 | throw new \InvalidArgumentException(sprintf('Edition of downloaded database "%s" is not selected.', $name)); |
|
359 | } |
||
360 | |||
361 | 47 | if (empty($database['url'])) { |
|
362 | 4 | throw new \InvalidArgumentException(sprintf('URL for download database "%s" is not specified.', $name)); |
|
363 | } |
||
364 | |||
365 | 43 | if (empty($database['path'])) { |
|
366 | 4 | throw new \InvalidArgumentException(sprintf('The destination path to download database "%s" is not specified.', $name)); |
|
367 | } |
||
368 | |||
369 | 39 | if (empty($database['locales'])) { |
|
370 | throw new \InvalidArgumentException(sprintf('The list of locales for database "%s" should not be empty.', $name)); |
||
371 | } |
||
372 | } |
||
373 | |||
374 | 56 | return $v; |
|
375 | 96 | }); |
|
376 | 96 | } |
|
377 | |||
378 | /** |
||
379 | * Normalize url option from license key and edition id. |
||
380 | * |
||
381 | * @param NodeDefinition $database_node |
||
382 | */ |
||
383 | 96 | private function normalizeUrl(NodeDefinition $database_node): void |
|
400 | |||
401 | /** |
||
402 | * Normalize path option from edition id. |
||
403 | * |
||
404 | * @param NodeDefinition $database_node |
||
405 | */ |
||
406 | 96 | private function normalizePath(NodeDefinition $database_node): void |
|
419 | |||
420 | /** |
||
421 | * The url option must be a valid URL. |
||
422 | * |
||
423 | * @param NodeDefinition $url |
||
424 | */ |
||
425 | 96 | private function validateURL(NodeDefinition $url): void |
|
436 | } |
||
437 |
This check looks for function calls that miss required arguments.