LTreeModelTrait   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 16
eloc 27
c 3
b 0
f 0
dl 0
loc 82
ccs 41
cts 41
cp 1
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A getAncestorByLevel() 0 3 1
A scopeWithoutSelf() 0 3 1
A scopeAncestorsOf() 0 6 2
A scopeRoot() 0 3 1
A isParentOf() 0 3 1
A ltreeChildren() 0 3 1
A scopeParentsOf() 0 6 1
A ltreeParent() 0 3 1
A getLtreeProxyUpdateColumns() 0 3 1
A scopeDescendantsOf() 0 6 2
A newCollection() 0 3 1
A getLtreeProxyDeleteColumns() 0 3 1
A scopeAncestorByLevel() 0 6 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Umbrellio\LTree\Traits;
6
7
use Illuminate\Database\Eloquent\Builder;
8
use Illuminate\Database\Eloquent\Model;
9
use Illuminate\Database\Eloquent\Relations\BelongsTo;
10
use Illuminate\Database\Eloquent\Relations\HasMany;
11
use Illuminate\Database\Eloquent\SoftDeletes;
12
use Umbrellio\LTree\Collections\LTreeCollection;
13
use Umbrellio\LTree\Interfaces\LTreeModelInterface;
14
15
/**
16
 * @mixin Model
17
 * @mixin LTreeModelInterface
18
 * @mixin SoftDeletes
19
 * @const string DELETED_AT
20
 */
21
trait LTreeModelTrait
22
{
23
    use LTreeTrait;
24
25 40
    public function newCollection(array $models = []): LTreeCollection
26
    {
27 40
        return new LTreeCollection($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 Umbrellio\LTree\Collecti...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

27
        return new LTreeCollection(/** @scrutinizer ignore-type */ $models);
Loading history...
28
    }
29
30 2
    public function ltreeParent(): BelongsTo
31
    {
32 2
        return $this->belongsTo(static::class, $this->getLtreeParentColumn());
0 ignored issues
show
Bug introduced by
It seems like belongsTo() 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

32
        return $this->/** @scrutinizer ignore-call */ belongsTo(static::class, $this->getLtreeParentColumn());
Loading history...
33
    }
34
35 2
    public function ltreeChildren(): HasMany
36
    {
37 2
        return $this->hasMany(static::class, $this->getLtreeParentColumn());
0 ignored issues
show
Bug introduced by
It seems like hasMany() 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

37
        return $this->/** @scrutinizer ignore-call */ hasMany(static::class, $this->getLtreeParentColumn());
Loading history...
38
    }
39
40 1
    public function isParentOf(int $id): bool
41
    {
42 1
        return self::descendantsOf($this)->withoutSelf($this->getKey())->find($id) !== null;
0 ignored issues
show
Bug introduced by
It seems like getKey() 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

42
        return self::descendantsOf($this)->withoutSelf($this->/** @scrutinizer ignore-call */ getKey())->find($id) !== null;
Loading history...
43
    }
44
45 6
    public function scopeParentsOf(Builder $query, array $paths): Builder
46
    {
47 6
        return $query->whereRaw(sprintf(
0 ignored issues
show
Bug introduced by
The method whereRaw() does not exist on Illuminate\Database\Eloquent\Builder. ( Ignorable by Annotation )

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

47
        return $query->/** @scrutinizer ignore-call */ whereRaw(sprintf(

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...
48 6
            "%s @> array['%s']::ltree[]",
49 6
            $this->getLtreePathColumn(),
50 6
            implode("', '", $paths)
51 6
        ));
52
    }
53
54 1
    public function scopeRoot(Builder $query): Builder
55
    {
56 1
        return $query->whereNull($this->getLtreeParentColumn());
0 ignored issues
show
Bug introduced by
The method whereNull() does not exist on Illuminate\Database\Eloquent\Builder. ( Ignorable by Annotation )

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

56
        return $query->/** @scrutinizer ignore-call */ whereNull($this->getLtreeParentColumn());

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...
57
    }
58
59 7
    public function scopeDescendantsOf(Builder $query, LTreeModelInterface $model, bool $reverse = true): Builder
60
    {
61 7
        return $query->whereRaw(sprintf(
62 7
            "({$this->getLtreePathColumn()} <@ text2ltree('%s')) = %s",
63 7
            $model->getLtreePath(LTreeModelInterface::AS_STRING),
64 7
            $reverse ? 'true' : 'false'
65 7
        ));
66
    }
67
68 3
    public function scopeAncestorsOf(Builder $query, LTreeModelInterface $model, bool $reverse = true): Builder
69
    {
70 3
        return $query->whereRaw(sprintf(
71 3
            "({$this->getLtreePathColumn()} @> text2ltree('%s')) = %s",
72 3
            $model->getLtreePath(LTreeModelInterface::AS_STRING),
73 3
            $reverse ? 'true' : 'false'
74 3
        ));
75
    }
76
77 5
    public function scopeWithoutSelf(Builder $query, int $id): Builder
78
    {
79 5
        return $query->whereRaw(sprintf('%s <> %s', $this->getKeyName(), $id));
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

79
        return $query->whereRaw(sprintf('%s <> %s', $this->/** @scrutinizer ignore-call */ getKeyName(), $id));
Loading history...
80
    }
81
82 2
    public function getLtreeProxyUpdateColumns(): array
83
    {
84 2
        return [$this->getUpdatedAtColumn()];
0 ignored issues
show
Bug introduced by
It seems like getUpdatedAtColumn() 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

84
        return [$this->/** @scrutinizer ignore-call */ getUpdatedAtColumn()];
Loading history...
85
    }
86
87 1
    public function getLtreeProxyDeleteColumns(): array
88
    {
89 1
        return [$this->getDeletedAtColumn()];
0 ignored issues
show
Bug introduced by
It seems like getDeletedAtColumn() 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

89
        return [$this->/** @scrutinizer ignore-call */ getDeletedAtColumn()];
Loading history...
90
    }
91
92 1
    public function getAncestorByLevel(int $level = 1)
93
    {
94 1
        return static::ancestorByLevel($level)->first();
95
    }
96
97 1
    public function scopeAncestorByLevel(Builder $query, int $level = 1, ?string $path = null): Builder
98
    {
99 1
        return $query->whereRaw(sprintf(
100 1
            "({$this->getLtreePathColumn()} @> text2ltree('%s')) and nlevel({$this->getLtreePathColumn()}) = %d",
101 1
            $path ?: $this->getLtreePath(LTreeModelInterface::AS_STRING),
0 ignored issues
show
Bug introduced by
It seems like $path ?: $this->getLtree...elInterface::AS_STRING) can also be of type array and string[]; however, parameter $values of sprintf() does only seem to accept double|integer|string, maybe add an additional type check? ( Ignorable by Annotation )

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

101
            /** @scrutinizer ignore-type */ $path ?: $this->getLtreePath(LTreeModelInterface::AS_STRING),
Loading history...
102 1
            $level
103 1
        ));
104
    }
105
}
106