Completed
Branch master (eb6d5f)
by Dmitry
02:18
created

Schema   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 94
Duplicated Lines 28.72 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 88.33%

Importance

Changes 9
Bugs 0 Features 0
Metric Value
wmc 20
c 9
b 0
f 0
lcom 2
cbo 3
dl 27
loc 94
ccs 53
cts 60
cp 0.8833
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A hasSpace() 0 4 1
A createSpace() 0 4 1
A hasIndex() 0 7 1
A getSpaceId() 13 13 4
A getSpaceName() 14 14 4
A listIndexes() 0 14 3
B createIndex() 0 16 5

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 22
    public function __construct(Client $client)
18
    {
19 22
        $this->client = $client;
20
21 22
        $this->spaceSpace = $client->getSpace('_vspace');
22 22
        $this->indexSpace = $client->getSpace('_vindex');
23 22
    }
24
25 22 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...
26
    {
27 22
        if (!array_key_exists($space, $this->spaceId)) {
28 22
            $response = $this->spaceSpace->select([$space], Index::SPACE_NAME);
29 22
            $data = $response->getData();
30 22
            if (!empty($data)) {
31 22
                $this->spaceId[$space] = $data[0][0];
32 22
            }
33 22
        }
34 22
        if (array_key_exists($space, $this->spaceId)) {
35 22
            return $this->spaceId[$space];
36
        }
37 22
    }
38
39 1 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...
40
    {
41 1
        if (!in_array($spaceId, $this->spaceId)) {
42
            $response = $this->spaceSpace->select([$spaceId], 0);
43
            $data = $response->getData();
44
            if (!empty($data)) {
45
                $this->spaceId[$data[0][2]] = $spaceId;
46
            }
47
        }
48
49 1
        if (in_array($spaceId, $this->spaceId)) {
50 1
            return array_search($spaceId, $this->spaceId);
51
        }
52
    }
53
54 22
    public function hasSpace($space)
55
    {
56 22
        return $this->getSpaceId($space) !== null;
57
    }
58
59 22
    public function createSpace($space)
60
    {
61 22
        $this->client->evaluate("box.schema.space.create('$space')");
62 22
    }
63
64 22
    public function hasIndex($space, $index)
65
    {
66 22
        $spaceId = $this->getSpaceId($space);
67 22
        $response = $this->indexSpace->select([$spaceId, $index], Index::INDEX_NAME);
68
69 22
        return !empty($response->getData());
70
    }
71
72 22
    public function listIndexes($space)
73
    {
74 22
        $result = [];
75 22
        $response = $this->indexSpace->select([$this->getSpaceId($space)], Index::INDEX_NAME);
76
77 22
        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...
78 22
            $result[$row[2]] = [];
79 22
            foreach ($row[5] as $f) {
80 22
                $result[$row[2]][] = $f[0];
81 22
            }
82 22
        }
83
84 22
        return $result;
85
    }
86
87 22
    public function createIndex($space, $index, array $arguments)
88
    {
89 22
        $config = [];
90 22
        foreach ($arguments as $k => $v) {
91 22
            if (is_array($v)) {
92
                // convert to lua array
93 22
                $v = str_replace(['[', ']'], ['{', '}'], json_encode($v));
94 22
            }
95 22
            if (is_bool($v)) {
96 22
                $v = $v ? 'true' : 'false';
97 22
            }
98 22
            $config[] = $k.' = '.$v;
99 22
        }
100 22
        $config = '{'.implode(', ', $config).'}';
101 22
        $this->client->evaluate("box.space.$space:create_index('$index', $config)");
102 22
    }
103
}
104