Completed
Push — master ( 229d8c...e7f8de )
by Dmitry
02:48
created

Meta::getConvention()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 7
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
crap 6
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
    protected $instances = [];
16
17
    public function __construct(Contracts\Manager $manager, array $data = null)
18
    {
19
        $this->manager = $manager;
20
21
        if($data) {
22
            $this->property = $data['property'];
23
            $this->indexes = $data['indexes'];
24
            $this->types = $data['types'];
25
26
        } else {
27
            $this->collectData();
28
        }
29
    }
30
31
    /**
32
     * @return Type
33
     */
34
    public function get($type)
35
    {
36
        if (!array_key_exists($type, $this->instances)) {
37
            $spaceId = $this->manager->getSchema()->getSpaceId($type);
38
            if (!$spaceId) {
39
                throw new LogicException("Type $type not exists");
40
            }
41
42
            $this->instances[$type] = new Type(
43
                $this->manager, $type,
44
                $this->property[$spaceId],
45
                $this->types[$spaceId],
46
                $this->indexes[$spaceId]
47
            );
48
        }
49
50
        return $this->instances[$type];
51
    }
52
53
    public function has($type)
54
    {
55
        // was created
56
        if (array_key_exists($type, $this->instances)) {
57
            return true;
58
        }
59
60
        // can be created
61
        $spaceId = $this->manager->getSchema()->getSpaceId($type);
62
        if (array_key_exists($spaceId, $this->property)) {
63
            return true;
64
        }
65
66
        return false;
67
    }
68
69
    public function remove($type)
70
    {
71
        $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...
72
        if (count($other)) {
73
            $name = $this->manager->getSchema()->getSpaceName($other[0]->space);
74
            throw new Exception("Space $name references ".$type);
75
        }
76
        $instance = $this->get($type);
77
        $rows = $instance->getSpace()->select([])->getData();
78
        if (count($rows)) {
79
            throw new Exception("Can't remove non-empty space $type");
80
        }
81
82
        $indexes = array_reverse(array_keys($instance->getIndexes()));
83
        foreach ($indexes as $index) {
84
            $instance->dropIndex($index);
85
        }
86
87
        foreach (array_reverse($instance->getProperties()) as $property) {
88
            $instance->removeProperty($property);
89
        }
90
91
        $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...
92
        if ($sq) {
93
            $this->manager->remove($sq);
94
            $this->manager->get('sequence')->flushCache();
95
        }
96
97
        $this->manager->getSchema()->dropSpace($type);
98
        unset($this->instances[$type]);
99
100
        $this->manager->forgetRepository($type);
101
    }
102
103
    /**
104
     * @return Type
105
     */
106
    public function create($type, array $fields = null)
107
    {
108
        if ($this->manager->getSchema()->hasSpace($type)) {
109
            throw new LogicException("Type $type exists");
110
        }
111
112
        $this->manager->getSchema()->createSpace($type);
113
114
        $instance = new Type($this->manager, $type, [], [], []);
115
116
        $instance->addProperty('id');
117
        $instance->addIndex('id');
118
119
        if ($fields) {
120
            foreach ($fields as $index => $field) {
121
                if ($field instanceof Contracts\Type) {
122
                    if (!is_numeric($index)) {
123
                        $instance->reference($field, $index);
124
                    } else {
125
                        $instance->reference($field);
126
                    }
127
                } else {
128
                    $instance->addProperty($field);
129
                }
130
            }
131
        }
132
        $this->instances[$type] = $instance;
133
134
        return $instance;
135
    }
136
137
    public function setConvention(Contracts\Convention $convention)
138
    {
139
        $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...
140
141
        return $this;
142
    }
143
144
    public function getConvention()
145
    {
146
        if (!isset($this->convention)) {
147
            $this->convention = new Convention();
148
        }
149
150
        return $this->convention;
151
    }
152
153
    private function collectData()
154
    {
155
        $client = $this->manager->getClient();
156
        foreach ($client->getSpace('property')->select([], 'space')->getData() as $property) {
157
            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...
158
            if (!array_key_exists($spaceId, $this->property)) {
159
                $this->property[$spaceId] = [];
160
                $this->types[$spaceId] = [];
161
            }
162
            $this->property[$spaceId][$line] = $name;
163
            $this->types[$spaceId][$name] = $type;
164
        }
165
        foreach ($client->getSpace('_vindex')->select([], 'primary')->getData() as $index) {
166
            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...
167
            if (!array_key_exists($spaceId, $this->property)) {
168
                // tarantool space index
169
                continue;
170
            }
171
            if (!isset($this->indexes[$spaceId])) {
172
                $this->indexes[$spaceId] = [];
173
            }
174
            $this->indexes[$spaceId][$num] = [];
175
            foreach ($properties as $row) {
176
                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...
177
                $this->indexes[$spaceId][$num][] = $this->property[$spaceId][$part];
178
            }
179
        }
180
        foreach ($this->property as $spaceId => $collection) {
181
            ksort($collection);
182
            $this->property[$spaceId] = $collection;
183
        }
184
185
    }
186
187
    public function toArray()
188
    {
189
        $this->collectData();
190
191
        return [
192
            'property' => $this->property,
193
            'indexes' => $this->indexes,
194
            'types' => $this->types,
195
        ];
196
    }
197
}
198