Passed
Push — master ( 98eee9...aa1f26 )
by Jonas
09:56
created

HasRecursiveRelationships::getPathName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
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\Ancestors;
9
use Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\BelongsToManyOfDescendants;
10
use Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Bloodline;
11
use Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Descendants;
12
use Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\HasManyOfDescendants;
13
use Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\MorphToManyOfDescendants;
14
use Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\RootAncestor;
15
use Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Siblings;
16
use Staudenmeir\LaravelCte\Eloquent\QueriesExpressions;
17
18
trait HasRecursiveRelationships
19
{
20
    use HasRecursiveRelationshipScopes;
21
    use QueriesExpressions;
22
23
    /**
24
     * Get the name of the parent key column.
25
     *
26
     * @return string
27
     */
28 484
    public function getParentKeyName()
29
    {
30 484
        return 'parent_id';
31
    }
32
33
    /**
34
     * Get the qualified parent key column.
35
     *
36
     * @return string
37
     */
38 226
    public function getQualifiedParentKeyName()
39
    {
40 226
        return (new static())->getTable().'.'.$this->getParentKeyName();
0 ignored issues
show
Bug introduced by
It seems like getTable() 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

40
        return (new static())->/** @scrutinizer ignore-call */ getTable().'.'.$this->getParentKeyName();
Loading history...
41
    }
42
43
    /**
44
     * Get the name of the local key column.
45
     *
46
     * @return string
47
     */
48 456
    public function getLocalKeyName()
49
    {
50 456
        return $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

50
        return $this->/** @scrutinizer ignore-call */ getKeyName();
Loading history...
51
    }
52
53
    /**
54
     * Get the qualified local key column.
55
     *
56
     * @return string
57
     */
58 248
    public function getQualifiedLocalKeyName()
59
    {
60 248
        return $this->qualifyColumn($this->getLocalKeyName());
0 ignored issues
show
Bug introduced by
It seems like qualifyColumn() 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

60
        return $this->/** @scrutinizer ignore-call */ qualifyColumn($this->getLocalKeyName());
Loading history...
61
    }
62
63
    /**
64
     * Get the name of the depth column.
65
     *
66
     * @return string
67
     */
68 214
    public function getDepthName()
69
    {
70 214
        return 'depth';
71
    }
72
73
    /**
74
     * Get the name of the path column.
75
     *
76
     * @return string
77
     */
78 318
    public function getPathName()
79
    {
80 318
        return 'path';
81
    }
82
83
    /**
84
     * Get the path separator.
85
     *
86
     * @return string
87
     */
88 262
    public function getPathSeparator()
89
    {
90 262
        return '.';
91
    }
92
93
    /**
94
     * Get the additional custom paths.
95
     *
96
     * @return array
97
     */
98 214
    public function getCustomPaths()
99
    {
100 214
        return [];
101
    }
102
103
    /**
104
     * Get the name of the common table expression.
105
     *
106
     * @return string
107
     */
108 416
    public function getExpressionName()
109
    {
110 416
        return 'laravel_cte';
111
    }
112
113
    /**
114
     * Get the model's ancestors.
115
     *
116
     * @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Ancestors
117
     */
118 50
    public function ancestors()
119
    {
120 50
        return $this->newAncestors(
121 50
            (new static())->newQuery(),
0 ignored issues
show
Bug introduced by
It seems like newQuery() 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

121
            (new static())->/** @scrutinizer ignore-call */ newQuery(),
Loading history...
122
            $this,
0 ignored issues
show
Bug introduced by
$this of type Staudenmeir\LaravelAdjac...sRecursiveRelationships is incompatible with the type Illuminate\Database\Eloquent\Model expected by parameter $parent of Staudenmeir\LaravelAdjac...onships::newAncestors(). ( Ignorable by Annotation )

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

122
            /** @scrutinizer ignore-type */ $this,
Loading history...
123 50
            $this->getQualifiedParentKeyName(),
124 50
            $this->getLocalKeyName(),
125 50
            false
126
        );
127
    }
128
129
    /**
130
     * Get the model's ancestors and itself.
131
     *
132
     * @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Ancestors
133
     */
134 26
    public function ancestorsAndSelf()
135
    {
136 26
        return $this->newAncestors(
137 26
            (new static())->newQuery(),
138
            $this,
0 ignored issues
show
Bug introduced by
$this of type Staudenmeir\LaravelAdjac...sRecursiveRelationships is incompatible with the type Illuminate\Database\Eloquent\Model expected by parameter $parent of Staudenmeir\LaravelAdjac...onships::newAncestors(). ( Ignorable by Annotation )

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

138
            /** @scrutinizer ignore-type */ $this,
Loading history...
139 26
            $this->getQualifiedParentKeyName(),
140 26
            $this->getLocalKeyName(),
141 26
            true
142
        );
143
    }
144
145
    /**
146
     * Instantiate a new Ancestors relationship.
147
     *
148
     * @param \Illuminate\Database\Eloquent\Builder $query
149
     * @param \Illuminate\Database\Eloquent\Model $parent
150
     * @param string $foreignKey
151
     * @param string $localKey
152
     * @param bool $andSelf
153
     * @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Ancestors
154
     */
155 76
    protected function newAncestors(Builder $query, Model $parent, $foreignKey, $localKey, $andSelf)
156
    {
157 76
        return new Ancestors($query, $parent, $foreignKey, $localKey, $andSelf);
158
    }
159
160
    /**
161
     * Get the model's bloodline.
162
     *
163
     * @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Bloodline
164
     */
165 22
    public function bloodline()
166
    {
167 22
        return $this->newBloodline(
168 22
            (new static())->newQuery(),
169
            $this,
0 ignored issues
show
Bug introduced by
$this of type Staudenmeir\LaravelAdjac...sRecursiveRelationships is incompatible with the type Illuminate\Database\Eloquent\Model expected by parameter $parent of Staudenmeir\LaravelAdjac...onships::newBloodline(). ( Ignorable by Annotation )

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

169
            /** @scrutinizer ignore-type */ $this,
Loading history...
170 22
            $this->getQualifiedParentKeyName(),
171 22
            $this->getLocalKeyName()
172
        );
173
    }
174
175
    /**
176
     * Instantiate a new Bloodline relationship.
177
     *
178
     * @param \Illuminate\Database\Eloquent\Builder $query
179
     * @param \Illuminate\Database\Eloquent\Model $parent
180
     * @param string $foreignKey
181
     * @param string $localKey
182
     * @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Bloodline
183
     */
184 22
    protected function newBloodline(Builder $query, Model $parent, $foreignKey, $localKey)
185
    {
186 22
        return new Bloodline($query, $parent, $foreignKey, $localKey);
187
    }
188
189
    /**
190
     * Get the model's children.
191
     *
192
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
193
     */
194 4
    public function children()
195
    {
196 4
        return $this->hasMany(static::class, $this->getParentKeyName(), $this->getLocalKeyName());
0 ignored issues
show
Bug introduced by
The method hasMany() does not exist on Staudenmeir\LaravelAdjac...sRecursiveRelationships. Did you maybe mean hasManyOfDescendantsAndSelf()? ( Ignorable by Annotation )

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

196
        return $this->/** @scrutinizer ignore-call */ hasMany(static::class, $this->getParentKeyName(), $this->getLocalKeyName());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
197
    }
198
199
    /**
200
     * Get the model's children and itself.
201
     *
202
     * @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Descendants
203
     */
204 4
    public function childrenAndSelf()
205
    {
206 4
        return $this->descendantsAndSelf()->whereDepth('<=', 1);
0 ignored issues
show
Bug introduced by
The method whereDepth() does not exist on Staudenmeir\LaravelAdjac...t\Relations\Descendants. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

206
        return $this->descendantsAndSelf()->/** @scrutinizer ignore-call */ whereDepth('<=', 1);
Loading history...
207
    }
208
209
    /**
210
     * Get the model's descendants.
211
     *
212
     * @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Descendants
213
     */
214 54
    public function descendants()
215
    {
216 54
        return $this->newDescendants(
217 54
            (new static())->newQuery(),
218
            $this,
0 ignored issues
show
Bug introduced by
$this of type Staudenmeir\LaravelAdjac...sRecursiveRelationships is incompatible with the type Illuminate\Database\Eloquent\Model expected by parameter $parent of Staudenmeir\LaravelAdjac...ships::newDescendants(). ( Ignorable by Annotation )

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

218
            /** @scrutinizer ignore-type */ $this,
Loading history...
219 54
            $this->getQualifiedParentKeyName(),
220 54
            $this->getLocalKeyName(),
221 54
            false
222
        );
223
    }
224
225
    /**
226
     * Get the model's descendants and itself.
227
     *
228
     * @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Descendants
229
     */
230 22
    public function descendantsAndSelf()
231
    {
232 22
        return $this->newDescendants(
233 22
            (new static())->newQuery(),
234
            $this,
0 ignored issues
show
Bug introduced by
$this of type Staudenmeir\LaravelAdjac...sRecursiveRelationships is incompatible with the type Illuminate\Database\Eloquent\Model expected by parameter $parent of Staudenmeir\LaravelAdjac...ships::newDescendants(). ( Ignorable by Annotation )

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

234
            /** @scrutinizer ignore-type */ $this,
Loading history...
235 22
            $this->getQualifiedParentKeyName(),
236 22
            $this->getLocalKeyName(),
237 22
            true
238
        );
239
    }
240
241
    /**
242
     * Instantiate a new Descendants relationship.
243
     *
244
     * @param \Illuminate\Database\Eloquent\Builder $query
245
     * @param \Illuminate\Database\Eloquent\Model $parent
246
     * @param string $foreignKey
247
     * @param string $localKey
248
     * @param bool $andSelf
249
     * @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Descendants
250
     */
251 76
    protected function newDescendants(Builder $query, Model $parent, $foreignKey, $localKey, $andSelf)
252
    {
253 76
        return new Descendants($query, $parent, $foreignKey, $localKey, $andSelf);
254
    }
255
256
    /**
257
     * Get the model's parent.
258
     *
259
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
260
     */
261 4
    public function parent()
262
    {
263 4
        return $this->belongsTo(static::class, $this->getParentKeyName(), $this->getLocalKeyName());
0 ignored issues
show
Bug introduced by
The method belongsTo() does not exist on Staudenmeir\LaravelAdjac...sRecursiveRelationships. Did you maybe mean belongsToManyOfDescendantsAndSelf()? ( Ignorable by Annotation )

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

263
        return $this->/** @scrutinizer ignore-call */ belongsTo(static::class, $this->getParentKeyName(), $this->getLocalKeyName());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
264
    }
265
266
    /**
267
     * Get the model's parent and itself.
268
     *
269
     * @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Ancestors
270
     */
271 4
    public function parentAndSelf()
272
    {
273 4
        return $this->ancestorsAndSelf()->whereDepth('>=', -1);
0 ignored issues
show
Bug introduced by
The method whereDepth() does not exist on Staudenmeir\LaravelAdjac...ent\Relations\Ancestors. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

273
        return $this->ancestorsAndSelf()->/** @scrutinizer ignore-call */ whereDepth('>=', -1);
Loading history...
274
    }
275
276
    /**
277
     * Get the model's root ancestor.
278
     *
279
     * @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\RootAncestor
280
     */
281 22
    public function rootAncestor()
282
    {
283 22
        return $this->newRootAncestor(
284 22
            (new static())->newQuery(),
285
            $this,
0 ignored issues
show
Bug introduced by
$this of type Staudenmeir\LaravelAdjac...sRecursiveRelationships is incompatible with the type Illuminate\Database\Eloquent\Model expected by parameter $parent of Staudenmeir\LaravelAdjac...hips::newRootAncestor(). ( Ignorable by Annotation )

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

285
            /** @scrutinizer ignore-type */ $this,
Loading history...
286 22
            $this->getQualifiedParentKeyName(),
287 22
            $this->getLocalKeyName()
288
        );
289
    }
290
291
    /**
292
     * Instantiate a new RootAncestor relationship.
293
     *
294
     * @param \Illuminate\Database\Eloquent\Builder $query
295
     * @param \Illuminate\Database\Eloquent\Model $parent
296
     * @param string $foreignKey
297
     * @param string $localKey
298
     * @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\RootAncestor
299
     */
300 22
    protected function newRootAncestor(Builder $query, Model $parent, $foreignKey, $localKey)
301
    {
302 22
        return new RootAncestor($query, $parent, $foreignKey, $localKey);
303
    }
304
305
    /**
306
     * Get the model's siblings.
307
     *
308
     * @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Siblings
309
     */
310 28
    public function siblings()
311
    {
312 28
        return $this->newSiblings(
313 28
            (new static())->newQuery(),
314
            $this,
0 ignored issues
show
Bug introduced by
$this of type Staudenmeir\LaravelAdjac...sRecursiveRelationships is incompatible with the type Illuminate\Database\Eloquent\Model expected by parameter $parent of Staudenmeir\LaravelAdjac...ionships::newSiblings(). ( Ignorable by Annotation )

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

314
            /** @scrutinizer ignore-type */ $this,
Loading history...
315 28
            $this->getQualifiedParentKeyName(),
316 28
            $this->getParentKeyName(),
317 28
            false
318
        );
319
    }
320
321
    /**
322
     * Get the model's siblings and itself.
323
     *
324
     * @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Siblings
325
     */
326 24
    public function siblingsAndSelf()
327
    {
328 24
        return $this->newSiblings(
329 24
            (new static())->newQuery(),
330
            $this,
0 ignored issues
show
Bug introduced by
$this of type Staudenmeir\LaravelAdjac...sRecursiveRelationships is incompatible with the type Illuminate\Database\Eloquent\Model expected by parameter $parent of Staudenmeir\LaravelAdjac...ionships::newSiblings(). ( Ignorable by Annotation )

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

330
            /** @scrutinizer ignore-type */ $this,
Loading history...
331 24
            $this->getQualifiedParentKeyName(),
332 24
            $this->getParentKeyName(),
333 24
            true
334
        );
335
    }
336
337
    /**
338
     * Instantiate a new Siblings relationship.
339
     *
340
     * @param \Illuminate\Database\Eloquent\Builder $query
341
     * @param \Illuminate\Database\Eloquent\Model $parent
342
     * @param string $foreignKey
343
     * @param string $localKey
344
     * @param bool $andSelf
345
     * @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Siblings
346
     */
347 52
    protected function newSiblings(Builder $query, Model $parent, $foreignKey, $localKey, $andSelf)
348
    {
349 52
        return new Siblings($query, $parent, $foreignKey, $localKey, $andSelf);
350
    }
351
352
    /**
353
     * Define a one-to-many relationship of the model's descendants.
354
     *
355
     * @param string $related
356
     * @param string|null $foreignKey
357
     * @param string|null $localKey
358
     * @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\HasManyOfDescendants
359
     */
360 54
    public function hasManyOfDescendants($related, $foreignKey = null, $localKey = null)
361
    {
362 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

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

364
        $foreignKey = $foreignKey ?: $this->/** @scrutinizer ignore-call */ getForeignKey();
Loading history...
365
366 54
        $localKey = $localKey ?: $this->getKeyName();
367
368 54
        return $this->newHasManyOfDescendants(
369 54
            $instance->newQuery(),
370
            $this,
0 ignored issues
show
Bug introduced by
$this of type Staudenmeir\LaravelAdjac...sRecursiveRelationships 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

370
            /** @scrutinizer ignore-type */ $this,
Loading history...
371 54
            $instance->qualifyColumn($foreignKey),
372
            $localKey,
373 54
            false
374
        );
375
    }
376
377
    /**
378
     * Define a one-to-many relationship of the model's descendants and itself.
379
     *
380
     * @param string $related
381
     * @param string|null $foreignKey
382
     * @param string|null $localKey
383
     * @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\HasManyOfDescendants
384
     */
385 22
    public function hasManyOfDescendantsAndSelf($related, $foreignKey = null, $localKey = null)
386
    {
387 22
        $instance = $this->newRelatedInstance($related);
388
389 22
        $foreignKey = $foreignKey ?: $this->getForeignKey();
390
391 22
        $localKey = $localKey ?: $this->getKeyName();
392
393 22
        return $this->newHasManyOfDescendants(
394 22
            $instance->newQuery(),
395
            $this,
0 ignored issues
show
Bug introduced by
$this of type Staudenmeir\LaravelAdjac...sRecursiveRelationships 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

395
            /** @scrutinizer ignore-type */ $this,
Loading history...
396 22
            $instance->qualifyColumn($foreignKey),
397
            $localKey,
398 22
            true
399
        );
400
    }
401
402
    /**
403
     * Instantiate a new HasManyOfDescendants relationship.
404
     *
405
     * @param \Illuminate\Database\Eloquent\Builder $query
406
     * @param \Illuminate\Database\Eloquent\Model $parent
407
     * @param string $foreignKey
408
     * @param string $localKey
409
     * @param bool $andSelf
410
     * @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\HasManyOfDescendants
411
     */
412 76
    protected function newHasManyOfDescendants(Builder $query, Model $parent, $foreignKey, $localKey, $andSelf)
413
    {
414 76
        return new HasManyOfDescendants($query, $parent, $foreignKey, $localKey, $andSelf);
415
    }
416
417
    /**
418
     * Define a many-to-many relationship of the model's descendants.
419
     *
420
     * @param string $related
421
     * @param string|null $table
422
     * @param string|null $foreignPivotKey
423
     * @param string|null $relatedPivotKey
424
     * @param string|null $parentKey
425
     * @param string|null $relatedKey
426
     * @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\BelongsToManyOfDescendants
427
     */
428 50
    public function belongsToManyOfDescendants(
429
        $related,
430
        $table = null,
431
        $foreignPivotKey = null,
432
        $relatedPivotKey = null,
433
        $parentKey = null,
434
        $relatedKey = null
435
    ) {
436 50
        $instance = $this->newRelatedInstance($related);
437
438 50
        $foreignPivotKey = $foreignPivotKey ?: $this->getForeignKey();
439
440 50
        $relatedPivotKey = $relatedPivotKey ?: $instance->getForeignKey();
441
442 50
        if (is_null($table)) {
443 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

443
            /** @scrutinizer ignore-call */ 
444
            $table = $this->joiningTable($related, $instance);
Loading history...
444
        }
445
446 50
        return $this->newBelongsToManyOfDescendants(
447 50
            $instance->newQuery(),
448
            $this,
0 ignored issues
show
Bug introduced by
$this of type Staudenmeir\LaravelAdjac...sRecursiveRelationships 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

448
            /** @scrutinizer ignore-type */ $this,
Loading history...
449
            $table,
450
            $foreignPivotKey,
451
            $relatedPivotKey,
452 50
            $parentKey ?: $this->getKeyName(),
453 50
            $relatedKey ?: $instance->getKeyName(),
454 50
            false
455
        );
456
    }
457
458
    /**
459
     * Define a many-to-many relationship of the model's descendants and itself.
460
     *
461
     * @param string $related
462
     * @param string|null $table
463
     * @param string|null $foreignPivotKey
464
     * @param string|null $relatedPivotKey
465
     * @param string|null $parentKey
466
     * @param string|null $relatedKey
467
     * @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\BelongsToManyOfDescendants
468
     */
469 22
    public function belongsToManyOfDescendantsAndSelf(
470
        $related,
471
        $table = null,
472
        $foreignPivotKey = null,
473
        $relatedPivotKey = null,
474
        $parentKey = null,
475
        $relatedKey = null
476
    ) {
477 22
        $instance = $this->newRelatedInstance($related);
478
479 22
        $foreignPivotKey = $foreignPivotKey ?: $this->getForeignKey();
480
481 22
        $relatedPivotKey = $relatedPivotKey ?: $instance->getForeignKey();
482
483 22
        if (is_null($table)) {
484 22
            $table = $this->joiningTable($related, $instance);
485
        }
486
487 22
        return $this->newBelongsToManyOfDescendants(
488 22
            $instance->newQuery(),
489
            $this,
0 ignored issues
show
Bug introduced by
$this of type Staudenmeir\LaravelAdjac...sRecursiveRelationships 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

489
            /** @scrutinizer ignore-type */ $this,
Loading history...
490
            $table,
491
            $foreignPivotKey,
492
            $relatedPivotKey,
493 22
            $parentKey ?: $this->getKeyName(),
494 22
            $relatedKey ?: $instance->getKeyName(),
495 22
            true
496
        );
497
    }
498
499
    /**
500
     * Instantiate a new BelongsToManyOfDescendants relationship.
501
     *
502
     * @param \Illuminate\Database\Eloquent\Builder $query
503
     * @param \Illuminate\Database\Eloquent\Model $parent
504
     * @param string $table
505
     * @param string $foreignPivotKey
506
     * @param string $relatedPivotKey
507
     * @param string $parentKey
508
     * @param string $relatedKey
509
     * @param bool $andSelf
510
     * @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\BelongsToManyOfDescendants
511
     */
512 72
    protected function newBelongsToManyOfDescendants(
513
        Builder $query,
514
        Model $parent,
515
        $table,
516
        $foreignPivotKey,
517
        $relatedPivotKey,
518
        $parentKey,
519
        $relatedKey,
520
        $andSelf
521
    ) {
522 72
        return new BelongsToManyOfDescendants(
523 72
            $query,
524
            $parent,
525
            $table,
526
            $foreignPivotKey,
527
            $relatedPivotKey,
528
            $parentKey,
529
            $relatedKey,
530
            $andSelf
531
        );
532
    }
533
534
    /**
535
     * Define a polymorphic many-to-many relationship of the model's descendants.
536
     *
537
     * @param string $related
538
     * @param string $name
539
     * @param string|null $table
540
     * @param string|null $foreignPivotKey
541
     * @param string|null $relatedPivotKey
542
     * @param string|null $parentKey
543
     * @param string|null $relatedKey
544
     * @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\MorphToManyOfDescendants
545
     */
546 50
    public function morphToManyOfDescendants(
547
        $related,
548
        $name,
549
        $table = null,
550
        $foreignPivotKey = null,
551
        $relatedPivotKey = null,
552
        $parentKey = null,
553
        $relatedKey = null
554
    ) {
555 50
        $instance = $this->newRelatedInstance($related);
556
557 50
        $foreignPivotKey = $foreignPivotKey ?: $name.'_id';
558
559 50
        $relatedPivotKey = $relatedPivotKey ?: $instance->getForeignKey();
560
561 50
        if (! $table) {
562 50
            $words = preg_split('/(_)/u', $name, -1, PREG_SPLIT_DELIM_CAPTURE);
563
564 50
            $lastWord = array_pop($words);
565
566 50
            $table = implode('', $words).Str::plural($lastWord);
567
        }
568
569 50
        return $this->newMorphToManyOfDescendants(
570 50
            $instance->newQuery(),
571
            $this,
0 ignored issues
show
Bug introduced by
$this of type Staudenmeir\LaravelAdjac...sRecursiveRelationships 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

571
            /** @scrutinizer ignore-type */ $this,
Loading history...
572
            $name,
573
            $table,
574
            $foreignPivotKey,
575
            $relatedPivotKey,
576 50
            $parentKey ?: $this->getKeyName(),
577 50
            $relatedKey ?: $instance->getKeyName(),
578 50
            false
579
        );
580
    }
581
582
    /**
583
     * Define a polymorphic many-to-many relationship of the model's descendants and itself.
584
     *
585
     * @param string $related
586
     * @param string $name
587
     * @param string|null $table
588
     * @param string|null $foreignPivotKey
589
     * @param string|null $relatedPivotKey
590
     * @param string|null $parentKey
591
     * @param string|null $relatedKey
592
     * @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\MorphToManyOfDescendants
593
     */
594 22
    public function morphToManyOfDescendantsAndSelf(
595
        $related,
596
        $name,
597
        $table = null,
598
        $foreignPivotKey = null,
599
        $relatedPivotKey = null,
600
        $parentKey = null,
601
        $relatedKey = null
602
    ) {
603 22
        $instance = $this->newRelatedInstance($related);
604
605 22
        $foreignPivotKey = $foreignPivotKey ?: $name.'_id';
606
607 22
        $relatedPivotKey = $relatedPivotKey ?: $instance->getForeignKey();
608
609 22
        if (! $table) {
610 22
            $words = preg_split('/(_)/u', $name, -1, PREG_SPLIT_DELIM_CAPTURE);
611
612 22
            $lastWord = array_pop($words);
613
614 22
            $table = implode('', $words).Str::plural($lastWord);
615
        }
616
617 22
        return $this->newMorphToManyOfDescendants(
618 22
            $instance->newQuery(),
619
            $this,
0 ignored issues
show
Bug introduced by
$this of type Staudenmeir\LaravelAdjac...sRecursiveRelationships 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

619
            /** @scrutinizer ignore-type */ $this,
Loading history...
620
            $name,
621
            $table,
622
            $foreignPivotKey,
623
            $relatedPivotKey,
624 22
            $parentKey ?: $this->getKeyName(),
625 22
            $relatedKey ?: $instance->getKeyName(),
626 22
            true
627
        );
628
    }
629
630
    /**
631
     * Instantiate a new MorphToManyOfDescendants relationship.
632
     *
633
     * @param \Illuminate\Database\Eloquent\Builder $query
634
     * @param \Illuminate\Database\Eloquent\Model $parent
635
     * @param string $name
636
     * @param string $table
637
     * @param string $foreignPivotKey
638
     * @param string $relatedPivotKey
639
     * @param string $parentKey
640
     * @param string $relatedKey
641
     * @param bool $andSelf
642
     * @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\MorphToManyOfDescendants
643
     */
644 72
    protected function newMorphToManyOfDescendants(
645
        Builder $query,
646
        Model $parent,
647
        $name,
648
        $table,
649
        $foreignPivotKey,
650
        $relatedPivotKey,
651
        $parentKey,
652
        $relatedKey,
653
        $andSelf
654
    ) {
655 72
        return new MorphToManyOfDescendants(
656 72
            $query,
657
            $parent,
658
            $name,
659
            $table,
660
            $foreignPivotKey,
661
            $relatedPivotKey,
662
            $parentKey,
663
            $relatedKey,
664
            $andSelf
665
        );
666
    }
667
668
    /**
669
     * Get the first segment of the model's path.
670
     *
671
     * @return string
672
     */
673 48
    public function getFirstPathSegment()
674
    {
675 48
        $path = $this->attributes[$this->getPathName()];
676
677 48
        return explode($this->getPathSeparator(), $path)[0];
678
    }
679
680
    /**
681
     * Determine whether the model's path is nested.
682
     *
683
     * @return bool
684
     */
685 8
    public function hasNestedPath()
686
    {
687 8
        $path = $this->attributes[$this->getPathName()];
688
689 8
        return Str::contains($path, $this->getPathSeparator());
690
    }
691
692
    /**
693
     * Create a new Eloquent query builder for the model.
694
     *
695
     * @param \Illuminate\Database\Query\Builder $query
696
     * @return \Illuminate\Database\Eloquent\Builder|static
697
     */
698 484
    public function newEloquentBuilder($query)
699
    {
700 484
        return new \Staudenmeir\LaravelAdjacencyList\Eloquent\Builder($query);
701
    }
702
703
    /**
704
     * Create a new Eloquent Collection instance.
705
     *
706
     * @param array $models
707
     * @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Collection
708
     */
709 480
    public function newCollection(array $models = [])
710
    {
711 480
        return new Collection($models);
712
    }
713
}
714