Completed
Push — master ( 2bdfc4...eb6d5f )
by Dmitry
03:21
created

Meta::create()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 6

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 0
loc 29
ccs 19
cts 19
cp 1
rs 8.439
cc 6
eloc 17
nc 3
nop 2
crap 6
1
<?php
2
3
namespace Tarantool\Mapper\Schema;
4
5
use Tarantool\Mapper\Contracts;
6
use LogicException;
7
8
class Meta implements Contracts\Meta
9
{
10
    protected $manager;
11
    protected $property = [];
12
    protected $types = [];
13
14 22
    public function __construct(Contracts\Manager $manager)
15
    {
16 22
        $this->manager = $manager;
17 22
        $this->property = [];
18 22
        $this->references = [];
0 ignored issues
show
Bug introduced by
The property references 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...
19
20 22
        $client = $manager->getClient();
21 22
        foreach ($client->getSpace('property')->select([], 'space')->getData() as $property) {
22 22
            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 22
            if (!array_key_exists($spaceId, $this->property)) {
24 22
                $this->property[$spaceId] = [];
25 22
                $this->types[$spaceId] = [];
26 22
            }
27 22
            $this->property[$spaceId][$line] = $name;
28 22
            $this->types[$spaceId][$name] = $type;
29 22
        }
30 22
        foreach ($this->property as $spaceId => $collection) {
31 22
            ksort($collection);
32 22
            $this->property[$spaceId] = $collection;
33 22
        }
34 22
    }
35
36
    /**
37
     * @return Type
38
     */
39 22
    public function get($type)
40
    {
41 22
        if (!array_key_exists($type, $this->types)) {
42 22
            $spaceId = $this->manager->getSchema()->getSpaceId($type);
43 22
            if (!$spaceId) {
44 1
                throw new LogicException("Type $type not exists");
45
            }
46
47 22
            $this->types[$type] = new Type($this->manager, $type, $this->property[$spaceId], $this->types[$spaceId]);
48 22
        }
49
50 22
        return $this->types[$type];
51
    }
52
53
    /**
54
     * @return Type
55
     */
56 22
    public function create($type, array $fields = null)
57
    {
58 22
        if ($this->manager->getSchema()->hasSpace($type)) {
59 1
            throw new LogicException("Type $type exists");
60
        }
61
62 22
        $this->manager->getSchema()->createSpace($type);
63
64 22
        $instance = new Type($this->manager, $type, [], []);
65
66 22
        $instance->addProperty('id');
67 22
        $instance->addIndex('id');
68
69 22
        if ($fields) {
70 22
            foreach ($fields as $index => $field) {
71 22
                if ($field instanceof Contracts\Type) {
72 2
                    if(!is_numeric($index)) {
73 1
                        $instance->reference($field, $index);
74 1
                    } else {
75 1
                        $instance->reference($field);
76
                    }
77 2
                } else {
78 22
                    $instance->addProperty($field);
79
                }
80 22
            }
81 22
        }
82
83 22
        return $this->types[$type] = $instance;
84
    }
85
86
    public function setConvention(Contracts\Convention $convention)
87
    {
88
        $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...
89
90
        return $this;
91
    }
92
93 22
    public function getConvention()
94
    {
95 22
        if (!isset($this->convention)) {
96 22
            $this->convention = new Convention();
97 22
        }
98
99 22
        return $this->convention;
100
    }
101
}
102