Completed
Pull Request — master (#29)
by Gabriel
01:46
created

ConfigLoader::loadConfig()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 8.7857
c 0
b 0
f 0
cc 6
nc 7
nop 1
1
<?php
2
3
namespace OckCyp\CoversValidator\Loader;
4
5
use OckCyp\CoversValidator\Model\ConfigurationHolder;
6
use PHPUnit\Util\Configuration;
7
8
class ConfigLoader
9
{
10
    /**
11
     * Load configuration from file
12
     *
13
     * @param string $fileName
14
     * @return ConfigurationHolder
15
     */
16
    public static function loadConfig(string $fileName): ConfigurationHolder
17
    {
18
        if (class_exists(Configuration::class)) {
19
            // @codeCoverageIgnoreStart
20
            $configuration = Configuration::getInstance($fileName);
21
            $filename = $configuration->getFilename();
22
            $phpunit = $configuration->getPHPUnitConfiguration();
23
            $bootstrap = '';
24
            if (isset($phpunit['bootstrap'])) {
25
                $bootstrap = $phpunit['bootstrap'];
26
            }
27
            // @codeCoverageIgnoreEnd
28
        } else {
29
            // PHPUnit < 9.3
30
            if (class_exists('PHPUnit\TextUI\Configuration\Loader', true)) {
31
                $loader = new \PHPUnit\TextUI\Configuration\Loader();
32
            }
33
            // PHPUnit >= 9.3
34
            elseif (class_exists('PHPUnit\TextUI\XmlConfiguration\Loader', true)) {
35
                $loader = new \PHPUnit\TextUI\XmlConfiguration\Loader();
36
            } else {
37
                throw new \RuntimeException('Could not find PHPUnit\'s configuration loader class.');
38
            }
39
40
            $configuration = $loader->load($fileName);
41
            $filename = $configuration->filename();
42
            $phpunit = $configuration->phpunit();
43
            $bootstrap = $phpunit->hasBootstrap() ? $phpunit->bootstrap() : null;
44
        }
45
46
        return new ConfigurationHolder($configuration, $filename, $bootstrap);
47
    }
48
}
49