Failed Conditions
Pull Request — master (#675)
by Jonathan
06:31
created

XmlConfiguration::doLoad()   D

Complexity

Conditions 11
Paths 257

Size

Total Lines 65
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 35
CRAP Score 11

Importance

Changes 0
Metric Value
cc 11
eloc 34
nc 257
nop 1
dl 0
loc 65
ccs 35
cts 35
cp 1
crap 11
rs 4.421
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\Configuration\Exception\XmlNotValid;
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 XmlNotValid::new();
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 9
            $config['column_name'] = (string) $xml->table['column'];
46
        }
47
48 17
        if (isset($xml->table['executed_at_column'])) {
49 9
            $config['executed_at_column_name'] = (string) $xml->table['executed_at_column'];
50
        }
51
52 17
        if (isset($xml->{'migrations-namespace'})) {
53 12
            $config['migrations_namespace'] = (string) $xml->{'migrations-namespace'};
54
        }
55
56 17
        if (isset($xml->{'organize-migrations'})) {
57 5
            $config['organize_migrations'] = (string) $xml->{'organize-migrations'};
58
        }
59
60 17
        if (isset($xml->{'migrations-directory'})) {
61 12
            $config['migrations_directory'] = $this->getDirectoryRelativeToFile($file, (string) $xml->{'migrations-directory'});
62
        }
63
64 17
        if (isset($xml->migrations->migration)) {
65 1
            $migrations = [];
66
67 1
            foreach ($xml->migrations->migration as $migration) {
68 1
                $attributes = $migration->attributes();
69
70 1
                $version = (string) $attributes['version'];
71 1
                $class   = (string) $attributes['class'];
72
73 1
                $migrations[] = [
74 1
                    'version' => $version,
75 1
                    'class' => $class,
76
                ];
77
            }
78
79 1
            $config['migrations'] = $migrations;
80
        }
81
82 17
        $this->setConfiguration($config);
83 16
    }
84
}
85