Completed
Push — master ( e7f8de...7e52b4 )
by Dmitry
03:22
created

Meta::has()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 1
crap 3
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 64
    public function __construct(Contracts\Manager $manager, array $data = null)
18
    {
19 64
        $this->manager = $manager;
20
21 64
        if($data) {
22 1
            $this->property = $data['property'];
23 1
            $this->indexes = $data['indexes'];
24 1
            $this->types = $data['types'];
25
26 1
        } else {
27 64
            $this->collectData();
28
        }
29 64
    }
30
31
    /**
32
     * @return Type
33
     */
34 64
    public function get($type)
35
    {
36 64
        if (!array_key_exists($type, $this->instances)) {
37 64
            $spaceId = $this->manager->getSchema()->getSpaceId($type);
38 64
            if (!$spaceId) {
39 1
                throw new LogicException("Type $type not exists");
40
            }
41
42 64
            $this->instances[$type] = new Type(
43 64
                $this->manager, $type,
44 64
                $this->property[$spaceId],
45 64
                $this->types[$spaceId],
46 64
                $this->indexes[$spaceId]
47 64
            );
48 64
        }
49
50 64
        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 2
        }
86
87 2
        foreach (array_reverse($instance->getProperties()) as $property) {
88 2
            $instance->removeProperty($property);
89 2
        }
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 1
        }
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 64
    public function create($type, array $fields = null)
107
    {
108 64
        if ($this->manager->getSchema()->hasSpace($type)) {
109 1
            throw new LogicException("Type $type exists");
110
        }
111
112 64
        $this->manager->getSchema()->createSpace($type);
113
114 64
        $instance = new Type($this->manager, $type, [], [], []);
115
116 64
        $instance->addProperty('id');
117 64
        $instance->addIndex('id');
118
119 64
        if ($fields) {
120 64
            foreach ($fields as $index => $field) {
121 64
                if ($field instanceof Contracts\Type) {
122 5
                    if (!is_numeric($index)) {
123 1
                        $instance->reference($field, $index);
124 1
                    } else {
125 4
                        $instance->reference($field);
126
                    }
127 5
                } else {
128 64
                    $instance->addProperty($field);
129
                }
130 64
            }
131 64
        }
132 64
        $this->instances[$type] = $instance;
133
134 64
        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 64
    public function getConvention()
145
    {
146 64
        if (!isset($this->convention)) {
147 64
            $this->convention = new Convention();
148 64
            $this->convention->setNumberType($this->manager->getSchema()->getNumberType());
149 64
        }
150
151 64
        return $this->convention;
152
    }
153
154 64
    private function collectData()
155
    {
156 64
        $client = $this->manager->getClient();
157 64
        foreach ($client->getSpace('property')->select([], 'space')->getData() as $property) {
158 64
            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...
159 64
            if (!array_key_exists($spaceId, $this->property)) {
160 64
                $this->property[$spaceId] = [];
161 64
                $this->types[$spaceId] = [];
162 64
            }
163 64
            $this->property[$spaceId][$line] = $name;
164 64
            $this->types[$spaceId][$name] = $type;
165 64
        }
166 64
        foreach ($client->getSpace('_vindex')->select([], 'primary')->getData() as $index) {
167 64
            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...
168 64
            if (!array_key_exists($spaceId, $this->property)) {
169
                // tarantool space index
170 64
                continue;
171
            }
172 64
            if (!isset($this->indexes[$spaceId])) {
173 64
                $this->indexes[$spaceId] = [];
174 64
            }
175 64
            $this->indexes[$spaceId][$num] = [];
176 64
            foreach ($properties as $row) {
177 64
                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...
178 64
                $this->indexes[$spaceId][$num][] = $this->property[$spaceId][$part];
179 64
            }
180 64
        }
181 64
        foreach ($this->property as $spaceId => $collection) {
182 64
            ksort($collection);
183 64
            $this->property[$spaceId] = $collection;
184 64
        }
185
186 64
    }
187
188 1
    public function toArray()
189
    {
190 1
        $this->collectData();
191
192
        return [
193 1
            'property' => $this->property,
194 1
            'indexes' => $this->indexes,
195 1
            'types' => $this->types,
196 1
        ];
197
    }
198
}
199