Completed
Push — master ( 6d4316...84d771 )
by Dmitry
03:34
created

Meta::remove()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 32
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 6

Importance

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