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 |
||
22 | class Config implements ConfigInterface, NamespaceAwareInterface |
||
23 | { |
||
24 | use NamespaceAwareTrait; |
||
25 | |||
26 | /** |
||
27 | * The value that identifies a version order by creation time. |
||
28 | */ |
||
29 | public const VERSION_ORDER_CREATION_TIME = 'creation'; |
||
30 | |||
31 | /** |
||
32 | * The value that identifies a version order by execution time. |
||
33 | */ |
||
34 | public const VERSION_ORDER_EXECUTION_TIME = 'execution'; |
||
35 | |||
36 | /** |
||
37 | * @var array |
||
38 | */ |
||
39 | protected $values = []; |
||
40 | |||
41 | /** |
||
42 | * @var string |
||
43 | */ |
||
44 | protected $configFilePath; |
||
45 | |||
46 | /** |
||
47 | * @inheritDoc |
||
48 | */ |
||
49 | public function __construct(array $configArray, $configFilePath = null) |
||
54 | |||
55 | /** |
||
56 | * Create a new instance of the config class using a Yaml file path. |
||
57 | * |
||
58 | * @param string $configFilePath Path to the Yaml File |
||
59 | * |
||
60 | * @throws \RuntimeException |
||
61 | * |
||
62 | * @return \Phinx\Config\Config |
||
63 | */ |
||
64 | public static function fromYaml($configFilePath) |
||
65 | { |
||
66 | 448 | if (!class_exists('Symfony\\Component\\Yaml\\Yaml', true)) { |
|
67 | throw new RuntimeException('Missing yaml parser, symfony/yaml package is not installed.'); |
||
68 | 448 | } |
|
69 | 448 | ||
70 | 448 | $configFile = file_get_contents($configFilePath); |
|
71 | $configArray = Yaml::parse($configFile); |
||
72 | |||
73 | if (!is_array($configArray)) { |
||
74 | throw new RuntimeException(sprintf( |
||
75 | 'File \'%s\' must be valid YAML', |
||
76 | $configFilePath |
||
77 | )); |
||
78 | } |
||
79 | 2 | ||
80 | return new static($configArray, $configFilePath); |
||
81 | 2 | } |
|
82 | 2 | ||
83 | /** |
||
84 | 2 | * Create a new instance of the config class using a JSON file path. |
|
85 | 1 | * |
|
86 | 1 | * @param string $configFilePath Path to the JSON File |
|
87 | * |
||
88 | 1 | * @throws \RuntimeException |
|
89 | * |
||
90 | 1 | * @return \Phinx\Config\Config |
|
91 | */ |
||
92 | public static function fromJson($configFilePath) |
||
93 | { |
||
94 | if (!function_exists('json_decode')) { |
||
95 | throw new RuntimeException("Need to install JSON PHP extension to use JSON config"); |
||
96 | } |
||
97 | |||
98 | $configArray = json_decode(file_get_contents($configFilePath), true); |
||
99 | if (!is_array($configArray)) { |
||
100 | 2 | throw new RuntimeException(sprintf( |
|
101 | 'File \'%s\' must be valid JSON', |
||
102 | 2 | $configFilePath |
|
103 | 2 | )); |
|
104 | 1 | } |
|
105 | 1 | ||
106 | return new static($configArray, $configFilePath); |
||
107 | 1 | } |
|
108 | |||
109 | 1 | /** |
|
110 | * Create a new instance of the config class using a PHP file path. |
||
111 | * |
||
112 | * @param string $configFilePath Path to the PHP File |
||
113 | * |
||
114 | * @throws \RuntimeException |
||
115 | * |
||
116 | * @return \Phinx\Config\Config |
||
117 | */ |
||
118 | public static function fromPhp($configFilePath) |
||
136 | |||
137 | /** |
||
138 | * @inheritDoc |
||
139 | */ |
||
140 | public function getEnvironments() |
||
155 | |||
156 | /** |
||
157 | * @inheritDoc |
||
158 | */ |
||
159 | public function getEnvironment($name) |
||
174 | |||
175 | /** |
||
176 | * @inheritDoc |
||
177 | */ |
||
178 | public function hasEnvironment($name) |
||
182 | |||
183 | /** |
||
184 | * @inheritDoc |
||
185 | */ |
||
186 | public function getDefaultEnvironment() |
||
228 | |||
229 | 7 | /** |
|
230 | * @inheritDoc |
||
231 | */ |
||
232 | public function getAlias($alias) |
||
236 | |||
237 | 448 | /** |
|
238 | * @inheritDoc |
||
239 | */ |
||
240 | public function getAliases() |
||
244 | |||
245 | 423 | /** |
|
246 | 1 | * @inheritDoc |
|
247 | */ |
||
248 | public function getConfigFilePath() |
||
252 | |||
253 | 422 | /** |
|
254 | * @inheritDoc |
||
255 | * |
||
256 | * @throws \UnexpectedValueException |
||
257 | */ |
||
258 | public function getMigrationPaths() |
||
259 | { |
||
260 | if (!isset($this->values['paths']['migrations'])) { |
||
261 | throw new UnexpectedValueException('Migrations path missing from config file'); |
||
262 | 14 | } |
|
263 | |||
264 | 14 | if (is_string($this->values['paths']['migrations'])) { |
|
265 | $this->values['paths']['migrations'] = [$this->values['paths']['migrations']]; |
||
266 | 14 | } |
|
267 | |||
268 | return $this->values['paths']['migrations']; |
||
269 | } |
||
270 | |||
271 | /** |
||
272 | 48 | * Gets the base class name for migrations. |
|
273 | * |
||
274 | 48 | * @param bool $dropNamespace Return the base migration class name without the namespace. |
|
275 | 28 | * |
|
276 | * @return string |
||
277 | */ |
||
278 | 20 | public function getMigrationBaseClassName($dropNamespace = true) |
|
284 | |||
285 | /** |
||
286 | * @inheritDoc |
||
287 | * |
||
288 | * @throws \UnexpectedValueException |
||
289 | */ |
||
290 | 14 | public function getSeedPaths() |
|
291 | { |
||
292 | 14 | if (!isset($this->values['paths']['seeds'])) { |
|
293 | 13 | throw new UnexpectedValueException('Seeds path missing from config file'); |
|
294 | } |
||
295 | |||
296 | 1 | if (is_string($this->values['paths']['seeds'])) { |
|
297 | $this->values['paths']['seeds'] = [$this->values['paths']['seeds']]; |
||
298 | } |
||
299 | |||
300 | return $this->values['paths']['seeds']; |
||
301 | } |
||
302 | |||
303 | /** |
||
304 | 14 | * Gets the base class name for seeders. |
|
305 | * |
||
306 | 14 | * @param bool $dropNamespace Return the base seeder class name without the namespace. |
|
307 | 10 | * @return string |
|
308 | */ |
||
309 | public function getSeedBaseClassName($dropNamespace = true) |
||
315 | |||
316 | /** |
||
317 | * Get the template file name. |
||
318 | 384 | * |
|
319 | * @return string|false |
||
320 | 384 | */ |
|
321 | 162 | public function getTemplateFile() |
|
329 | |||
330 | /** |
||
331 | * Get the template class name. |
||
332 | 357 | * |
|
333 | * @return string|false |
||
334 | 357 | */ |
|
335 | public function getTemplateClass() |
||
343 | |||
344 | /** |
||
345 | * {@inheritdoc} |
||
346 | */ |
||
347 | 448 | public function getDataDomain() |
|
355 | 2 | ||
356 | 448 | /** |
|
357 | * Get the version order. |
||
358 | * |
||
359 | 448 | * @return string |
|
360 | 448 | */ |
|
361 | public function getVersionOrder() |
||
362 | { |
||
363 | 448 | if (!isset($this->values['version_order'])) { |
|
364 | return self::VERSION_ORDER_CREATION_TIME; |
||
365 | } |
||
366 | |||
367 | return $this->values['version_order']; |
||
368 | } |
||
369 | |||
370 | /** |
||
371 | * Is version order creation time? |
||
372 | * |
||
373 | 448 | * @return bool |
|
374 | */ |
||
375 | 448 | public function isVersionOrderCreationTime() |
|
376 | 448 | { |
|
377 | 447 | $versionOrder = $this->getVersionOrder(); |
|
378 | 446 | ||
379 | 446 | return $versionOrder == self::VERSION_ORDER_CREATION_TIME; |
|
380 | } |
||
381 | 446 | ||
382 | 446 | /** |
|
383 | 446 | * Get the bootstrap file path |
|
384 | 446 | * |
|
385 | 446 | * @return string|false |
|
386 | 446 | */ |
|
387 | public function getBootstrapFile() |
||
395 | |||
396 | 213 | /** |
|
397 | * Replace tokens in the specified array. |
||
398 | 213 | * |
|
399 | 213 | * @param array $arr Array to replace |
|
400 | * |
||
401 | * @return array |
||
402 | */ |
||
403 | protected function replaceTokens(array $arr) |
||
423 | |||
424 | 1 | /** |
|
425 | * Recurse an array for the specified tokens and replace them. |
||
426 | 1 | * |
|
427 | 1 | * @param array $arr Array to recurse |
|
428 | * @param string[] $tokens Array of tokens to search for |
||
429 | * |
||
430 | * @return array |
||
431 | */ |
||
432 | protected function recurseArrayForTokens($arr, $tokens) |
||
452 | |||
453 | /** |
||
454 | * Parse a database-agnostic DSN into individual options. |
||
455 | * |
||
456 | * @param array $options Options |
||
457 | * |
||
458 | * @return array |
||
459 | */ |
||
460 | protected function parseAgnosticDsn(array $options) |
||
479 | |||
480 | /** |
||
481 | * @inheritDoc |
||
482 | */ |
||
483 | public function offsetSet($id, $value) |
||
487 | |||
488 | /** |
||
489 | * @inheritDoc |
||
490 | * |
||
491 | * @throws \InvalidArgumentException |
||
492 | */ |
||
493 | public function offsetGet($id) |
||
501 | |||
502 | /** |
||
503 | * @inheritDoc |
||
504 | */ |
||
505 | public function offsetExists($id) |
||
509 | |||
510 | /** |
||
511 | * @inheritDoc |
||
512 | */ |
||
513 | public function offsetUnset($id) |
||
517 | } |
||
518 |