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

Meta   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 164
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 17
Bugs 0 Features 6
Metric Value
wmc 29
c 17
b 0
f 6
lcom 2
cbo 7
dl 0
loc 164
ccs 0
cts 124
cp 0
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
C __construct() 0 34 8
A get() 0 18 3
A has() 0 15 3
B remove() 0 33 6
B create() 0 30 6
A setConvention() 0 6 1
A getConvention() 0 8 2
1
<?php
2
3
namespace Tarantool\Mapper\Schema;
4
5
use Tarantool\Mapper\Contracts;
6
use Exception;
7
use LogicException;
8
9
class Meta implements Contracts\Meta
10
{
11
    protected $manager;
12
    protected $property = [];
13
    protected $indexes = [];
14
    protected $types = [];
15
16
    public function __construct(Contracts\Manager $manager)
17
    {
18
        $this->manager = $manager;
19
20
        $client = $manager->getClient();
21
        foreach ($client->getSpace('property')->select([], 'space')->getData() as $property) {
22
            list($id, $spaceId, $line, $name, $type) = $property;
0 ignored issues
show
Unused Code introduced by
The assignment to $id 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...
23
            if (!array_key_exists($spaceId, $this->property)) {
24
                $this->property[$spaceId] = [];
25
                $this->types[$spaceId] = [];
26
            }
27
            $this->property[$spaceId][$line] = $name;
28
            $this->types[$spaceId][$name] = $type;
29
        }
30
        foreach ($client->getSpace('_vindex')->select([], 'primary')->getData() as $index) {
31
            list($spaceId, $num, $name, $type, $params, $properties) = $index;
0 ignored issues
show
Unused Code introduced by
The assignment to $name 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...
Unused Code introduced by
The assignment to $type 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...
Unused Code introduced by
The assignment to $params 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...
32
            if (!array_key_exists($spaceId, $this->property)) {
33
                // tarantool space index
34
                continue;
35
            }
36
            if (!isset($this->indexes[$spaceId])) {
37
                $this->indexes[$spaceId] = [];
38
            }
39
            $this->indexes[$spaceId][$num] = [];
40
            foreach ($properties as $row) {
41
                list($part, $type) = $row;
0 ignored issues
show
Unused Code introduced by
The assignment to $type 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...
42
                $this->indexes[$spaceId][$num][] = $this->property[$spaceId][$part];
43
            }
44
        }
45
        foreach ($this->property as $spaceId => $collection) {
46
            ksort($collection);
47
            $this->property[$spaceId] = $collection;
48
        }
49
    }
50
51
    /**
52
     * @return Type
53
     */
54
    public function get($type)
55
    {
56
        if (!array_key_exists($type, $this->types)) {
57
            $spaceId = $this->manager->getSchema()->getSpaceId($type);
58
            if (!$spaceId) {
59
                throw new LogicException("Type $type not exists");
60
            }
61
62
            $this->types[$type] = new Type(
63
                $this->manager, $type,
64
                $this->property[$spaceId],
65
                $this->types[$spaceId],
66
                $this->indexes[$spaceId]
67
            );
68
        }
69
70
        return $this->types[$type];
71
    }
72
73
    public function has($type)
74
    {
75
        // was created
76
        if (array_key_exists($type, $this->types)) {
77
            return true;
78
        }
79
80
        // can be created
81
        $spaceId = $this->manager->getSchema()->getSpaceId($type);
82
        if (array_key_exists($spaceId, $this->property)) {
83
            return true;
84
        }
85
86
        return false;
87
    }
88
89
    public function remove($type)
90
    {
91
        $other = $this->manager->get('property')->find(['type' => $type]);
0 ignored issues
show
Bug introduced by
The method find does only exist in Tarantool\Mapper\Contracts\Repository, but not in Tarantool\Mapper\Contracts\Entity.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
92
        if (count($other)) {
93
            $name = $this->manager->getSchema()->getSpaceName($other[0]->space);
94
            throw new Exception("Space $name references ".$type);
95
        }
96
        $instance = $this->get($type);
97
        $rows = $instance->getSpace()->select([])->getData();
98
        if (count($rows)) {
99
            throw new Exception("Can't remove non-empty space $type");
100
        }
101
102
        $indexes = array_reverse(array_keys($instance->getIndexes()));
103
        foreach ($indexes as $index) {
104
            $instance->dropIndex($index);
105
        }
106
107
        foreach (array_reverse($instance->getProperties()) as $property) {
108
            $instance->removeProperty($property);
109
        }
110
111
        $sq = $this->manager->get('sequence')->findOne(['space' => $instance->getSpaceId()]);
0 ignored issues
show
Bug introduced by
The method findOne does only exist in Tarantool\Mapper\Contracts\Repository, but not in Tarantool\Mapper\Contracts\Entity.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
112
        if ($sq) {
113
            $this->manager->remove($sq);
114
            $this->manager->get('sequence')->flushCache();
115
        }
116
117
        $this->manager->getSchema()->dropSpace($type);
118
        unset($this->types[$type]);
119
120
        $this->manager->forgetRepository($type);
121
    }
122
123
    /**
124
     * @return Type
125
     */
126
    public function create($type, array $fields = null)
127
    {
128
        if ($this->manager->getSchema()->hasSpace($type)) {
129
            throw new LogicException("Type $type exists");
130
        }
131
132
        $this->manager->getSchema()->createSpace($type);
133
134
        $instance = new Type($this->manager, $type, [], [], []);
135
136
        $instance->addProperty('id');
137
        $instance->addIndex('id');
138
139
        if ($fields) {
140
            foreach ($fields as $index => $field) {
141
                if ($field instanceof Contracts\Type) {
142
                    if (!is_numeric($index)) {
143
                        $instance->reference($field, $index);
144
                    } else {
145
                        $instance->reference($field);
146
                    }
147
                } else {
148
                    $instance->addProperty($field);
149
                }
150
            }
151
        }
152
        $this->types[$type] = $instance;
153
154
        return $instance;
155
    }
156
157
    public function setConvention(Contracts\Convention $convention)
158
    {
159
        $this->convention = $convention;
0 ignored issues
show
Bug introduced by
The property convention does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
160
161
        return $this;
162
    }
163
164
    public function getConvention()
165
    {
166
        if (!isset($this->convention)) {
167
            $this->convention = new Convention();
168
        }
169
170
        return $this->convention;
171
    }
172
}
173