Completed
Branch master (0bf65c)
by Dmitry
02:31
created

Meta::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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