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 |
||
24 | class Config implements ConfigInterface, NamespaceAwareInterface |
||
25 | { |
||
26 | use NamespaceAwareTrait; |
||
27 | |||
28 | /** |
||
29 | * The value that identifies a version order by creation time. |
||
30 | */ |
||
31 | public const VERSION_ORDER_CREATION_TIME = 'creation'; |
||
32 | |||
33 | /** |
||
34 | * The value that identifies a version order by execution time. |
||
35 | */ |
||
36 | public const VERSION_ORDER_EXECUTION_TIME = 'execution'; |
||
37 | |||
38 | /** |
||
39 | * @var array |
||
40 | */ |
||
41 | protected $values = []; |
||
42 | |||
43 | /** |
||
44 | * @var string |
||
45 | */ |
||
46 | protected $configFilePath; |
||
47 | |||
48 | /** |
||
49 | * @inheritDoc |
||
50 | */ |
||
51 | public function __construct(array $configArray, $configFilePath = null) |
||
56 | |||
57 | /** |
||
58 | * Create a new instance of the config class using a Yaml file path. |
||
59 | * |
||
60 | * @param string $configFilePath Path to the Yaml File |
||
61 | * |
||
62 | * @throws \RuntimeException |
||
63 | * |
||
64 | * @return \Phinx\Config\Config |
||
65 | */ |
||
66 | 448 | public static function fromYaml($configFilePath) |
|
84 | 2 | ||
85 | 1 | /** |
|
86 | 1 | * Create a new instance of the config class using a JSON file path. |
|
87 | * |
||
88 | 1 | * @param string $configFilePath Path to the JSON File |
|
89 | * |
||
90 | 1 | * @throws \RuntimeException |
|
91 | * |
||
92 | * @return \Phinx\Config\Config |
||
93 | */ |
||
94 | public static function fromJson($configFilePath) |
||
110 | |||
111 | /** |
||
112 | * Create a new instance of the config class using a PHP file path. |
||
113 | * |
||
114 | * @param string $configFilePath Path to the PHP File |
||
115 | * |
||
116 | * @throws \RuntimeException |
||
117 | * |
||
118 | * @return \Phinx\Config\Config |
||
119 | 3 | */ |
|
120 | public static function fromPhp($configFilePath) |
||
138 | |||
139 | /** |
||
140 | * @inheritDoc |
||
141 | 25 | */ |
|
142 | public function getEnvironments() |
||
157 | |||
158 | /** |
||
159 | * @inheritDoc |
||
160 | 25 | */ |
|
161 | public function getEnvironment($name) |
||
184 | |||
185 | /** |
||
186 | * @inheritDoc |
||
187 | 20 | */ |
|
188 | public function hasEnvironment($name) |
||
192 | 2 | ||
193 | 1 | /** |
|
194 | * @inheritDoc |
||
195 | */ |
||
196 | 1 | public function getDefaultEnvironment() |
|
238 | |||
239 | /** |
||
240 | * @inheritDoc |
||
241 | */ |
||
242 | public function getAlias($alias) |
||
246 | 1 | ||
247 | /** |
||
248 | * @inheritDoc |
||
249 | 422 | */ |
|
250 | 219 | public function getAliases() |
|
254 | |||
255 | /** |
||
256 | * @inheritDoc |
||
257 | */ |
||
258 | public function getConfigFilePath() |
||
262 | 14 | ||
263 | /** |
||
264 | 14 | * @inheritDoc |
|
265 | * |
||
266 | 14 | * @throws \UnexpectedValueException |
|
267 | */ |
||
268 | public function getMigrationPaths() |
||
269 | { |
||
270 | if (!isset($this->values['paths']['migrations'])) { |
||
271 | throw new UnexpectedValueException('Migrations path missing from config file'); |
||
272 | 48 | } |
|
273 | |||
274 | 48 | if (is_string($this->values['paths']['migrations'])) { |
|
275 | 28 | $this->values['paths']['migrations'] = [$this->values['paths']['migrations']]; |
|
276 | } |
||
277 | |||
278 | 20 | return $this->values['paths']['migrations']; |
|
279 | 13 | } |
|
280 | 13 | ||
281 | /** |
||
282 | 20 | * Gets the base class name for migrations. |
|
283 | * |
||
284 | * @param bool $dropNamespace Return the base migration class name without the namespace. |
||
285 | * |
||
286 | * @return string |
||
287 | */ |
||
288 | public function getMigrationBaseClassName($dropNamespace = true) |
||
294 | |||
295 | /** |
||
296 | 1 | * @inheritDoc |
|
297 | * |
||
298 | * @throws \UnexpectedValueException |
||
299 | */ |
||
300 | public function getSeedPaths() |
||
301 | { |
||
302 | if (!isset($this->values['paths']['seeds'])) { |
||
303 | throw new UnexpectedValueException('Seeds path missing from config file'); |
||
304 | 14 | } |
|
305 | |||
306 | 14 | if (is_string($this->values['paths']['seeds'])) { |
|
307 | 10 | $this->values['paths']['seeds'] = [$this->values['paths']['seeds']]; |
|
308 | } |
||
309 | |||
310 | 4 | return $this->values['paths']['seeds']; |
|
311 | } |
||
312 | |||
313 | /** |
||
314 | * Gets the base class name for seeders. |
||
315 | * |
||
316 | * @param bool $dropNamespace Return the base seeder class name without the namespace. |
||
317 | * @return string |
||
318 | 384 | */ |
|
319 | public function getSeedBaseClassName($dropNamespace = true) |
||
325 | |||
326 | /** |
||
327 | * Get the template file name. |
||
328 | * |
||
329 | * @return string|false |
||
330 | */ |
||
331 | public function getTemplateFile() |
||
339 | |||
340 | /** |
||
341 | * Get the template class name. |
||
342 | * |
||
343 | * @return string|false |
||
344 | */ |
||
345 | public function getTemplateClass() |
||
353 | 448 | ||
354 | 2 | /** |
|
355 | 2 | * {@inheritdoc} |
|
356 | 448 | */ |
|
357 | public function getDataDomain() |
||
365 | |||
366 | /** |
||
367 | * @inheritDoc |
||
368 | */ |
||
369 | public function getContainer() |
||
377 | 447 | ||
378 | 446 | /** |
|
379 | 446 | * Get the version order. |
|
380 | * |
||
381 | 446 | * @return string |
|
382 | 446 | */ |
|
383 | 446 | public function getVersionOrder() |
|
384 | 446 | { |
|
385 | 446 | if (!isset($this->values['version_order'])) { |
|
386 | 446 | return self::VERSION_ORDER_CREATION_TIME; |
|
387 | } |
||
388 | 43 | ||
389 | 448 | return $this->values['version_order']; |
|
390 | 448 | } |
|
391 | |||
392 | /** |
||
393 | * Is version order creation time? |
||
394 | * |
||
395 | * @return bool |
||
396 | 213 | */ |
|
397 | public function isVersionOrderCreationTime() |
||
398 | 213 | { |
|
399 | 213 | $versionOrder = $this->getVersionOrder(); |
|
400 | |||
401 | return $versionOrder == self::VERSION_ORDER_CREATION_TIME; |
||
402 | } |
||
403 | |||
404 | 2 | /** |
|
405 | * Get the bootstrap file path |
||
406 | 2 | * |
|
407 | 1 | * @return string|false |
|
408 | */ |
||
409 | public function getBootstrapFile() |
||
417 | |||
418 | 1 | /** |
|
419 | * Replace tokens in the specified array. |
||
420 | * |
||
421 | * @param array $arr Array to replace |
||
422 | * |
||
423 | * @return array |
||
424 | 1 | */ |
|
425 | protected function replaceTokens(array $arr) |
||
445 | |||
446 | /** |
||
447 | * Recurse an array for the specified tokens and replace them. |
||
448 | * |
||
449 | * @param array $arr Array to recurse |
||
450 | * @param string[] $tokens Array of tokens to search for |
||
451 | * |
||
452 | * @return array |
||
453 | */ |
||
454 | protected function recurseArrayForTokens($arr, $tokens) |
||
474 | |||
475 | /** |
||
476 | * Parse a database-agnostic DSN into individual options. |
||
477 | * |
||
478 | * @param array $options Options |
||
479 | * |
||
480 | * @return array |
||
481 | */ |
||
482 | protected function parseAgnosticDsn(array $options) |
||
493 | |||
494 | /** |
||
495 | * @inheritDoc |
||
496 | */ |
||
497 | public function offsetSet($id, $value) |
||
501 | |||
502 | /** |
||
503 | * @inheritDoc |
||
504 | * |
||
505 | * @throws \InvalidArgumentException |
||
506 | */ |
||
507 | public function offsetGet($id) |
||
515 | |||
516 | /** |
||
517 | * @inheritDoc |
||
518 | */ |
||
519 | public function offsetExists($id) |
||
523 | |||
524 | /** |
||
525 | * @inheritDoc |
||
526 | */ |
||
527 | public function offsetUnset($id) |
||
531 | } |
||
532 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.