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 | 41 | public function __construct(?string $cache_dir) |
|
37 | |||
38 | /** |
||
39 | * @return TreeBuilder |
||
40 | */ |
||
41 | 41 | public function getConfigTreeBuilder(): TreeBuilder |
|
69 | |||
70 | /** |
||
71 | * @return ArrayNodeDefinition |
||
72 | */ |
||
73 | 41 | private function getDatabaseNode(): ArrayNodeDefinition |
|
104 | |||
105 | /** |
||
106 | * @param string $name |
||
107 | * |
||
108 | * @return TreeBuilder |
||
109 | */ |
||
110 | 41 | private function createTreeBuilder(string $name): TreeBuilder |
|
120 | |||
121 | /** |
||
122 | * @param TreeBuilder $tree_builder |
||
123 | * @param string $name |
||
124 | * |
||
125 | * @return ArrayNodeDefinition |
||
126 | */ |
||
127 | 41 | private function getRootNode(TreeBuilder $tree_builder, string $name): ArrayNodeDefinition |
|
145 | |||
146 | /** |
||
147 | * @param ArrayNodeDefinition $root_node |
||
148 | * |
||
149 | * @return ArrayNodeDefinition |
||
150 | */ |
||
151 | 41 | private function arrayPrototype(ArrayNodeDefinition $root_node): ArrayNodeDefinition |
|
152 | { |
||
153 | // Symfony 3.3 + |
||
154 | 41 | if (method_exists($root_node, 'arrayPrototype')) { |
|
155 | 41 | return $root_node->arrayPrototype(); |
|
156 | } |
||
157 | |||
158 | // Symfony 3.2 and below |
||
159 | $node = $root_node->prototype('array'); |
||
160 | |||
161 | // @codeCoverageIgnoreStart |
||
162 | if (!($node instanceof ArrayNodeDefinition)) { // should be always false |
||
163 | throw new \RuntimeException(sprintf('The "array" prototype should be instance of %s, got %s instead.', ArrayNodeDefinition::class, get_class($node))); |
||
164 | } |
||
165 | // @codeCoverageIgnoreEnd |
||
166 | |||
167 | return $node; |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * Normalize default_database from databases. |
||
172 | * |
||
173 | * @param NodeDefinition $root_node |
||
174 | */ |
||
175 | 41 | private function normalizeDefaultDatabase(NodeDefinition $root_node): void |
|
194 | |||
195 | /** |
||
196 | * Normalize databases root configuration to default_database. |
||
197 | * |
||
198 | * @param NodeDefinition $root_node |
||
199 | */ |
||
200 | 41 | private function normalizeRootConfigurationToDefaultDatabase(NodeDefinition $root_node): void |
|
222 | |||
223 | /** |
||
224 | * Validate that the default_database exists in the list of databases. |
||
225 | * |
||
226 | * @param NodeDefinition $root_node |
||
227 | */ |
||
228 | 41 | private function validateAvailableDefaultDatabase(NodeDefinition $root_node): void |
|
246 | |||
247 | /** |
||
248 | * Add a license option to the databases configuration if it does not exist. |
||
249 | * Allow use a global license for all databases. |
||
250 | * |
||
251 | * @param NodeDefinition $root_node |
||
252 | */ |
||
253 | 41 | private function allowGlobalLicense(NodeDefinition $root_node): void |
|
274 | |||
275 | /** |
||
276 | * Add a locales option to the databases configuration if it does not exist. |
||
277 | * Allow use a global locales for all databases. |
||
278 | * |
||
279 | * @param NodeDefinition $root_node |
||
280 | */ |
||
281 | 41 | private function allowGlobalLocales(NodeDefinition $root_node): void |
|
302 | |||
303 | /** |
||
304 | * Validate database locales. |
||
305 | * |
||
306 | * @param NodeDefinition $root_node |
||
307 | */ |
||
308 | 41 | private function validateDatabaseLocales(NodeDefinition $root_node): void |
|
325 | |||
326 | /** |
||
327 | * Normalize url option from license key and edition id. |
||
328 | * |
||
329 | * @param NodeDefinition $database_node |
||
330 | */ |
||
331 | 41 | private function normalizeUrl(NodeDefinition $database_node): void |
|
348 | |||
349 | /** |
||
350 | * Normalize path option from edition id. |
||
351 | * |
||
352 | * @param NodeDefinition $database_node |
||
353 | */ |
||
354 | 41 | private function normalizePath(NodeDefinition $database_node): void |
|
367 | |||
368 | /** |
||
369 | * The url option must be a valid URL. |
||
370 | * |
||
371 | * @param NodeDefinition $url |
||
372 | */ |
||
373 | 41 | private function validateURL(NodeDefinition $url): void |
|
384 | } |
||
385 |
This check looks for function calls that miss required arguments.