Completed
Push — master ( 762e93...98e9e2 )
by Dmitry
32:47 queued 02:55
created

Schema   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 119
Duplicated Lines 22.69 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 13
Bugs 0 Features 2
Metric Value
wmc 23
c 13
b 0
f 2
lcom 1
cbo 3
dl 27
loc 119
ccs 0
cts 94
cp 0
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 2
A getSpaceId() 13 13 4
A getSpaceName() 14 14 4
A hasSpace() 0 4 1
A createSpace() 0 4 1
A dropSpace() 0 5 1
A hasIndex() 0 7 1
A listIndexes() 0 14 3
B createIndex() 0 21 5
A dropIndex() 0 8 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Tarantool\Mapper\Schema;
4
5
use Tarantool\Client;
6
use Tarantool\Mapper\Contracts;
7
use Tarantool\Schema\Space;
8
use Tarantool\Schema\Index;
9
10
class Schema implements Contracts\Schema
11
{
12
    protected $client;
13
    protected $spaceSpace;
14
    protected $indexSpace;
15
    protected $spaceId = [];
16
17
    public function __construct(Client $client)
18
    {
19
        $this->client = $client;
20
21
        $this->spaceSpace = $client->getSpace('_vspace');
22
        $this->indexSpace = $client->getSpace('_vindex');
23
        $spaces = $this->spaceSpace->select([])->getData();
24
        foreach ($spaces as $row) {
0 ignored issues
show
Bug introduced by
The expression $spaces of type null|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
25
            list($id, $sys, $name) = $row;
0 ignored issues
show
Unused Code introduced by
The assignment to $sys is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
26
            $this->spaceId[$name] = $id;
27
        }
28
    }
29
30 View Code Duplication
    public function getSpaceId($space)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
31
    {
32
        if (!array_key_exists($space, $this->spaceId)) {
33
            $response = $this->spaceSpace->select([$space], Index::SPACE_NAME);
34
            $data = $response->getData();
35
            if (!empty($data)) {
36
                $this->spaceId[$space] = $data[0][0];
37
            }
38
        }
39
        if (array_key_exists($space, $this->spaceId)) {
40
            return $this->spaceId[$space];
41
        }
42
    }
43
44 View Code Duplication
    public function getSpaceName($spaceId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
45
    {
46
        if (!in_array($spaceId, $this->spaceId)) {
47
            $response = $this->spaceSpace->select([$spaceId], 0);
48
            $data = $response->getData();
49
            if (!empty($data)) {
50
                $this->spaceId[$data[0][2]] = $spaceId;
51
            }
52
        }
53
54
        if (in_array($spaceId, $this->spaceId)) {
55
            return array_search($spaceId, $this->spaceId);
56
        }
57
    }
58
59
    public function hasSpace($space)
60
    {
61
        return $this->getSpaceId($space) !== null;
62
    }
63
64
    public function createSpace($space)
65
    {
66
        $this->client->evaluate("box.schema.space.create('$space')");
67
    }
68
69
    public function dropSpace($space)
70
    {
71
        $this->client->evaluate('box.schema.space.drop('.$this->getSpaceId($space).')');
72
        unset($this->spaceId[$space]);
73
    }
74
75
    public function hasIndex($space, $index)
76
    {
77
        $spaceId = $this->getSpaceId($space);
78
        $response = $this->indexSpace->select([$spaceId, $index], Index::INDEX_NAME);
79
80
        return !empty($response->getData());
81
    }
82
83
    public function listIndexes($space)
84
    {
85
        $result = [];
86
        $response = $this->indexSpace->select([$this->getSpaceId($space)], Index::INDEX_NAME);
87
88
        foreach ($response->getData() as $row) {
0 ignored issues
show
Bug introduced by
The expression $response->getData() of type null|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
89
            $result[$row[2]] = [];
90
            foreach ($row[5] as $f) {
91
                $result[$row[2]][] = $f[0];
92
            }
93
        }
94
95
        return $result;
96
    }
97
98
    public function createIndex($space, $index, array $arguments)
99
    {
100
        $config = [];
101
        foreach ($arguments as $k => $v) {
102
            if (is_array($v)) {
103
                // convert to lua array
104
                $v = str_replace(['[', ']'], ['{', '}'], json_encode($v));
105
            }
106
            if (is_bool($v)) {
107
                $v = $v ? 'true' : 'false';
108
            }
109
            $config[] = $k.' = '.$v;
110
        }
111
        $config = '{'.implode(', ', $config).'}';
112
        $this->client->evaluate("box.space.$space:create_index('$index', $config)");
113
114
        $schema = $this->client->getSpace(Space::VINDEX);
115
        $response = $schema->select([$this->getSpaceId($space), $index], Index::INDEX_NAME);
116
117
        return $response->getData()[0][1];
118
    }
119
120
    public function dropIndex($spaceId, $index)
121
    {
122
        $space = $this->client->getSpace('_vindex');
123
        $row = $space->select([$spaceId, $index])->getData();
124
        $spaceName = $this->getSpaceName($spaceId);
125
        $indexName = $row[0][2];
126
        $this->client->evaluate("box.space.$spaceName.index.$indexName:drop{}");
127
    }
128
}
129