Passed
Push — master ( cae146...a37530 )
by Jonas
06:59
created

belongsToManyOfDescendants()   A

Complexity

Conditions 6
Paths 2

Size

Total Lines 27
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 27
ccs 11
cts 11
cp 1
rs 9.2222
cc 6
nc 2
nop 6
crap 6
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
     * @param bool $inverse
207
     * @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\MorphToManyOfDescendants
208
     */
209 100
    public function morphToManyOfDescendants(
210
        $related,
211
        $name,
212
        $table = null,
213
        $foreignPivotKey = null,
214
        $relatedPivotKey = null,
215
        $parentKey = null,
216
        $relatedKey = null,
217
        $inverse = false
218
    ) {
219 100
        $instance = $this->newRelatedInstance($related);
220
221 100
        $foreignPivotKey = $foreignPivotKey ?: $name.'_id';
222
223 100
        $relatedPivotKey = $relatedPivotKey ?: $instance->getForeignKey();
224
225 100
        if (! $table) {
226 100
            $words = preg_split('/(_)/u', $name, -1, PREG_SPLIT_DELIM_CAPTURE);
227
228 100
            $lastWord = array_pop($words);
229
230 100
            $table = implode('', $words).Str::plural($lastWord);
231
        }
232
233 100
        return $this->newMorphToManyOfDescendants(
234 100
            $instance->newQuery(),
235
            $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

235
            /** @scrutinizer ignore-type */ $this,
Loading history...
236
            $name,
237
            $table,
238
            $foreignPivotKey,
239
            $relatedPivotKey,
240 100
            $parentKey ?: $this->getKeyName(),
241 100
            $relatedKey ?: $instance->getKeyName(),
242
            $inverse,
243 100
            false
244
        );
245
    }
246
247
    /**
248
     * Define a polymorphic many-to-many relationship of the model's descendants and itself.
249
     *
250
     * @param string $related
251
     * @param string $name
252
     * @param string|null $table
253
     * @param string|null $foreignPivotKey
254
     * @param string|null $relatedPivotKey
255
     * @param string|null $parentKey
256
     * @param string|null $relatedKey
257
     * @param bool $inverse
258
     * @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\MorphToManyOfDescendants
259
     */
260 44
    public function morphToManyOfDescendantsAndSelf(
261
        $related,
262
        $name,
263
        $table = null,
264
        $foreignPivotKey = null,
265
        $relatedPivotKey = null,
266
        $parentKey = null,
267
        $relatedKey = null,
268
        $inverse = false
269
    ) {
270 44
        $instance = $this->newRelatedInstance($related);
271
272 44
        $foreignPivotKey = $foreignPivotKey ?: $name.'_id';
273
274 44
        $relatedPivotKey = $relatedPivotKey ?: $instance->getForeignKey();
275
276 44
        if (! $table) {
277 44
            $words = preg_split('/(_)/u', $name, -1, PREG_SPLIT_DELIM_CAPTURE);
278
279 44
            $lastWord = array_pop($words);
280
281 44
            $table = implode('', $words).Str::plural($lastWord);
282
        }
283
284 44
        return $this->newMorphToManyOfDescendants(
285 44
            $instance->newQuery(),
286
            $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

286
            /** @scrutinizer ignore-type */ $this,
Loading history...
287
            $name,
288
            $table,
289
            $foreignPivotKey,
290
            $relatedPivotKey,
291 44
            $parentKey ?: $this->getKeyName(),
292 44
            $relatedKey ?: $instance->getKeyName(),
293
            $inverse,
294 44
            true
295
        );
296
    }
297
298
    /**
299
     * Instantiate a new MorphToManyOfDescendants relationship.
300
     *
301
     * @param \Illuminate\Database\Eloquent\Builder $query
302
     * @param \Illuminate\Database\Eloquent\Model $parent
303
     * @param string $name
304
     * @param string $table
305
     * @param string $foreignPivotKey
306
     * @param string $relatedPivotKey
307
     * @param string $parentKey
308
     * @param string $relatedKey
309
     * @param bool $inverse
310
     * @param bool $andSelf
311
     * @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\MorphToManyOfDescendants
312
     */
313 144
    protected function newMorphToManyOfDescendants(
314
        Builder $query,
315
        Model $parent,
316
        $name,
317
        $table,
318
        $foreignPivotKey,
319
        $relatedPivotKey,
320
        $parentKey,
321
        $relatedKey,
322
        $inverse,
323
        $andSelf
324
    ) {
325 144
        return new MorphToManyOfDescendants(
326 144
            $query,
327
            $parent,
328
            $name,
329
            $table,
330
            $foreignPivotKey,
331
            $relatedPivotKey,
332
            $parentKey,
333
            $relatedKey,
334
            $inverse,
335
            $andSelf
336
        );
337
    }
338
339
    /**
340
     * Define a polymorphic, inverse many-to-many relationship of the model's descendants.
341
     *
342
     * @param string $related
343
     * @param string $name
344
     * @param string|null $table
345
     * @param string|null $foreignPivotKey
346
     * @param string|null $relatedPivotKey
347
     * @param string|null $parentKey
348
     * @param string|null $relatedKey
349
     * @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\MorphToManyOfDescendants
350
     */
351 50
    public function morphedByManyOfDescendants(
352
        $related,
353
        $name,
354
        $table = null,
355
        $foreignPivotKey = null,
356
        $relatedPivotKey = null,
357
        $parentKey = null,
358
        $relatedKey = null
359
    ) {
360 50
        $foreignPivotKey = $foreignPivotKey ?: $this->getForeignKey();
361
362 50
        $relatedPivotKey = $relatedPivotKey ?: $name.'_id';
363
364 50
        return $this->morphToManyOfDescendants(
365 50
            $related,
366
            $name,
367
            $table,
368
            $foreignPivotKey,
369
            $relatedPivotKey,
370
            $parentKey,
371
            $relatedKey,
372 50
            true
373
        );
374
    }
375
376
    /**
377
     * Define a polymorphic, inverse many-to-many relationship of the model's descendants and itself.
378
     *
379
     * @param string $related
380
     * @param string $name
381
     * @param string|null $table
382
     * @param string|null $foreignPivotKey
383
     * @param string|null $relatedPivotKey
384
     * @param string|null $parentKey
385
     * @param string|null $relatedKey
386
     * @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\MorphToManyOfDescendants
387
     */
388 22
    public function morphedByManyOfDescendantsAndSelf(
389
        $related,
390
        $name,
391
        $table = null,
392
        $foreignPivotKey = null,
393
        $relatedPivotKey = null,
394
        $parentKey = null,
395
        $relatedKey = null
396
    ) {
397 22
        $foreignPivotKey = $foreignPivotKey ?: $this->getForeignKey();
398
399 22
        $relatedPivotKey = $relatedPivotKey ?: $name.'_id';
400
401 22
        return $this->morphToManyOfDescendantsAndSelf(
402 22
            $related,
403
            $name,
404
            $table,
405
            $foreignPivotKey,
406
            $relatedPivotKey,
407
            $parentKey,
408
            $relatedKey,
409 22
            true
410
        );
411
    }
412
}
413