Passed
Push — master ( aa1f26...3776bc )
by Jonas
06:48
created

newBelongsToManyOfDescendants()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 19
ccs 3
cts 3
cp 1
rs 9.9666
cc 1
nc 1
nop 8
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
namespace Staudenmeir\LaravelAdjacencyList\Eloquent;
4
5
use Illuminate\Database\Eloquent\Builder;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Staudenmeir\LaravelAdjacencyList\Eloquent\Builder. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Support\Str;
8
use Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\BelongsToManyOfDescendants;
9
use Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\HasManyOfDescendants;
10
use Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\MorphToManyOfDescendants;
11
12
trait HasOfDescendantsRelationships
13
{
14
    /**
15
     * Define a one-to-many relationship of the model's descendants.
16
     *
17
     * @param string $related
18
     * @param string|null $foreignKey
19
     * @param string|null $localKey
20
     * @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\HasManyOfDescendants
21
     */
22 54
    public function hasManyOfDescendants($related, $foreignKey = null, $localKey = null)
23
    {
24 54
        $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

24
        /** @scrutinizer ignore-call */ 
25
        $instance = $this->newRelatedInstance($related);
Loading history...
25
26 54
        $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

26
        $foreignKey = $foreignKey ?: $this->/** @scrutinizer ignore-call */ getForeignKey();
Loading history...
27
28 54
        $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

28
        $localKey = $localKey ?: $this->/** @scrutinizer ignore-call */ getKeyName();
Loading history...
29
30 54
        return $this->newHasManyOfDescendants(
31 54
            $instance->newQuery(),
32
            $this,
0 ignored issues
show
Bug introduced by
$this of type Staudenmeir\LaravelAdjac...escendantsRelationships is incompatible with the type Illuminate\Database\Eloquent\Model expected by parameter $parent of Staudenmeir\LaravelAdjac...wHasManyOfDescendants(). ( Ignorable by Annotation )

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

32
            /** @scrutinizer ignore-type */ $this,
Loading history...
33 54
            $instance->qualifyColumn($foreignKey),
34
            $localKey,
35 54
            false
36
        );
37
    }
38
39
    /**
40
     * Define a one-to-many relationship of the model's descendants and itself.
41
     *
42
     * @param string $related
43
     * @param string|null $foreignKey
44
     * @param string|null $localKey
45
     * @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\HasManyOfDescendants
46
     */
47 22
    public function hasManyOfDescendantsAndSelf($related, $foreignKey = null, $localKey = null)
48
    {
49 22
        $instance = $this->newRelatedInstance($related);
50
51 22
        $foreignKey = $foreignKey ?: $this->getForeignKey();
52
53 22
        $localKey = $localKey ?: $this->getKeyName();
54
55 22
        return $this->newHasManyOfDescendants(
56 22
            $instance->newQuery(),
57
            $this,
0 ignored issues
show
Bug introduced by
$this of type Staudenmeir\LaravelAdjac...escendantsRelationships is incompatible with the type Illuminate\Database\Eloquent\Model expected by parameter $parent of Staudenmeir\LaravelAdjac...wHasManyOfDescendants(). ( Ignorable by Annotation )

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

57
            /** @scrutinizer ignore-type */ $this,
Loading history...
58 22
            $instance->qualifyColumn($foreignKey),
59
            $localKey,
60 22
            true
61
        );
62
    }
63
64
    /**
65
     * Instantiate a new HasManyOfDescendants relationship.
66
     *
67
     * @param \Illuminate\Database\Eloquent\Builder $query
68
     * @param \Illuminate\Database\Eloquent\Model $parent
69
     * @param string $foreignKey
70
     * @param string $localKey
71
     * @param bool $andSelf
72
     * @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\HasManyOfDescendants
73
     */
74 76
    protected function newHasManyOfDescendants(Builder $query, Model $parent, $foreignKey, $localKey, $andSelf)
75
    {
76 76
        return new HasManyOfDescendants($query, $parent, $foreignKey, $localKey, $andSelf);
77
    }
78
79
    /**
80
     * Define a many-to-many relationship of the model's descendants.
81
     *
82
     * @param string $related
83
     * @param string|null $table
84
     * @param string|null $foreignPivotKey
85
     * @param string|null $relatedPivotKey
86
     * @param string|null $parentKey
87
     * @param string|null $relatedKey
88
     * @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\BelongsToManyOfDescendants
89
     */
90 50
    public function belongsToManyOfDescendants(
91
        $related,
92
        $table = null,
93
        $foreignPivotKey = null,
94
        $relatedPivotKey = null,
95
        $parentKey = null,
96
        $relatedKey = null
97
    ) {
98 50
        $instance = $this->newRelatedInstance($related);
99
100 50
        $foreignPivotKey = $foreignPivotKey ?: $this->getForeignKey();
101
102 50
        $relatedPivotKey = $relatedPivotKey ?: $instance->getForeignKey();
103
104 50
        if (is_null($table)) {
105 50
            $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

105
            /** @scrutinizer ignore-call */ 
106
            $table = $this->joiningTable($related, $instance);
Loading history...
106
        }
107
108 50
        return $this->newBelongsToManyOfDescendants(
109 50
            $instance->newQuery(),
110
            $this,
0 ignored issues
show
Bug introduced by
$this of type Staudenmeir\LaravelAdjac...escendantsRelationships is incompatible with the type Illuminate\Database\Eloquent\Model expected by parameter $parent of Staudenmeir\LaravelAdjac...gsToManyOfDescendants(). ( Ignorable by Annotation )

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

110
            /** @scrutinizer ignore-type */ $this,
Loading history...
111
            $table,
112
            $foreignPivotKey,
113
            $relatedPivotKey,
114 50
            $parentKey ?: $this->getKeyName(),
115 50
            $relatedKey ?: $instance->getKeyName(),
116 50
            false
117
        );
118
    }
119
120
    /**
121
     * Define a many-to-many relationship of the model's descendants and itself.
122
     *
123
     * @param string $related
124
     * @param string|null $table
125
     * @param string|null $foreignPivotKey
126
     * @param string|null $relatedPivotKey
127
     * @param string|null $parentKey
128
     * @param string|null $relatedKey
129
     * @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\BelongsToManyOfDescendants
130
     */
131 22
    public function belongsToManyOfDescendantsAndSelf(
132
        $related,
133
        $table = null,
134
        $foreignPivotKey = null,
135
        $relatedPivotKey = null,
136
        $parentKey = null,
137
        $relatedKey = null
138
    ) {
139 22
        $instance = $this->newRelatedInstance($related);
140
141 22
        $foreignPivotKey = $foreignPivotKey ?: $this->getForeignKey();
142
143 22
        $relatedPivotKey = $relatedPivotKey ?: $instance->getForeignKey();
144
145 22
        if (is_null($table)) {
146 22
            $table = $this->joiningTable($related, $instance);
147
        }
148
149 22
        return $this->newBelongsToManyOfDescendants(
150 22
            $instance->newQuery(),
151
            $this,
0 ignored issues
show
Bug introduced by
$this of type Staudenmeir\LaravelAdjac...escendantsRelationships is incompatible with the type Illuminate\Database\Eloquent\Model expected by parameter $parent of Staudenmeir\LaravelAdjac...gsToManyOfDescendants(). ( Ignorable by Annotation )

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

151
            /** @scrutinizer ignore-type */ $this,
Loading history...
152
            $table,
153
            $foreignPivotKey,
154
            $relatedPivotKey,
155 22
            $parentKey ?: $this->getKeyName(),
156 22
            $relatedKey ?: $instance->getKeyName(),
157 22
            true
158
        );
159
    }
160
161
    /**
162
     * Instantiate a new BelongsToManyOfDescendants relationship.
163
     *
164
     * @param \Illuminate\Database\Eloquent\Builder $query
165
     * @param \Illuminate\Database\Eloquent\Model $parent
166
     * @param string $table
167
     * @param string $foreignPivotKey
168
     * @param string $relatedPivotKey
169
     * @param string $parentKey
170
     * @param string $relatedKey
171
     * @param bool $andSelf
172
     * @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\BelongsToManyOfDescendants
173
     */
174 72
    protected function newBelongsToManyOfDescendants(
175
        Builder $query,
176
        Model $parent,
177
        $table,
178
        $foreignPivotKey,
179
        $relatedPivotKey,
180
        $parentKey,
181
        $relatedKey,
182
        $andSelf
183
    ) {
184 72
        return new BelongsToManyOfDescendants(
185 72
            $query,
186
            $parent,
187
            $table,
188
            $foreignPivotKey,
189
            $relatedPivotKey,
190
            $parentKey,
191
            $relatedKey,
192
            $andSelf
193
        );
194
    }
195
196
    /**
197
     * Define a polymorphic many-to-many relationship of the model's descendants.
198
     *
199
     * @param string $related
200
     * @param string $name
201
     * @param string|null $table
202
     * @param string|null $foreignPivotKey
203
     * @param string|null $relatedPivotKey
204
     * @param string|null $parentKey
205
     * @param string|null $relatedKey
206
     * @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\MorphToManyOfDescendants
207
     */
208 50
    public function morphToManyOfDescendants(
209
        $related,
210
        $name,
211
        $table = null,
212
        $foreignPivotKey = null,
213
        $relatedPivotKey = null,
214
        $parentKey = null,
215
        $relatedKey = null
216
    ) {
217 50
        $instance = $this->newRelatedInstance($related);
218
219 50
        $foreignPivotKey = $foreignPivotKey ?: $name.'_id';
220
221 50
        $relatedPivotKey = $relatedPivotKey ?: $instance->getForeignKey();
222
223 50
        if (! $table) {
224 50
            $words = preg_split('/(_)/u', $name, -1, PREG_SPLIT_DELIM_CAPTURE);
225
226 50
            $lastWord = array_pop($words);
227
228 50
            $table = implode('', $words).Str::plural($lastWord);
229
        }
230
231 50
        return $this->newMorphToManyOfDescendants(
232 50
            $instance->newQuery(),
233
            $this,
0 ignored issues
show
Bug introduced by
$this of type Staudenmeir\LaravelAdjac...escendantsRelationships is incompatible with the type Illuminate\Database\Eloquent\Model expected by parameter $parent of Staudenmeir\LaravelAdjac...phToManyOfDescendants(). ( Ignorable by Annotation )

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

233
            /** @scrutinizer ignore-type */ $this,
Loading history...
234
            $name,
235
            $table,
236
            $foreignPivotKey,
237
            $relatedPivotKey,
238 50
            $parentKey ?: $this->getKeyName(),
239 50
            $relatedKey ?: $instance->getKeyName(),
240 50
            false
241
        );
242
    }
243
244
    /**
245
     * Define a polymorphic many-to-many relationship of the model's descendants and itself.
246
     *
247
     * @param string $related
248
     * @param string $name
249
     * @param string|null $table
250
     * @param string|null $foreignPivotKey
251
     * @param string|null $relatedPivotKey
252
     * @param string|null $parentKey
253
     * @param string|null $relatedKey
254
     * @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\MorphToManyOfDescendants
255
     */
256 22
    public function morphToManyOfDescendantsAndSelf(
257
        $related,
258
        $name,
259
        $table = null,
260
        $foreignPivotKey = null,
261
        $relatedPivotKey = null,
262
        $parentKey = null,
263
        $relatedKey = null
264
    ) {
265 22
        $instance = $this->newRelatedInstance($related);
266
267 22
        $foreignPivotKey = $foreignPivotKey ?: $name.'_id';
268
269 22
        $relatedPivotKey = $relatedPivotKey ?: $instance->getForeignKey();
270
271 22
        if (! $table) {
272 22
            $words = preg_split('/(_)/u', $name, -1, PREG_SPLIT_DELIM_CAPTURE);
273
274 22
            $lastWord = array_pop($words);
275
276 22
            $table = implode('', $words).Str::plural($lastWord);
277
        }
278
279 22
        return $this->newMorphToManyOfDescendants(
280 22
            $instance->newQuery(),
281
            $this,
0 ignored issues
show
Bug introduced by
$this of type Staudenmeir\LaravelAdjac...escendantsRelationships is incompatible with the type Illuminate\Database\Eloquent\Model expected by parameter $parent of Staudenmeir\LaravelAdjac...phToManyOfDescendants(). ( Ignorable by Annotation )

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

281
            /** @scrutinizer ignore-type */ $this,
Loading history...
282
            $name,
283
            $table,
284
            $foreignPivotKey,
285
            $relatedPivotKey,
286 22
            $parentKey ?: $this->getKeyName(),
287 22
            $relatedKey ?: $instance->getKeyName(),
288 22
            true
289
        );
290
    }
291
292
    /**
293
     * Instantiate a new MorphToManyOfDescendants relationship.
294
     *
295
     * @param \Illuminate\Database\Eloquent\Builder $query
296
     * @param \Illuminate\Database\Eloquent\Model $parent
297
     * @param string $name
298
     * @param string $table
299
     * @param string $foreignPivotKey
300
     * @param string $relatedPivotKey
301
     * @param string $parentKey
302
     * @param string $relatedKey
303
     * @param bool $andSelf
304
     * @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\MorphToManyOfDescendants
305
     */
306 72
    protected function newMorphToManyOfDescendants(
307
        Builder $query,
308
        Model $parent,
309
        $name,
310
        $table,
311
        $foreignPivotKey,
312
        $relatedPivotKey,
313
        $parentKey,
314
        $relatedKey,
315
        $andSelf
316
    ) {
317 72
        return new MorphToManyOfDescendants(
318 72
            $query,
319
            $parent,
320
            $name,
321
            $table,
322
            $foreignPivotKey,
323
            $relatedPivotKey,
324
            $parentKey,
325
            $relatedKey,
326
            $andSelf
327
        );
328
    }
329
}
330