Passed
Push — master ( 3fade5...c39224 )
by Jonas
11:04
created

HasAdjacencyList::setRecursiveQueryConstraint()   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 1
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 HasRecursiveRelationshipScopes;
19
20
    /**
21
     * The additional constraint for the recursive query.
22
     *
23
     * @var callable|null
24
     */
25
    public static $recursiveQueryConstraint;
26
27
    /**
28
     * Get the name of the parent key column.
29
     *
30
     * @return string
31
     */
32 700
    public function getParentKeyName()
33
    {
34 700
        return 'parent_id';
35
    }
36
37
    /**
38
     * Get the qualified parent key column.
39
     *
40
     * @return string
41
     */
42 291
    public function getQualifiedParentKeyName()
43
    {
44 291
        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

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

54
        return $this->/** @scrutinizer ignore-call */ getKeyName();
Loading history...
55
    }
56
57
    /**
58
     * Get the qualified local key column.
59
     *
60
     * @return string
61
     */
62 338
    public function getQualifiedLocalKeyName()
63
    {
64 338
        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

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

125
            (new static())->/** @scrutinizer ignore-call */ newQuery(),
Loading history...
126
            $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

126
            /** @scrutinizer ignore-type */ $this,
Loading history...
127 63
            $this->getQualifiedParentKeyName(),
128 63
            $this->getLocalKeyName(),
129
            false
130
        );
131
    }
132
133
    /**
134
     * Get the model's ancestors and itself.
135
     *
136
     * @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Ancestors
137
     */
138 34
    public function ancestorsAndSelf()
139
    {
140 34
        return $this->newAncestors(
141 34
            (new static())->newQuery(),
142
            $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

142
            /** @scrutinizer ignore-type */ $this,
Loading history...
143 34
            $this->getQualifiedParentKeyName(),
144 34
            $this->getLocalKeyName(),
145
            true
146
        );
147
    }
148
149
    /**
150
     * Instantiate a new Ancestors relationship.
151
     *
152
     * @param \Illuminate\Database\Eloquent\Builder $query
153
     * @param \Illuminate\Database\Eloquent\Model $parent
154
     * @param string $foreignKey
155
     * @param string $localKey
156
     * @param bool $andSelf
157
     * @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Ancestors
158
     */
159 97
    protected function newAncestors(Builder $query, Model $parent, $foreignKey, $localKey, $andSelf)
160
    {
161 97
        return new Ancestors($query, $parent, $foreignKey, $localKey, $andSelf);
162
    }
163
164
    /**
165
     * Get the model's bloodline.
166
     *
167
     * @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Bloodline
168
     */
169 29
    public function bloodline()
170
    {
171 29
        return $this->newBloodline(
172 29
            (new static())->newQuery(),
173
            $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

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

200
        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...
201
    }
202
203
    /**
204
     * Get the model's children and itself.
205
     *
206
     * @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Descendants
207
     */
208 5
    public function childrenAndSelf()
209
    {
210 5
        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

210
        return $this->descendantsAndSelf()->/** @scrutinizer ignore-call */ whereDepth('<=', 1);
Loading history...
211
    }
212
213
    /**
214
     * Get the model's descendants.
215
     *
216
     * @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Descendants
217
     */
218 69
    public function descendants()
219
    {
220 69
        return $this->newDescendants(
221 69
            (new static())->newQuery(),
222
            $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

222
            /** @scrutinizer ignore-type */ $this,
Loading history...
223 69
            $this->getQualifiedParentKeyName(),
224 69
            $this->getLocalKeyName(),
225
            false
226
        );
227
    }
228
229
    /**
230
     * Get the model's descendants and itself.
231
     *
232
     * @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Descendants
233
     */
234 29
    public function descendantsAndSelf()
235
    {
236 29
        return $this->newDescendants(
237 29
            (new static())->newQuery(),
238
            $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

238
            /** @scrutinizer ignore-type */ $this,
Loading history...
239 29
            $this->getQualifiedParentKeyName(),
240 29
            $this->getLocalKeyName(),
241
            true
242
        );
243
    }
244
245
    /**
246
     * Instantiate a new Descendants relationship.
247
     *
248
     * @param \Illuminate\Database\Eloquent\Builder $query
249
     * @param \Illuminate\Database\Eloquent\Model $parent
250
     * @param string $foreignKey
251
     * @param string $localKey
252
     * @param bool $andSelf
253
     * @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Descendants
254
     */
255 98
    protected function newDescendants(Builder $query, Model $parent, $foreignKey, $localKey, $andSelf)
256
    {
257 98
        return new Descendants($query, $parent, $foreignKey, $localKey, $andSelf);
258
    }
259
260
    /**
261
     * Get the model's parent.
262
     *
263
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
264
     */
265 5
    public function parent()
266
    {
267 5
        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

267
        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...
268
    }
269
270
    /**
271
     * Get the model's parent and itself.
272
     *
273
     * @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Ancestors
274
     */
275 5
    public function parentAndSelf()
276
    {
277 5
        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

277
        return $this->ancestorsAndSelf()->/** @scrutinizer ignore-call */ whereDepth('>=', -1);
Loading history...
278
    }
279
280
    /**
281
     * Get the model's root ancestor.
282
     *
283
     * @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\RootAncestor
284
     */
285 25
    public function rootAncestor()
286
    {
287 25
        return $this->newRootAncestor(
288 25
            (new static())->newQuery(),
289
            $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

289
            /** @scrutinizer ignore-type */ $this,
Loading history...
290 25
            $this->getQualifiedParentKeyName(),
291 25
            $this->getLocalKeyName()
292
        );
293
    }
294
295
    /**
296
     * Instantiate a new RootAncestor relationship.
297
     *
298
     * @param \Illuminate\Database\Eloquent\Builder $query
299
     * @param \Illuminate\Database\Eloquent\Model $parent
300
     * @param string $foreignKey
301
     * @param string $localKey
302
     * @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\RootAncestor
303
     */
304 25
    protected function newRootAncestor(Builder $query, Model $parent, $foreignKey, $localKey)
305
    {
306 25
        return new RootAncestor($query, $parent, $foreignKey, $localKey);
307
    }
308
309
    /**
310
     * Get the model's siblings.
311
     *
312
     * @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Siblings
313
     */
314 35
    public function siblings()
315
    {
316 35
        return $this->newSiblings(
317 35
            (new static())->newQuery(),
318
            $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

318
            /** @scrutinizer ignore-type */ $this,
Loading history...
319 35
            $this->getQualifiedParentKeyName(),
320 35
            $this->getParentKeyName(),
321
            false
322
        );
323
    }
324
325
    /**
326
     * Get the model's siblings and itself.
327
     *
328
     * @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Siblings
329
     */
330 30
    public function siblingsAndSelf()
331
    {
332 30
        return $this->newSiblings(
333 30
            (new static())->newQuery(),
334
            $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

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

388
        /** @scrutinizer ignore-call */ 
389
        $casts = $this->getCasts();
Loading history...
389
390 94
        return isset($casts[$attribute]) && in_array($casts[$attribute], ['int', 'integer']);
391
    }
392
393
    /**
394
     * Create a new Eloquent query builder for the model.
395
     *
396
     * @param \Illuminate\Database\Query\Builder $query
397
     * @return \Illuminate\Database\Eloquent\Builder|static
398
     */
399 700
    public function newEloquentBuilder($query)
400
    {
401 700
        return new \Staudenmeir\LaravelAdjacencyList\Eloquent\Builder($query);
402
    }
403
404
    /**
405
     * Create a new Eloquent Collection instance.
406
     *
407
     * @param array $models
408
     * @return \Staudenmeir\LaravelAdjacencyList\Eloquent\Collection
409
     */
410 695
    public function newCollection(array $models = [])
411
    {
412 695
        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

412
        return new Collection(/** @scrutinizer ignore-type */ $models);
Loading history...
413
    }
414
415
    /**
416
     * Set an additional constraint for the recursive query.
417
     *
418
     * @param callable $constraint
419
     * @return void
420
     */
421 5
    public static function setRecursiveQueryConstraint(callable $constraint)
422
    {
423 5
        static::$recursiveQueryConstraint = $constraint;
424
    }
425
426
    /**
427
     * Unset the additional constraint for the recursive query.
428
     *
429
     * @return void
430
     */
431 5
    public static function unsetRecursiveQueryConstraint()
432
    {
433 5
        static::$recursiveQueryConstraint = null;
434
    }
435
436
    /**
437
     * Execute a query with an additional constraint for the recursive query.
438
     *
439
     * @param callable $constraint
440
     * @param callable $query
441
     * @return mixed
442
     */
443 5
    public static function withRecursiveQueryConstraint(callable $constraint, callable $query)
444
    {
445 5
        $previous = static::$recursiveQueryConstraint;
446
447 5
        static::$recursiveQueryConstraint = $constraint;
448
449 5
        $result = $query();
450
451 5
        static::$recursiveQueryConstraint = $previous;
452
453 5
        return $result;
454
    }
455
}
456