Completed
Push — master ( 13dce9...b41dd5 )
by Dmitry
03:01
created

Meta::get()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 5.0073

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 20
ccs 14
cts 15
cp 0.9333
rs 8.8571
cc 5
eloc 12
nc 4
nop 1
crap 5.0073
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 2
    public function __construct(Contracts\Manager $manager)
14
    {
15 2
        $this->manager = $manager;
16 2
    }
17
18
    /**
19
     * @return Type
20
     */
21 2
    public function get($type)
22
    {
23 2
        if (!array_key_exists($type, $this->types)) {
24 2
            if (!$this->manager->getSchema()->hasSpace($type)) {
25
                throw new LogicException("Type $type not exists");
26
            }
27
28 2
            $fields = [];
29 2
            if ($type != 'mapping') {
30 2
                $mapping = $this->manager->get('mapping')->find(['space' => $type]);
31 2
                foreach ($mapping as $row) {
32 2
                    $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 2
                }
34 2
                ksort($fields);
35 2
            }
36
37 2
            $this->types[$type] = new Type($this->manager, $type, array_values($fields));
38 2
        }
39 2
        return $this->types[$type];
40
    }
41
42
    /**
43
     * @return Type
44
     */
45 2
    public function create($type, array $fields = null)
46
    {
47 2
        if ($this->manager->getSchema()->hasSpace($type)) {
48
            throw new LogicException("Type $type exists");
49
        }
50
51 2
        $this->manager->getSchema()->createSpace($type);
52
53 2
        $instance = new Type($this->manager, $type);
54
55 2
        $instance->addProperty('id');
56 2
        $instance->addIndex('id');
57
58 2
        if ($fields) {
59 2
            call_user_func_array([$instance, 'addProperty'], (array) $fields);
60 2
        }
61
62 2
        return $this->types[$type] = $instance;
63
    }
64
}
65