Completed
Push — master ( e93ee4...0d9d3f )
by Dmitry
03:35
created

Meta::create()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 6

Importance

Changes 6
Bugs 0 Features 3
Metric Value
c 6
b 0
f 3
dl 0
loc 30
ccs 16
cts 16
cp 1
rs 8.439
cc 6
eloc 18
nc 3
nop 2
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
16 40
    public function __construct(Contracts\Manager $manager)
17
    {
18 40
        $this->manager = $manager;
19
20 40
        $client = $manager->getClient();
21 40
        foreach ($client->getSpace('property')->select([], 'space')->getData() as $property) {
22 40
            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 40
            if (!array_key_exists($spaceId, $this->property)) {
24 40
                $this->property[$spaceId] = [];
25 40
                $this->types[$spaceId] = [];
26
            }
27 40
            $this->property[$spaceId][$line] = $name;
28 40
            $this->types[$spaceId][$name] = $type;
29
        }
30 40
        foreach ($client->getSpace('_vindex')->select([], 'primary')->getData() as $index) {
31 40
            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 40
            if (!array_key_exists($spaceId, $this->property)) {
33
                // tarantool space index
34 40
                continue;
35
            }
36 40
            if (!isset($this->indexes[$spaceId])) {
37 40
                $this->indexes[$spaceId] = [];
38
            }
39 40
            $this->indexes[$spaceId][$num] = [];
40 40
            foreach ($properties as $row) {
41 40
                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 40
                $this->indexes[$spaceId][$num][] = $this->property[$spaceId][$part];
43
            }
44
        }
45 40
        foreach ($this->property as $spaceId => $collection) {
46 40
            ksort($collection);
47 40
            $this->property[$spaceId] = $collection;
48
        }
49 40
    }
50
51
    /**
52
     * @return Type
53
     */
54 40
    public function get($type)
55
    {
56 40
        if (!array_key_exists($type, $this->types)) {
57 40
            $spaceId = $this->manager->getSchema()->getSpaceId($type);
58 40
            if (!$spaceId) {
59 1
                throw new LogicException("Type $type not exists");
60
            }
61
62 40
            $this->types[$type] = new Type(
63 40
                $this->manager, $type,
64 40
                $this->property[$spaceId],
65 40
                $this->types[$spaceId],
66 40
                $this->indexes[$spaceId]
67
            );
68
        }
69
70 40
        return $this->types[$type];
71
    }
72
73 3
    public function has($type)
74
    {
75
        // was created
76 3
        if (array_key_exists($type, $this->types)) {
77 1
            return true;
78
        }
79
80
        // can be created
81 3
        $spaceId = $this->manager->getSchema()->getSpaceId($type);
82 3
        if (array_key_exists($spaceId, $this->property)) {
83 1
            return true;
84
        }
85
86 3
        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
        }
106
107 2
        foreach (array_reverse($instance->getProperties()) as $property) {
108 2
            $instance->removeProperty($property);
109
        }
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
        }
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 40
    public function create($type, array $fields = null)
127
    {
128 40
        if ($this->manager->getSchema()->hasSpace($type)) {
129 1
            throw new LogicException("Type $type exists");
130
        }
131
132 40
        $this->manager->getSchema()->createSpace($type);
133
134 40
        $instance = new Type($this->manager, $type, [], [], []);
135
136 40
        $instance->addProperty('id');
137 40
        $instance->addIndex('id');
138
139 40
        if ($fields) {
140 40
            foreach ($fields as $index => $field) {
141 40
                if ($field instanceof Contracts\Type) {
142 4
                    if (!is_numeric($index)) {
143 1
                        $instance->reference($field, $index);
144
                    } else {
145 4
                        $instance->reference($field);
146
                    }
147
                } else {
148 40
                    $instance->addProperty($field);
149
                }
150
            }
151
        }
152 40
        $this->types[$type] = $instance;
153
154 40
        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 40
    public function getConvention()
165
    {
166 40
        if (!isset($this->convention)) {
167 40
            $this->convention = new Convention();
168
        }
169
170 40
        return $this->convention;
171
    }
172
}
173