Completed
Branch feature/pre-split (ca29cf)
by Anton
03:23
created

RecordSchema   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 134
Duplicated Lines 8.96 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 12
loc 134
rs 10
c 0
b 0
f 0
wmc 16
lcom 1
cbo 2

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getClass() 0 4 1
A getReflection() 0 4 1
A getInstantiator() 0 4 1
A getDatabase() 0 10 2
A getTable() 0 11 2
A getFields() 12 12 3
A getRelations() 0 4 1
A defineTable() 0 4 1
A packSchema() 0 4 1
A isRelation() 0 8 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * components
4
 *
5
 * @author    Wolfy-J
6
 */
7
namespace Spiral\ORM\Schemas;
8
9
use Doctrine\Common\Inflector\Inflector;
10
use Spiral\Database\Schemas\Prototypes\AbstractTable;
11
use Spiral\Models\Reflections\ReflectionEntity;
12
use Spiral\ORM\Configs\MutatorsConfig;
13
use Spiral\ORM\Entities\RecordInstantiator;
14
15
class RecordSchema implements SchemaInterface
16
{
17
    /**
18
     * @var ReflectionEntity
19
     */
20
    private $reflection;
21
22
    /**
23
     * @invisible
24
     * @var MutatorsConfig
25
     */
26
    private $mutatorsConfig;
27
28
    /**
29
     * @param ReflectionEntity $reflection
30
     * @param MutatorsConfig   $mutators
31
     */
32
    public function __construct(ReflectionEntity $reflection, MutatorsConfig $mutators)
33
    {
34
        $this->reflection = $reflection;
35
        $this->mutatorsConfig = $mutators;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function getClass(): string
42
    {
43
        return $this->reflection->getName();
44
    }
45
46
    /**
47
     * @return ReflectionEntity
48
     */
49
    public function getReflection(): ReflectionEntity
50
    {
51
        return $this->reflection;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function getInstantiator(): string
58
    {
59
        return $this->reflection->getProperty('instantiator') ?? RecordInstantiator::class;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function getDatabase()
66
    {
67
        $database = $this->reflection->getProperty('database');
68
        if (empty($database)) {
69
            //Empty database to be used
70
            return null;
71
        }
72
73
        return $database;
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    public function getTable(): string
80
    {
81
        $table = $this->reflection->getProperty('table');
82
        if (empty($table)) {
83
            //Generate collection using short class name
84
            $table = Inflector::camelize($this->reflection->getShortName());
85
            $table = Inflector::pluralize($table);
86
        }
87
88
        return $table;
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     *
94
     * //todo: do we need it?
95
     */
96 View Code Duplication
    public function getFields(): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
97
    {
98
        $fields = $this->reflection->getFields();
99
100
        foreach ($fields as $field => $type) {
101
            if ($this->isRelation($type)) {
102
                unset($fields[$field]);
103
            }
104
        }
105
106
        return $fields;
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112
    public function getRelations(): array
113
    {
114
        return [];
115
    }
116
117
    /**
118
     * {@inheritdoc}
119
     */
120
    public function defineTable(AbstractTable $table): AbstractTable
121
    {
122
        return $table;
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128
    public function packSchema(SchemaBuilder $builder, AbstractTable $table = null): array
129
    {
130
        return [];
131
    }
132
133
    /**
134
     * Check if field schema/type defines relation.
135
     *
136
     * @param mixed $type
137
     *
138
     * @return bool
139
     */
140
    protected function isRelation($type): bool
141
    {
142
        if (is_array($type)) {
143
            return true;
144
        }
145
146
        return false;
147
    }
148
}