Completed
Push — master ( af98e0...8c0891 )
by Dmitry
07:01
created

Schema   B

Complexity

Total Complexity 44

Size/Duplication

Total Lines 167
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 44
c 0
b 0
f 0
lcom 1
cbo 4
dl 0
loc 167
rs 8.3396

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A createSpace() 0 17 2
C getDefaultValue() 0 26 12
C formatValue() 0 27 13
B getSpace() 0 17 5
A getSpaceId() 0 7 2
A getSpaces() 0 7 2
A hasSpace() 0 4 1
A once() 0 10 2
A reset() 0 11 1
A getMeta() 0 12 2

How to fix   Complexity   

Complex Class

Complex classes like Schema often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Schema, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Tarantool\Mapper;
4
5
use Exception;
6
7
class Schema
8
{
9
    private $mapper;
10
11
    private $names = [];
12
    private $spaces = [];
13
    private $params = [];
14
15
    public function __construct(Mapper $mapper, $meta = null)
16
    {
17
        $this->mapper = $mapper;
18
        if ($meta) {
19
            $this->names = $meta['names'];
20
            $this->params = $meta['params'];
21
        } else {
22
            $this->reset();
23
        }
24
    }
25
26
    public function createSpace($space, $properties = null)
27
    {
28
        $id = $this->mapper->getClient()->evaluate("
29
            box.schema.space.create('$space')
30
            return box.space.$space.id
31
        ")->getData()[0];
32
33
        $this->names[$space] = $id;
34
35
        $this->spaces[$id] = new Space($this->mapper, $id, $space);
36
37
        if ($properties) {
38
            $this->spaces[$id]->addProperties($properties);
39
        }
40
41
        return $this->spaces[$id];
42
    }
43
44
    public function getDefaultValue($type)
45
    {
46
        switch ($type) {
47
            case 'STR':
48
            case 'STRING':
49
            case 'str':
50
            case 'string':
51
                return (string) null;
52
53
            case 'double':
54
            case 'float':
55
            case 'number':
56
                return (float) null;
57
58
            case 'unsigned':
59
            case 'UNSIGNED':
60
            case 'num':
61
            case 'NUM':
62
                return (int) null;
63
64
            default:
65
                return $value;
0 ignored issues
show
Bug introduced by
The variable $value does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
66
        }
67
68
        return $this->formatValue($type, null, true);
0 ignored issues
show
Unused Code introduced by
return $this->formatValue($type, null, true); does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
69
    }
70
71
    public function formatValue($type, $value)
72
    {
73
        if(is_null($value)) {
74
            return null;
75
        }
76
        switch ($type) {
77
            case 'STR':
78
            case 'STRING':
79
            case 'str':
80
            case 'string':
81
                return (string) $value;
82
83
            case 'double':
84
            case 'float':
85
            case 'number':
86
                return (float) $value;
87
88
            case 'unsigned':
89
            case 'UNSIGNED':
90
            case 'num':
91
            case 'NUM':
92
                return (int) $value;
93
94
            default:
95
                return $value;
96
        }
97
    }
98
99
    public function getSpace($id)
100
    {
101
        if (is_string($id)) {
102
            return $this->getSpace($this->getSpaceId($id));
103
        }
104
105
        if (!$id) {
106
            throw new Exception("Space id or name not defined");
107
        }
108
109
        if (!array_key_exists($id, $this->spaces)) {
110
            $name = array_search($id, $this->names);
111
            $meta = array_key_exists($id, $this->params) ? $this->params[$id] : null;
112
            $this->spaces[$id] = new Space($this->mapper, $id, $name, $meta);
113
        }
114
        return $this->spaces[$id];
115
    }
116
117
    public function getSpaceId($name)
118
    {
119
        if (!$this->hasSpace($name)) {
120
            throw new Exception("No space $name");
121
        }
122
        return $this->names[$name];
123
    }
124
125
    public function getSpaces()
126
    {
127
        foreach ($this->names as $id) {
128
            $this->getSpace($id);
129
        }
130
        return $this->spaces;
131
    }
132
133
    public function hasSpace($name)
134
    {
135
        return array_key_exists($name, $this->names);
136
    }
137
138
    public function once($name, $callback)
139
    {
140
        $key = 'once' . $name;
141
142
        $rows = $this->mapper->find('_schema', ['key' => $key]);
143
        if (!count($rows)) {
144
            $this->mapper->create('_schema', ['key' => $key]);
145
            return $callback($this->mapper);
146
        }
147
    }
148
149
    public function reset()
150
    {
151
        $this->names = $this->mapper->getClient()->evaluate("
152
            local spaces = {}
153
            local i, s
154
            for i, s in box.space._vspace:pairs() do
155
                spaces[s[3]] = s[1]
156
            end
157
            return spaces
158
        ")->getData()[0];
159
    }
160
161
    public function getMeta()
162
    {
163
        $params = [];
164
        foreach ($this->getSpaces() as $space) {
165
            $params[$space->getId()] = $space->getMeta();
166
        }
167
168
        return [
169
            'names' => $this->names,
170
            'params' => $params
171
        ];
172
    }
173
}
174