Failed Conditions
Pull Request — master (#632)
by Michael
02:44
created

XmlConfiguration::doLoad()   C

Complexity

Conditions 10
Paths 129

Size

Total Lines 61
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 33
CRAP Score 10

Importance

Changes 0
Metric Value
cc 10
eloc 32
nc 129
nop 1
dl 0
loc 61
ccs 33
cts 33
cp 1
crap 10
rs 6.0317
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Migrations\Configuration;
6
7
use Doctrine\Migrations\MigrationException;
8
use DOMDocument;
9
use const DIRECTORY_SEPARATOR;
10
use const LIBXML_NOCDATA;
11
use function libxml_clear_errors;
12
use function libxml_use_internal_errors;
13
use function simplexml_load_file;
14
15
class XmlConfiguration extends AbstractFileConfiguration
16
{
17
    /** @inheritdoc */
18 19
    protected function doLoad(string $file) : void
19
    {
20 19
        libxml_use_internal_errors(true);
21
22 19
        $xml = new DOMDocument();
23 19
        $xml->load($file);
24
25 19
        $xsdPath = __DIR__ . DIRECTORY_SEPARATOR . 'XML' . DIRECTORY_SEPARATOR . 'configuration.xsd';
26
27 19
        if (! $xml->schemaValidate($xsdPath)) {
28 2
            libxml_clear_errors();
29
30 2
            throw MigrationException::configurationNotValid('XML configuration did not pass the validation test.');
31
        }
32
33 17
        $xml    = simplexml_load_file($file, 'SimpleXMLElement', LIBXML_NOCDATA);
34 17
        $config = [];
35
36 17
        if (isset($xml->name)) {
37 13
            $config['name'] = (string) $xml->name;
38
        }
39
40 17
        if (isset($xml->table['name'])) {
41 13
            $config['table_name'] = (string) $xml->table['name'];
42
        }
43
44 17
        if (isset($xml->table['column'])) {
45 8
            $config['column_name'] = (string) $xml->table['column'];
46
        }
47
48 17
        if (isset($xml->{'migrations-namespace'})) {
49 12
            $config['migrations_namespace'] = (string) $xml->{'migrations-namespace'};
50
        }
51
52 17
        if (isset($xml->{'organize-migrations'})) {
53 5
            $config['organize_migrations'] = (string) $xml->{'organize-migrations'};
54
        }
55
56 17
        if (isset($xml->{'migrations-directory'})) {
57 12
            $config['migrations_directory'] = $this->getDirectoryRelativeToFile($file, (string) $xml->{'migrations-directory'});
58
        }
59
60 17
        if (isset($xml->migrations->migration)) {
61 1
            $migrations = [];
62
63 1
            foreach ($xml->migrations->migration as $migration) {
64 1
                $attributes = $migration->attributes();
65
66 1
                $version = (string) $attributes['version'];
67 1
                $class   = (string) $attributes['class'];
68
69 1
                $migrations[] = [
70 1
                    'version' => $version,
71 1
                    'class' => $class,
72
                ];
73
            }
74
75 1
            $config['migrations'] = $migrations;
76
        }
77
78 17
        $this->setConfiguration($config);
79 16
    }
80
}
81