Passed
Push — master ( 3ce322...a26324 )
by Jonas
13:44 queued 23s
created

HasAdjacencyList::withRecursiveQueryConstraint()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Staudenmeir\LaravelAdjacencyList\Eloquent\Traits;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Support\Str;
8
use Staudenmeir\LaravelAdjacencyList\Eloquent\Collection;
9
use Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Ancestors;
10
use Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Bloodline;
11
use Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Descendants;
12
use Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\RootAncestor;
13
use Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Siblings;
14
15
trait HasAdjacencyList
16
{
17
    use HasOfDescendantsRelationships;
18
    use HasQueryConstraints;
19
    use HasRecursiveRelationshipScopes;
20
21
    /**
22
     * Get the name of the parent key column.
23
     *
24
     * @return string
25
     */
26
    public function getParentKeyName()
27
    {
28
        return 'parent_id';
29
    }
30
31
    /**
32 957
     * Get the qualified parent key column.
33
     *
34 957
     * @return string
35
     */
36
    public function getQualifiedParentKeyName()
37
    {
38
        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

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

48
        return $this->/** @scrutinizer ignore-call */ getKeyName();
Loading history...
49
    }
50
51
    /**
52 915
     * Get the qualified local key column.
53
     *
54 915
     * @return string
55
     */
56
    public function getQualifiedLocalKeyName()
57
    {
58
        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

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

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

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

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

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

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

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

167
            /** @scrutinizer ignore-type */ $this,
Loading history...
168
            $this->getQualifiedParentKeyName(),
169 68
            $this->getLocalKeyName()
170
        );
171 68
    }
172 68
173 68
    /**
174 68
     * Instantiate a new Bloodline relationship.
175 68
     *
176 68
     * @param \Illuminate\Database\Eloquent\Builder $query
177
     * @param \Illuminate\Database\Eloquent\Model $parent
178
     * @param string $foreignKey
179
     * @param string $localKey
180
     * @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Bloodline<static>
181
     */
182
    protected function newBloodline(Builder $query, Model $parent, $foreignKey, $localKey)
183
    {
184
        return new Bloodline($query, $parent, $foreignKey, $localKey);
185
    }
186
187
    /**
188 68
     * Get the model's children.
189
     *
190 68
     * @return \Illuminate\Database\Eloquent\Relations\HasMany<static>
191
     */
192
    public function children()
193
    {
194
        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...Traits\HasAdjacencyList. 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

194
        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...
195
    }
196
197
    /**
198 6
     * Get the model's children and itself.
199
     *
200 6
     * @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Descendants<static>
201
     */
202
    public function childrenAndSelf()
203
    {
204
        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

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

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

216
            /** @scrutinizer ignore-type */ $this,
Loading history...
217
            $this->getQualifiedParentKeyName(),
218 130
            $this->getLocalKeyName(),
219
            false
220 130
        );
221 130
    }
222 130
223 130
    /**
224 130
     * Get the model's descendants and itself.
225 130
     *
226 130
     * @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Descendants<static>
227
     */
228
    public function descendantsAndSelf()
229
    {
230
        return $this->newDescendants(
231
            (new static())->newQuery(),
232
            $this,
0 ignored issues
show
Bug introduced by
$this of type Staudenmeir\LaravelAdjac...Traits\HasAdjacencyList is incompatible with the type Illuminate\Database\Eloquent\Model expected by parameter $parent of Staudenmeir\LaravelAdjac...yList::newDescendants(). ( 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
            $this->getQualifiedParentKeyName(),
234 57
            $this->getLocalKeyName(),
235
            true
236 57
        );
237 57
    }
238 57
239 57
    /**
240 57
     * Instantiate a new Descendants relationship.
241 57
     *
242 57
     * @param \Illuminate\Database\Eloquent\Builder $query
243
     * @param \Illuminate\Database\Eloquent\Model $parent
244
     * @param string $foreignKey
245
     * @param string $localKey
246
     * @param bool $andSelf
247
     * @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Descendants<static>
248
     */
249
    protected function newDescendants(Builder $query, Model $parent, $foreignKey, $localKey, $andSelf)
250
    {
251
        return new Descendants($query, $parent, $foreignKey, $localKey, $andSelf);
252
    }
253
254
    /**
255 187
     * Get the model's parent.
256
     *
257 187
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo<static, static>
258
     */
259
    public function parent()
260
    {
261
        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...Traits\HasAdjacencyList. 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

261
        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...
262
    }
263
264
    /**
265 6
     * Get the model's parent and itself.
266
     *
267 6
     * @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Ancestors<static>
268
     */
269
    public function parentAndSelf()
270
    {
271
        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

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

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

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

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

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

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

328
            /** @scrutinizer ignore-type */ $this,
Loading history...
329
            $this->getQualifiedParentKeyName(),
330 35
            $this->getParentKeyName(),
331
            true
332 35
        );
333 35
    }
334 35
335 35
    /**
336 35
     * Instantiate a new Siblings relationship.
337 35
     *
338 35
     * @param \Illuminate\Database\Eloquent\Builder $query
339
     * @param \Illuminate\Database\Eloquent\Model $parent
340
     * @param string $foreignKey
341
     * @param string $localKey
342
     * @param bool $andSelf
343
     * @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Siblings<static>
344
     */
345
    protected function newSiblings(Builder $query, Model $parent, $foreignKey, $localKey, $andSelf)
346
    {
347
        return new Siblings($query, $parent, $foreignKey, $localKey, $andSelf);
348
    }
349
350
    /**
351 76
     * Get the first segment of the model's path.
352
     *
353 76
     * @return string
354
     */
355
    public function getFirstPathSegment()
356
    {
357
        $path = $this->attributes[$this->getPathName()];
358
359
        return explode($this->getPathSeparator(), $path)[0];
360
    }
361 72
362
    /**
363 72
     * Determine whether the model's path is nested.
364
     *
365 72
     * @return bool
366
     */
367
    public function hasNestedPath()
368
    {
369
        $path = $this->attributes[$this->getPathName()];
370
371
        return Str::contains($path, $this->getPathSeparator());
372
    }
373 12
374
    /**
375 12
     * Determine if an attribute is an integer.
376
     *
377 12
     * @param string $attribute
378
     * @return bool
379
     */
380
    public function isIntegerAttribute($attribute)
381
    {
382
        $segments = explode('.', $attribute);
383
        $attribute = end($segments);
384
385
        $casts = $this->getCasts();
0 ignored issues
show
Bug introduced by
It seems like getCasts() 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

385
        /** @scrutinizer ignore-call */ 
386
        $casts = $this->getCasts();
Loading history...
386 135
387
        return isset($casts[$attribute]) && in_array($casts[$attribute], ['int', 'integer']);
388 135
    }
389 135
390
    /**
391 135
     * Create a new Eloquent query builder for the model.
392
     *
393 135
     * @param \Illuminate\Database\Query\Builder $query
394
     * @return \Illuminate\Database\Eloquent\Builder|static
395
     */
396
    public function newEloquentBuilder($query)
397
    {
398
        return new \Staudenmeir\LaravelAdjacencyList\Eloquent\Builder($query);
399
    }
400
401
    /**
402 957
     * Create a new Eloquent Collection instance.
403
     *
404 957
     * @param array $models
405
     * @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Collection
406
     */
407
    public function newCollection(array $models = [])
408
    {
409
        return new Collection($models);
0 ignored issues
show
Bug introduced by
$models of type array is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $items of Staudenmeir\LaravelAdjac...llection::__construct(). ( Ignorable by Annotation )

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

409
        return new Collection(/** @scrutinizer ignore-type */ $models);
Loading history...
410
    }
411
412
    /**
413 916
     * Execute a query with a maximum depth constraint for the recursive query.
414
     *
415 916
     * @param int $maxDepth
416
     * @param callable $query
417
     * @return mixed
418
     */
419
    public static function withMaxDepth(int $maxDepth, callable $query): mixed
420
    {
421
        $operator = $maxDepth > 0 ? '<' : '>';
422
423
        return static::withRecursiveQueryConstraint(
424 6
            fn (Builder $query) => $query->whereDepth($operator, $maxDepth),
425
            $query
426 6
        );
427
    }
428
}
429