Test Failed
Pull Request — master (#4)
by
unknown
11:18
created

Model   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 128
Duplicated Lines 15.63 %

Coupling/Cohesion

Components 2
Dependencies 6

Importance

Changes 0
Metric Value
dl 20
loc 128
rs 10
c 0
b 0
f 0
wmc 19
lcom 2
cbo 6

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getCollection() 0 3 1
A setCollection() 0 3 1
A newBaseQueryBuilder() 0 8 1
A qualifyColumn() 0 8 2
A getEntityName() 0 3 1
B newFromBuilder() 0 22 5
A hasOne() 10 10 3
A hasMany() 10 10 3
A newBelongsTo() 0 4 1
A newBelongsToMany() 0 4 1

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
namespace sonrac\Arango\Eloquent;
3
4
use Illuminate\Database\Eloquent\Model as BaseModel;
5
use Illuminate\Support\Str;
6
use sonrac\Arango\Eloquent\Reletations\BelongsTo;
7
use sonrac\Arango\Eloquent\Reletations\BelongsToMany;
8
use function sonrac\Arango\Helpers\getEntityName;
9
use sonrac\Arango\Query\QueryBuilder;
10
use \Illuminate\Database\Eloquent\Builder as BaseBuilder;
11
12
abstract class Model extends BaseModel
13
{
14
    /**
15
     * @inheritdoc
16
     */
17
    protected $primaryKey = '_key';
18
19
    /**
20
     * @inheritdoc
21
     */
22
    protected $keyType = 'string';
23
24
    /**
25
     * Get collection name
26
     *
27
     * @return string
28
     */
29
    function getCollection(){
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
30
        return $this->getTable();
31
    }
32
33
    /**
34
     * Set collection name
35
     *
36
     * @param string $collection
37
     */
38
    function setCollection($collection){
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
39
        $this->table = $collection;
40
    }
41
42
    /**
43
     * @inheritdoc
44
     */
45
    protected function newBaseQueryBuilder()
46
    {
47
        $connection = $this->getConnection();
48
49
        return new QueryBuilder(
50
            $connection, $connection->getQueryGrammar(), $connection->getPostProcessor()
51
        );
52
    }
53
54
    /**
55
     * @inheritdoc
56
     */
57
    public function qualifyColumn($column)
58
    {
59
        if (Str::contains($column, '.')) {
60
            return $column;
61
        }
62
63
        return $this->getEntityName().'.'.$column;
64
    }
65
66
    public function getEntityName(){
67
        return getEntityName($this->getCollection());
68
    }
69
70
    /**
71
     * @inheritdoc
72
     */
73
    public function newFromBuilder($attributes = [], $connection = null)
74
    {
75
        $model = $this->newInstance([], true);
76
77
        $attributesResult = [];
78
        foreach ($attributes as $key => $value){
79
            if(is_array($value) && $this->getEntityName() === $key){
80
81
                $attributesResult = array_merge($attributesResult, $value);
82
                continue;
83
            }
84
            $attributesResult[$key] = $value;
85
        }
86
87
        $model->setRawAttributes((array) $attributesResult, true);
88
89
        $model->setConnection($connection ?: $this->getConnectionName());
90
91
        $model->fireModelEvent('retrieved', false);
92
93
        return $model;
94
    }
95
96
    /**
97
     * @inheritdoc
98
     */
99 View Code Duplication
    public function hasOne($related, $foreignKey = null, $localKey = null)
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...
100
    {
101
        $instance = $this->newRelatedInstance($related);
102
103
        $foreignKey = $foreignKey ?: $this->getForeignKey();
104
105
        $localKey = $localKey ?: $this->getKeyName();
106
107
        return $this->newHasOne($instance->newQuery(), $this, $foreignKey, $localKey);
108
    }
109
110
    /**
111
     * @inheritdoc
112
     */
113 View Code Duplication
    public function hasMany($related, $foreignKey = null, $localKey = null)
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...
114
    {
115
        $instance = $this->newRelatedInstance($related);
116
117
        $foreignKey = $foreignKey ?: $this->getForeignKey();
118
119
        $localKey = $localKey ?: $this->getKeyName();
120
121
        return $this->newHasMany($instance->newQuery(), $this, $foreignKey, $localKey);
122
    }
123
124
    /**
125
     * @inheritdoc
126
     */
127
    function newBelongsTo(BaseBuilder $query, BaseModel $child, $foreignKey, $ownerKey, $relation)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
128
    {
129
        return new BelongsTo($query, $child, $foreignKey, $ownerKey, $relation);
130
    }
131
132
    /**
133
     * @inheritdoc
134
     */
135
    function newBelongsToMany(BaseBuilder $query, BaseModel $parent, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, $relationName = null)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
136
    {
137
        return new BelongsToMany($query, $parent, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, $relationName);
138
    }
139
}
140