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

Schema::getDefaultValue()   C

Complexity

Conditions 12
Paths 12

Size

Total Lines 26
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 26
rs 5.1612
cc 12
eloc 19
nc 12
nop 1

How to fix   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
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