Completed
Push — master ( addaf0...c57d80 )
by Dmitry
03:20
created

Meta::get()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 20
ccs 0
cts 18
cp 0
rs 8.8571
cc 5
eloc 12
nc 4
nop 1
crap 30
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 $types = [];
12
13
    public function __construct(Contracts\Manager $manager)
14
    {
15
        $this->manager = $manager;
16
    }
17
18
    /**
19
     * @return Type
20
     */
21
    public function get($type)
22
    {
23
        if (!array_key_exists($type, $this->types)) {
24
            if (!$this->manager->getSchema()->hasSpace($type)) {
25
                throw new LogicException("Type $type not exists");
26
            }
27
28
            $fields = [];
29
            if ($type != 'mapping') {
30
                $mapping = $this->manager->get('mapping')->find(['space' => $type]);
31
                foreach ($mapping as $row) {
32
                    $fields[$row->line] = $row->property;
0 ignored issues
show
Bug introduced by
Accessing line on the interface Tarantool\Mapper\Contracts\Entity suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
Bug introduced by
Accessing property on the interface Tarantool\Mapper\Contracts\Entity suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
33
                }
34
                ksort($fields);
35
            }
36
37
            $this->types[$type] = new Type($this->manager, $type, array_values($fields));
38
        }
39
        return $this->types[$type];
40
    }
41
42
    /**
43
     * @return Type
44
     */
45
    public function create($type, array $fields = null)
46
    {
47
        if ($this->manager->getSchema()->hasSpace($type)) {
48
            throw new LogicException("Type $type exists");
49
        }
50
51
        $this->manager->getSchema()->createSpace($type);
52
53
        $instance = new Type($this->manager, $type);
54
55
        $instance->addProperty('id');
56
        $instance->addIndex('id');
57
58
        if ($fields) {
59
            call_user_func_array([$instance, 'addProperty'], (array) $fields);
60
        }
61
62
        return $this->types[$type] = $instance;
63
    }
64
}
65