Completed
Push — master ( c45a68...1b5e4d )
by Jonathan
05:57 queued 01:39
created

XmlConfiguration::doLoad()   F

Complexity

Conditions 12
Paths 513

Size

Total Lines 69
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 37
CRAP Score 12

Importance

Changes 0
Metric Value
cc 12
eloc 36
nc 513
nop 1
dl 0
loc 69
ccs 37
cts 37
cp 1
crap 12
rs 3.545
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 20
    protected function doLoad(string $file) : void
19
    {
20 20
        libxml_use_internal_errors(true);
21
22 20
        $xml = new DOMDocument();
23 20
        $xml->load($file);
24
25 20
        $xsdPath = __DIR__ . DIRECTORY_SEPARATOR . 'XML' . DIRECTORY_SEPARATOR . 'configuration.xsd';
26
27 20
        if (! $xml->schemaValidate($xsdPath)) {
28 2
            libxml_clear_errors();
29
30 2
            throw XmlNotValid::new();
31
        }
32
33 18
        $xml    = simplexml_load_file($file, 'SimpleXMLElement', LIBXML_NOCDATA);
34 18
        $config = [];
35
36 18
        if (isset($xml->name)) {
37 14
            $config['name'] = (string) $xml->name;
38
        }
39
40 18
        if (isset($xml->table['name'])) {
41 14
            $config['table_name'] = (string) $xml->table['name'];
42
        }
43
44 18
        if (isset($xml->table['column'])) {
45 10
            $config['column_name'] = (string) $xml->table['column'];
46
        }
47
48 18
        if (isset($xml->table['column_length'])) {
49 10
            $config['column_length'] = (int) $xml->table['column_length'];
50
        }
51
52 18
        if (isset($xml->table['executed_at_column'])) {
53 10
            $config['executed_at_column_name'] = (string) $xml->table['executed_at_column'];
54
        }
55
56 18
        if (isset($xml->{'migrations-namespace'})) {
57 13
            $config['migrations_namespace'] = (string) $xml->{'migrations-namespace'};
58
        }
59
60 18
        if (isset($xml->{'organize-migrations'})) {
61 5
            $config['organize_migrations'] = (string) $xml->{'organize-migrations'};
62
        }
63
64 18
        if (isset($xml->{'migrations-directory'})) {
65 13
            $config['migrations_directory'] = $this->getDirectoryRelativeToFile($file, (string) $xml->{'migrations-directory'});
66
        }
67
68 18
        if (isset($xml->migrations->migration)) {
69 1
            $migrations = [];
70
71 1
            foreach ($xml->migrations->migration as $migration) {
72 1
                $attributes = $migration->attributes();
73
74 1
                $version = (string) $attributes['version'];
75 1
                $class   = (string) $attributes['class'];
76
77 1
                $migrations[] = [
78 1
                    'version' => $version,
79 1
                    'class' => $class,
80
                ];
81
            }
82
83 1
            $config['migrations'] = $migrations;
84
        }
85
86 18
        $this->setConfiguration($config);
87 17
    }
88
}
89