Completed
Push — master ( 6ecafd...3d8b1a )
by Dmitry
03:08
created

Select::getBody()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 66
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 66
ccs 2
cts 2
cp 1
rs 9.3191
cc 1
eloc 4
nc 1
nop 0
crap 1

How to fix   Long Method   

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 Basis\Procedure;
4
5
use Tarantool\Mapper\Procedure;
6
7
class Select extends Procedure
8
{
9 5
    public function getParams() : array
10
    {
11 5
        return ['space', 'index', 'values'];
12
    }
13
14 5
    public function getBody(): string
15
    {
16
        return <<<LUA
17 5
        if box.space[space] == nil then
18
            return nil
19
        end
20
        if box.space[space].index[index] == nil then
21
            return nil
22
        end
23
24
        if #values == 0 then
25
            return {}
26
        end
27
28
        local pk = {}
29
        for i, t in pairs(box.space[space].index[0].parts) do
30
            table.insert(pk, t.fieldno)
31
        end
32
33
        local result = {}
34
        local keys = {}
35
36
        local parentField = nil
37
38
        for i, f in pairs(box.space[space]:format()) do
39
            if f.name == 'parent' and f.reference == space then
40
                parentField = i
41
            end
42
        end
43
44
        local childIndex = nil
45
46
        if parentField ~= nil then
47
            for i, candidate in pairs(box.space[space].index) do
48
                if #candidate.parts == 1 then
49
                    if candidate.parts[1].fieldno == parentField then
50
                        childIndex = candidate.name
51
                    end
52
                end
53
            end
54
        end
55
56
        local function selector(index, value)
57
            for j, tuple in box.space[space].index[index]:pairs(value) do
58
                local key = ""
59
                for i, f in pairs(pk) do
60
                    key = key .. tuple[f] .. '-'
61
                end
62
                if keys[key] == nil then
63
                    keys[key] = true
64
                    table.insert(result, tuple)
65
                    if childIndex ~= nil then
66
                        selector(childIndex, tuple[1])
67
                    end
68
                end
69
            end
70
71
        end
72
73
        for i, value in pairs(values) do
74
            selector(index, value)
75
        end
76
77
        return result
78
LUA;
79
    }
80
}
81