Completed
Push — master ( f0ac72...8c37f7 )
by Dmitry
02:48
created

Meta::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

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