Passed
Push — master ( 8d031b...eddbb7 )
by Bas
22:43 queued 12s
created

HasRelationships::newBelongsToMany()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 8
dl 0
loc 11
rs 10

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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
use LaravelFreelancerNL\Aranguent\Eloquent\Relations\BelongsToMany;
14
15
trait HasRelationships
16
{
17
    /**
18
     * Define a one-to-one relationship.
19
     *
20
     * @param  string  $related
21
     * @param  string  $foreignKey
22
     * @param  string  $localKey
23
     * @return HasOne
24
     */
25
    public function hasOne($related, $foreignKey = null, $localKey = null)
26
    {
27
        $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

27
        /** @scrutinizer ignore-call */ 
28
        $instance = $this->newRelatedInstance($related);
Loading history...
28
29
        $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

29
        $foreignKey = $foreignKey ?: $this->/** @scrutinizer ignore-call */ getForeignKey();
Loading history...
30
31
        $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

31
        $localKey = $localKey ?: $this->/** @scrutinizer ignore-call */ getKeyName();
Loading history...
32
33
        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

33
        return $this->/** @scrutinizer ignore-call */ newHasOne($instance->newQuery(), $this, $instance->qualifyColumn($foreignKey), $localKey);
Loading history...
34
    }
35
36
    /**
37
     * Define an inverse one-to-one or many relationship.
38
     *
39
     * @param  string  $related
40
     * @param  string  $foreignKey
41
     * @param  string  $ownerKey
42
     * @param  string  $relation
43
     * @return BelongsTo
44
     */
45
    public function belongsTo($related, $foreignKey = null, $ownerKey = null, $relation = null)
46
    {
47
        // If no relation name was given, we will use this debug backtrace to extract
48
        // the calling method's name and use that as the relationship name as most
49
        // of the time this will be what we desire to use for the relationships.
50
        if (is_null($relation)) {
51
            $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

51
            /** @scrutinizer ignore-call */ 
52
            $relation = $this->guessBelongsToRelation();
Loading history...
52
        }
53
54
        $instance = $this->newRelatedInstance($related);
55
56
        // If no foreign key was supplied, we can use a backtrace to guess the proper
57
        // foreign key name by using the name of the relationship function, which
58
        // when combined with an "_id" should conventionally match the columns.
59
        if (is_null($foreignKey)) {
60
            $foreignKey = Str::snake($relation) . $instance->getKeyName();
61
        }
62
63
        // Once we have the foreign key names, we'll just create a new Eloquent query
64
        // for the related models and returns the relationship instance which will
65
        // actually be responsible for retrieving and hydrating every relations.
66
        $ownerKey = $ownerKey ?: $instance->getKeyName();
67
68
        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

68
        return new BelongsTo($instance->newQuery(), /** @scrutinizer ignore-type */ $this, $foreignKey, $ownerKey, $relation);
Loading history...
69
    }
70
71
    /**
72
     * Define a one-to-many relationship.
73
     *
74
     * @param  string  $related
75
     * @param  string|null  $foreignKey
76
     * @param  string|null  $localKey
77
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
78
     */
79
    public function hasMany($related, $foreignKey = null, $localKey = null)
80
    {
81
        $instance = $this->newRelatedInstance($related);
82
83
        $foreignKey = $foreignKey ?: $this->getForeignKey();
84
85
        $localKey = $localKey ?: $this->getKeyName();
86
87
        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

87
        return $this->/** @scrutinizer ignore-call */ newHasMany($instance->newQuery(), $this, $foreignKey, $localKey);
Loading history...
88
    }
89
90
    /**
91
     * Define a polymorphic one-to-one relationship.
92
     *
93
     * @param  string  $related
94
     * @param  string  $name
95
     * @param  string|null  $type
96
     * @param  string|null  $id
97
     * @param  string|null  $localKey
98
     * @return MorphOne
99
     */
100
    public function morphOne($related, $name, $type = null, $id = null, $localKey = null)
101
    {
102
        $instance = $this->newRelatedInstance($related);
103
104
        [$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

104
        /** @scrutinizer ignore-call */ 
105
        [$type, $id] = $this->getMorphs($name, $type, $id);
Loading history...
105
106
        $localKey = $localKey ?: $this->getKeyName();
107
108
        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

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

169
        return $this->newMorphMany($instance->newQuery(), /** @scrutinizer ignore-type */ $this, $type, $id, $localKey);
Loading history...
170
    }
171
172
    /**
173
     * Instantiate a new MorphMany relationship.
174
     *
175
     * @param  IlluminateBuilder  $query
176
     * @param  IlluminateModel  $parent
177
     * @param  string  $type
178
     * @param  string  $id
179
     * @param  string  $localKey
180
     * @return MorphMany
181
     */
182
    protected function newMorphMany(IlluminateBuilder $query, IlluminateModel $parent, $type, $id, $localKey)
183
    {
184
        return new MorphMany($query, $parent, $type, $id, $localKey);
185
    }
186
187
    /**
188
     * Define a many-to-many relationship.
189
     *
190
     * @param  string  $related
191
     * @param  string|null  $table
192
     * @param  string|null  $foreignPivotKey
193
     * @param  string|null  $relatedPivotKey
194
     * @param  string|null  $parentKey
195
     * @param  string|null  $relatedKey
196
     * @param  string|null  $relation
197
     * @return BelongsToMany
198
     */
199
    public function belongsToMany(
200
        $related,
201
        $table = null,
202
        $foreignPivotKey = null,
203
        $relatedPivotKey = null,
204
        $parentKey = null,
205
        $relatedKey = null,
206
        $relation = null
207
    ) {
208
        // If no relationship name was passed, we will pull backtraces to get the
209
        // name of the calling function. We will use that function name as the
210
        // title of this relation since that is a great convention to apply.
211
        if (is_null($relation)) {
212
            $relation = $this->guessBelongsToManyRelation();
0 ignored issues
show
Bug introduced by
It seems like guessBelongsToManyRelation() 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

212
            /** @scrutinizer ignore-call */ 
213
            $relation = $this->guessBelongsToManyRelation();
Loading history...
213
        }
214
215
        // First, we'll need to determine the foreign key and "other key" for the
216
        // relationship. Once we have determined the keys we'll make the query
217
        // instances as well as the relationship instances we need for this.
218
        $instance = $this->newRelatedInstance($related);
219
220
        $foreignPivotKey = $foreignPivotKey ?: $this->getForeignKey();
221
222
        $relatedPivotKey = $relatedPivotKey ?: $instance->getForeignKey();
223
        // If no table name was provided, we can guess it by concatenating the two
224
        // models using underscores in alphabetical order. The two model names
225
        // are transformed to snake case from their default CamelCase also.
226
        if (is_null($table)) {
227
            $table = $this->joiningTable($related, $instance);
0 ignored issues
show
Bug introduced by
It seems like joiningTable() 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

227
            /** @scrutinizer ignore-call */ 
228
            $table = $this->joiningTable($related, $instance);
Loading history...
228
        }
229
230
        return $this->newBelongsToMany(
231
            $instance->newQuery(),
232
            $this,
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...ips::newBelongsToMany(). ( Ignorable by Annotation )

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

232
            /** @scrutinizer ignore-type */ $this,
Loading history...
233
            $table,
234
            $foreignPivotKey,
235
            $relatedPivotKey,
236
            $parentKey ?: $this->getKeyName(),
237
            $relatedKey ?: $instance->getKeyName(),
238
            $relation
239
        );
240
    }
241
242
    /**
243
     * Instantiate a new BelongsToMany relationship.
244
     *
245
     * @param  IlluminateBuilder  $query
246
     * @param  IlluminateModel  $parent
247
     * @param  string  $table
248
     * @param  string  $foreignPivotKey
249
     * @param  string  $relatedPivotKey
250
     * @param  string  $parentKey
251
     * @param  string  $relatedKey
252
     * @param  string|null  $relationName
253
     * @return BelongsToMany
254
     */
255
    protected function newBelongsToMany(
256
        IlluminateBuilder $query,
257
        IlluminateModel $parent,
258
        $table,
259
        $foreignPivotKey,
260
        $relatedPivotKey,
261
        $parentKey,
262
        $relatedKey,
263
        $relationName = null
264
    ) {
265
        return new BelongsToMany($query, $parent, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, $relationName);
266
    }
267
}
268