Passed
Push — master ( cb0392...409c3f )
by Andreas
09:17
created

schemadb::check_inheritance()   C

Complexity

Conditions 16
Paths 86

Size

Total Lines 68
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 197.6303

Importance

Changes 0
Metric Value
cc 16
eloc 38
nc 86
nop 1
dl 0
loc 68
ccs 4
cts 37
cp 0.1081
crap 197.6303
rs 5.5666
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
 * @copyright CONTENT CONTROL GmbH, http://www.contentcontrol-berlin.de
4
 */
5
6
namespace midcom\datamanager;
7
8
use midcom_error;
9
use midcom_helper_misc;
10
11
/**
12
 * Experimental schemadb class
13
 */
14
class schemadb
15
{
16
    private array $schemas = [];
17
18 170
    public static function from_path(string $path) : self
19
    {
20 170
        return new static(static::load_from_path($path));
21
    }
22
23 170
    private static function load_from_path(string $path) : array
24
    {
25 170
        $data = midcom_helper_misc::get_snippet_content($path);
26 170
        return midcom_helper_misc::parse_config($data, $path);
27
    }
28
29 192
    public function __construct(array $data = [])
30
    {
31 192
        $this->check_inheritance($data);
32 192
        foreach ($data as $name => $config) {
33 189
            $this->add($name, new schema($config));
34
        }
35
    }
36
37 192
    private function check_inheritance(array &$data)
38
    {
39 192
        foreach ($data as $schema_name => $schema) {
40 189
            if (!isset($schema['extends'])) {
41 189
                continue;
42
            }
43
44
            // Default extended schema is with the same name
45
            $extended_schema_name = $schema_name;
46
            $path = '';
47
48
            if (is_array($schema['extends'])) {
49
                if (isset($schema['extends']['path'])) {
50
                    $path = $schema['extends']['path'];
51
                }
52
53
                // Override schema name
54
                if (isset($schema['extends']['name'])) {
55
                    $extended_schema_name = $schema['extends']['name'];
56
                }
57
            } elseif (isset($data[$schema['extends']])) {
58
                $schema['extends'] = [
59
                    'name' => $schema['extends'],
60
                ];
61
            } else {
62
                $path = $schema['extends'];
63
            }
64
65
            if ($path === '') {
66
                // Infinite loop, set an UI message and stop executing
67
                if (   !isset($schema['extends']['name'])
68
                    || $schema['extends']['name'] === $schema_name) {
69
                    throw new midcom_error('schema ' . $schema_name . ' extends itself');
70
                }
71
                $extended_schema_name = $schema['extends']['name'];
72
                $extended_schemadb = [$extended_schema_name => $data[$extended_schema_name]];
73
            } else {
74
                $extended_schemadb = static::load_from_path($path);
75
                if (!isset($extended_schemadb[$extended_schema_name])) {
76
                    throw new midcom_error('extended schema ' . $path . ':' . $schema_name . ' was not found');
77
                }
78
            }
79
80
            // Override the extended schema with fields from the new schema
81
            foreach ($schema as $key => $value) {
82
                if ($key === 'extends') {
83
                    continue;
84
                }
85
86
                // This is probably either fields or operations
87
                if (is_array($value)) {
88
                    $extended_schemadb[$extended_schema_name][$key] ??= [];
89
90
                    foreach ($value as $name => $field) {
91
                        if (!$field) {
92
                            unset($extended_schemadb[$extended_schema_name][$key][$name]);
93
                            continue;
94
                        }
95
96
                        $extended_schemadb[$extended_schema_name][$key][$name] = $field;
97
                    }
98
                } else {
99
                    $extended_schemadb[$extended_schema_name][$key] = $value;
100
                }
101
            }
102
103
            // Replace the new schema with extended schema
104
            $data[$schema_name] = $extended_schemadb[$extended_schema_name];
105
        }
106
    }
107
108 192
    public function add(string $name, schema $schema)
109
    {
110 192
        $this->schemas[$name] = $schema;
111 192
        $schema->set_name($name);
112
    }
113
114
    /**
115
     * @return schema[]
116
     */
117 34
    public function all() : array
118
    {
119 34
        return $this->schemas;
120
    }
121
122 118
    public function get_first() : schema
123
    {
124 118
        if (empty($this->schemas)) {
125
            throw new midcom_error('Schema DB is empty');
126
        }
127 118
        return reset($this->schemas);
128
    }
129
130 59
    public function has(string $name) : bool
131
    {
132 59
        return array_key_exists($name, $this->schemas);
133
    }
134
135 78
    public function get(string $name) : schema
136
    {
137 78
        if (!array_key_exists($name, $this->schemas)) {
138
            throw new midcom_error('Schema ' . $name . ' not found in schemadb');
139
        }
140 78
        return $this->schemas[$name];
141
    }
142
}
143