Passed
Push — master ( 0ba402...934d75 )
by Bas
05:33
created

HasRelationships   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 170
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 27
dl 0
loc 170
rs 10
c 2
b 0
f 0
wmc 17

8 Methods

Rating   Name   Duplication   Size   Complexity  
A hasMany() 0 9 3
A belongsTo() 0 24 4
A hasOne() 0 9 3
A newMorphTo() 0 9 1
A morphMany() 0 12 2
A newMorphMany() 0 3 1
A morphOne() 0 9 2
A newMorphOne() 0 3 1
1
<?php
2
3
namespace LaravelFreelancerNL\Aranguent\Eloquent\Concerns;
4
5
use Illuminate\Database\Eloquent\Builder as IlluminateBuilder;
6
use Illuminate\Database\Eloquent\Model as IlluminateModel;
7
use Illuminate\Support\Str;
8
use LaravelFreelancerNL\Aranguent\Eloquent\Relations\BelongsTo;
9
use LaravelFreelancerNL\Aranguent\Eloquent\Relations\HasOne;
10
use LaravelFreelancerNL\Aranguent\Eloquent\Relations\MorphOne;
11
use LaravelFreelancerNL\Aranguent\Eloquent\Relations\MorphTo;
12
use LaravelFreelancerNL\Aranguent\Eloquent\Relations\MorphMany;
13
14
trait HasRelationships
15
{
16
    /**
17
     * Define a one-to-one relationship.
18
     *
19
     * @param  string  $related
20
     * @param  string  $foreignKey
21
     * @param  string  $localKey
22
     * @return HasOne
23
     */
24
    public function hasOne($related, $foreignKey = null, $localKey = null)
25
    {
26
        $instance = $this->newRelatedInstance($related);
0 ignored issues
show
Bug introduced by
It seems like newRelatedInstance() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

26
        /** @scrutinizer ignore-call */ 
27
        $instance = $this->newRelatedInstance($related);
Loading history...
27
28
        $foreignKey = $foreignKey ?: $this->getForeignKey();
0 ignored issues
show
Bug introduced by
It seems like getForeignKey() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

28
        $foreignKey = $foreignKey ?: $this->/** @scrutinizer ignore-call */ getForeignKey();
Loading history...
29
30
        $localKey = $localKey ?: $this->getKeyName();
0 ignored issues
show
Bug introduced by
It seems like getKeyName() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

30
        $localKey = $localKey ?: $this->/** @scrutinizer ignore-call */ getKeyName();
Loading history...
31
32
        return $this->newHasOne($instance->newQuery(), $this, $instance->qualifyColumn($foreignKey), $localKey);
0 ignored issues
show
Bug introduced by
It seems like newHasOne() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

32
        return $this->/** @scrutinizer ignore-call */ newHasOne($instance->newQuery(), $this, $instance->qualifyColumn($foreignKey), $localKey);
Loading history...
33
    }
34
35
    /**
36
     * Define an inverse one-to-one or many relationship.
37
     *
38
     * @param  string  $related
39
     * @param  string  $foreignKey
40
     * @param  string  $ownerKey
41
     * @param  string  $relation
42
     * @return BelongsTo
43
     */
44
    public function belongsTo($related, $foreignKey = null, $ownerKey = null, $relation = null)
45
    {
46
        // If no relation name was given, we will use this debug backtrace to extract
47
        // the calling method's name and use that as the relationship name as most
48
        // of the time this will be what we desire to use for the relationships.
49
        if (is_null($relation)) {
50
            $relation = $this->guessBelongsToRelation();
0 ignored issues
show
Bug introduced by
It seems like guessBelongsToRelation() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

50
            /** @scrutinizer ignore-call */ 
51
            $relation = $this->guessBelongsToRelation();
Loading history...
51
        }
52
53
        $instance = $this->newRelatedInstance($related);
54
55
        // If no foreign key was supplied, we can use a backtrace to guess the proper
56
        // foreign key name by using the name of the relationship function, which
57
        // when combined with an "_id" should conventionally match the columns.
58
        if (is_null($foreignKey)) {
59
            $foreignKey = Str::snake($relation) . $instance->getKeyName();
60
        }
61
62
        // Once we have the foreign key names, we'll just create a new Eloquent query
63
        // for the related models and returns the relationship instance which will
64
        // actually be responsible for retrieving and hydrating every relations.
65
        $ownerKey = $ownerKey ?: $instance->getKeyName();
66
67
        return new BelongsTo($instance->newQuery(), $this, $foreignKey, $ownerKey, $relation);
0 ignored issues
show
Bug introduced by
$this of type LaravelFreelancerNL\Aran...ncerns\HasRelationships is incompatible with the type Illuminate\Database\Eloquent\Model expected by parameter $child of LaravelFreelancerNL\Aran...elongsTo::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

67
        return new BelongsTo($instance->newQuery(), /** @scrutinizer ignore-type */ $this, $foreignKey, $ownerKey, $relation);
Loading history...
68
    }
69
70
    /**
71
     * Define a one-to-many relationship.
72
     *
73
     * @param  string  $related
74
     * @param  string|null  $foreignKey
75
     * @param  string|null  $localKey
76
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
77
     */
78
    public function hasMany($related, $foreignKey = null, $localKey = null)
79
    {
80
        $instance = $this->newRelatedInstance($related);
81
82
        $foreignKey = $foreignKey ?: $this->getForeignKey();
83
84
        $localKey = $localKey ?: $this->getKeyName();
85
86
        return $this->newHasMany($instance->newQuery(), $this, $foreignKey, $localKey);
0 ignored issues
show
Bug introduced by
It seems like newHasMany() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

86
        return $this->/** @scrutinizer ignore-call */ newHasMany($instance->newQuery(), $this, $foreignKey, $localKey);
Loading history...
87
    }
88
89
    /**
90
     * Define a polymorphic one-to-one relationship.
91
     *
92
     * @param  string  $related
93
     * @param  string  $name
94
     * @param  string|null  $type
95
     * @param  string|null  $id
96
     * @param  string|null  $localKey
97
     * @return MorphOne
98
     */
99
    public function morphOne($related, $name, $type = null, $id = null, $localKey = null)
100
    {
101
        $instance = $this->newRelatedInstance($related);
102
103
        [$type, $id] = $this->getMorphs($name, $type, $id);
0 ignored issues
show
Bug introduced by
It seems like getMorphs() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

103
        /** @scrutinizer ignore-call */ 
104
        [$type, $id] = $this->getMorphs($name, $type, $id);
Loading history...
104
105
        $localKey = $localKey ?: $this->getKeyName();
106
107
        return $this->newMorphOne($instance->newQuery(), $this, $type, $id, $localKey);
0 ignored issues
show
Bug introduced by
$this of type LaravelFreelancerNL\Aran...ncerns\HasRelationships is incompatible with the type Illuminate\Database\Eloquent\Model expected by parameter $parent of LaravelFreelancerNL\Aran...ionships::newMorphOne(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

107
        return $this->newMorphOne($instance->newQuery(), /** @scrutinizer ignore-type */ $this, $type, $id, $localKey);
Loading history...
108
    }
109
110
    /**
111
     * Instantiate a new MorphOne relationship.
112
     *
113
     * @param  IlluminateBuilder  $query
114
     * @param  IlluminateModel  $parent
115
     * @param  string  $type
116
     * @param  string  $id
117
     * @param  string  $localKey
118
     * @return MorphOne
119
     */
120
    protected function newMorphOne(IlluminateBuilder $query, IlluminateModel $parent, $type, $id, $localKey)
121
    {
122
        return new MorphOne($query, $parent, $type, $id, $localKey);
123
    }
124
125
    /**
126
     * Instantiate a new MorphTo relationship.
127
     *
128
     * @param  IlluminateBuilder  $query
129
     * @param  IlluminateModel  $parent
130
     * @param  string  $foreignKey
131
     * @param  string  $ownerKey
132
     * @param  string  $type
133
     * @param  string  $relation
134
     * @return MorphTo
135
     */
136
    protected function newMorphTo(
137
        IlluminateBuilder $query,
138
        IlluminateModel $parent,
139
        $foreignKey,
140
        $ownerKey,
141
        $type,
142
        $relation
143
    ) {
144
        return new MorphTo($query, $parent, $foreignKey, $ownerKey, $type, $relation);
145
    }
146
147
    /**
148
     * Define a polymorphic one-to-many relationship.
149
     *
150
     * @param  string  $related
151
     * @param  string  $name
152
     * @param  string|null  $type
153
     * @param  string|null  $id
154
     * @param  string|null  $localKey
155
     * @return MorphMany
156
     */
157
    public function morphMany($related, $name, $type = null, $id = null, $localKey = null)
158
    {
159
        $instance = $this->newRelatedInstance($related);
160
161
        // Here we will gather up the morph type and ID for the relationship so that we
162
        // can properly query the intermediate table of a relation. Finally, we will
163
        // get the table and create the relationship instances for the developers.
164
        [$type, $id] = $this->getMorphs($name, $type, $id);
165
166
        $localKey = $localKey ?: $this->getKeyName();
167
168
        return $this->newMorphMany($instance->newQuery(), $this, $type, $id, $localKey);
0 ignored issues
show
Bug introduced by
$this of type LaravelFreelancerNL\Aran...ncerns\HasRelationships is incompatible with the type Illuminate\Database\Eloquent\Model expected by parameter $parent of LaravelFreelancerNL\Aran...onships::newMorphMany(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

168
        return $this->newMorphMany($instance->newQuery(), /** @scrutinizer ignore-type */ $this, $type, $id, $localKey);
Loading history...
169
    }
170
171
    /**
172
     * Instantiate a new MorphMany relationship.
173
     *
174
     * @param  IlluminateBuilder  $query
175
     * @param  IlluminateModel  $parent
176
     * @param  string  $type
177
     * @param  string  $id
178
     * @param  string  $localKey
179
     * @return MorphMany
180
     */
181
    protected function newMorphMany(IlluminateBuilder $query, IlluminateModel $parent, $type, $id, $localKey)
182
    {
183
        return new MorphMany($query, $parent, $type, $id, $localKey);
184
    }
185
}
186