Passed
Pull Request — next (#84)
by Bas
15:17 queued 11:12
created

HasAranguentRelationships::newMorphToMany()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 1

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 11
c 3
b 0
f 0
dl 0
loc 23
ccs 12
cts 12
cp 1
rs 9.9
cc 1
nc 1
nop 10
crap 1

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
declare(strict_types=1);
4
5
namespace LaravelFreelancerNL\Aranguent\Eloquent\Concerns;
6
7
use Illuminate\Database\Eloquent\Builder;
8
use Illuminate\Database\Eloquent\Model;
9
use Illuminate\Database\Eloquent\Relations\BelongsTo as IlluminateBelongsTo;
10
use Illuminate\Database\Eloquent\Relations\BelongsToMany as IlluminateBelongsToMany;
11
use Illuminate\Database\Eloquent\Relations\HasMany as IlluminateHasMany;
12
use Illuminate\Database\Eloquent\Relations\HasOne as IlluminateHasOne;
13
use Illuminate\Database\Eloquent\Relations\HasOneThrough as IlluminateHasOneThrough;
14
use Illuminate\Database\Eloquent\Relations\MorphMany as IlluminateMorphMany;
15
use Illuminate\Database\Eloquent\Relations\MorphOne as IlluminateMorphOne;
16
use Illuminate\Database\Eloquent\Relations\MorphTo as IlluminateMorphTo;
17
use Illuminate\Database\Eloquent\Relations\MorphToMany as IlluminateMorphToMany;
18
use LaravelFreelancerNL\Aranguent\Eloquent\Relations\BelongsTo;
19
use LaravelFreelancerNL\Aranguent\Eloquent\Relations\BelongsToMany;
20
use LaravelFreelancerNL\Aranguent\Eloquent\Relations\HasMany;
21
use LaravelFreelancerNL\Aranguent\Eloquent\Relations\HasOne;
22
use LaravelFreelancerNL\Aranguent\Eloquent\Relations\HasOneThrough;
23
use LaravelFreelancerNL\Aranguent\Eloquent\Relations\MorphMany;
24
use LaravelFreelancerNL\Aranguent\Eloquent\Relations\MorphOne;
25
use LaravelFreelancerNL\Aranguent\Eloquent\Relations\MorphTo;
26
use LaravelFreelancerNL\Aranguent\Eloquent\Relations\MorphToMany;
27
28
trait HasAranguentRelationships
29
{
30
    /**
31
     * Instantiate a new BelongsTo relationship.
32
     *
33
     * @param  string  $foreignKey
34
     * @param  string  $ownerKey
35
     * @param  string  $relation
36
     * @return IlluminateBelongsTo
37
     */
38 21
    protected function newBelongsTo(Builder $query, Model $child, $foreignKey, $ownerKey, $relation)
39
    {
40 21
        return new BelongsTo($query, $child, $foreignKey, $ownerKey, $relation);
41
    }
42
43
    /**
44
     * Instantiate a new BelongsToMany relationship.
45
     *
46
     * @param  string  $table
47
     * @param  string  $foreignPivotKey
48
     * @param  string  $relatedPivotKey
49
     * @param  string  $parentKey
50
     * @param  string  $relatedKey
51
     * @param  string|null  $relationName
52
     * @return IlluminateBelongsToMany
53
     */
54 6
    protected function newBelongsToMany(
55
        Builder $query,
56
        Model $parent,
57
        $table,
58
        $foreignPivotKey,
59
        $relatedPivotKey,
60
        $parentKey,
61
        $relatedKey,
62
        $relationName = null
63
    ) {
64 6
        return new BelongsToMany(
65 6
            $query,
66 6
            $parent,
67 6
            $table,
68 6
            $foreignPivotKey,
69 6
            $relatedPivotKey,
70 6
            $parentKey,
71 6
            $relatedKey,
72 6
            $relationName
73 6
        );
74
    }
75
76
    /**
77
     * Instantiate a new HasMany relationship.
78
     *
79
     * @param  string  $foreignKey
80
     * @param  string  $localKey
81
     * @return IlluminateHasMany
82
     */
83 6
    protected function newHasMany(Builder $query, Model $parent, $foreignKey, $localKey)
84
    {
85 6
        return new HasMany($query, $parent, $foreignKey, $localKey);
86
    }
87
88
    /**
89
     * Instantiate a new HasOne relationship.
90
     *
91
     * @param  string  $foreignKey
92
     * @param  string  $localKey
93
     * @return IlluminateHasOne
94
     */
95 10
    protected function newHasOne(Builder $query, Model $parent, $foreignKey, $localKey)
96
    {
97 10
        return new HasOne($query, $parent, $foreignKey, $localKey);
98
    }
99
100
    /**
101
     * Instantiate a new HasOneThrough relationship.
102
     *
103
     * @param  string  $firstKey
104
     * @param  string  $secondKey
105
     * @param  string  $localKey
106
     * @param  string  $secondLocalKey
107
     * @return IlluminateHasOneThrough
108
     */
109
    protected function newHasOneThrough(
110
        Builder $query,
111
        Model $farParent,
112
        Model $throughParent,
113
        $firstKey,
114
        $secondKey,
115
        $localKey,
116
        $secondLocalKey
117
    ) {
118
        return new HasOneThrough($query, $farParent, $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey);
119
    }
120
121
    /**
122
     * Instantiate a new MorphMany relationship.
123
     *
124
     * @param  string  $type
125
     * @param  string  $id
126
     * @param  string  $localKey
127
     * @return IlluminateMorphMany
128
     */
129 3
    protected function newMorphMany(Builder $query, Model $parent, $type, $id, $localKey)
130
    {
131 3
        return new MorphMany($query, $parent, $type, $id, $localKey);
132
    }
133
134
    /**
135
     * Instantiate a new MorphOne relationship.
136
     *
137
     * @param  string  $type
138
     * @param  string  $id
139
     * @param  string  $localKey
140
     * @return IlluminateMorphOne
141
     */
142 3
    protected function newMorphOne(Builder $query, Model $parent, $type, $id, $localKey)
143
    {
144 3
        return new MorphOne($query, $parent, $type, $id, $localKey);
145
    }
146
147
    /**
148
     * Instantiate a new MorphTo relationship.
149
     *
150
     * @param  string  $foreignKey
151
     * @param  string  $ownerKey
152
     * @param  string  $type
153
     * @param  string  $relation
154
     * @return IlluminateMorphTo
155
     */
156 4
    protected function newMorphTo(Builder $query, Model $parent, $foreignKey, $ownerKey, $type, $relation)
157
    {
158 4
        return new MorphTo($query, $parent, $foreignKey, $ownerKey, $type, $relation);
159
    }
160
161
    /**
162
     * Instantiate a new MorphToMany relationship.
163
     *
164
     *  Laravel API PHPMD exclusions
165
     *
166
     *  @SuppressWarnings(PHPMD.BooleanArgumentFlag)
167
     *  @SuppressWarnings(PHPMD.ExcessiveParameterList)
168
     *
169
     * @param  string  $name
170
     * @param  string  $table
171
     * @param  string  $foreignPivotKey
172
     * @param  string  $relatedPivotKey
173
     * @param  string  $parentKey
174
     * @param  string  $relatedKey
175
     * @param  string|null  $relationName
176
     * @param  bool  $inverse
177
     * @return IlluminateMorphToMany
178
     */
179 8
    protected function newMorphToMany(
180
        Builder $query,
181
        Model $parent,
182
        $name,
183
        $table,
184
        $foreignPivotKey,
185
        $relatedPivotKey,
186
        $parentKey,
187
        $relatedKey,
188
        $relationName = null,
189
        $inverse = false
190
    ) {
191 8
        return new MorphToMany(
192 8
            $query,
193 8
            $parent,
194 8
            $name,
195 8
            $table,
196 8
            $foreignPivotKey,
197 8
            $relatedPivotKey,
198 8
            $parentKey,
199 8
            $relatedKey,
200 8
            $relationName,
201 8
            $inverse
202 8
        );
203
    }
204
}
205