Completed
Push — master ( c1fc0d...a7d98d )
by Dmitry
28:31 queued 26:01
created

Meta   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 164
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 7

Test Coverage

Coverage 100%

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 103
cts 103
cp 1
rs 10

7 Methods

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