Test Failed
Pull Request — master (#48)
by
unknown
03:03
created

src/Traits/LTreeModelTrait.php (8 issues)

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
    public function newCollection(array $models = []): LTreeCollection
26
    {
27
        return new LTreeCollection($models);
28
    }
29
30
    public function ltreeParent(): BelongsTo
31
    {
32
        return $this->belongsTo(static::class, $this->getLtreeParentColumn());
0 ignored issues
show
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
    public function ltreeChildren(): HasMany
36
    {
37
        return $this->hasMany(static::class, $this->getLtreeParentColumn());
0 ignored issues
show
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
    public function isParentOf(int $id): bool
41
    {
42
        return self::descendantsOf($this)->withoutSelf($this->getKey())->find($id) !== null;
0 ignored issues
show
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
    public function scopeParentsOf(Builder $query, array $paths): Builder
46
    {
47
        return $query->whereRaw(sprintf(
0 ignored issues
show
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
            "%s @> array['%s']::ltree[]",
49
            $this->getLtreePathColumn(),
50
            implode("', '", $paths)
51
        ));
52
    }
53
54
    public function scopeRoot(Builder $query): Builder
55
    {
56
        return $query->whereNull($this->getLtreeParentColumn());
0 ignored issues
show
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
    public function scopeDescendantsOf(Builder $query, LTreeModelInterface $model, bool $reverse = true): Builder
60
    {
61
        return $query->whereRaw(sprintf(
62
            "({$this->getLtreePathColumn()} <@ text2ltree('%s')) = %s",
63
            $model->getLtreePath(LTreeModelInterface::AS_STRING),
64
            $reverse ? 'true' : 'false'
65
        ));
66
    }
67
68
    public function scopeAncestorsOf(Builder $query, LTreeModelInterface $model, bool $reverse = true): Builder
69
    {
70
        return $query->whereRaw(sprintf(
71
            "({$this->getLtreePathColumn()} @> text2ltree('%s')) = %s",
72
            $model->getLtreePath(LTreeModelInterface::AS_STRING),
73
            $reverse ? 'true' : 'false'
74
        ));
75
    }
76
77
    public function scopeWithoutSelf(Builder $query, int $id): Builder
78
    {
79
        return $query->whereRaw(sprintf('%s <> %s', $this->getKeyName(), $id));
0 ignored issues
show
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
    public function getLtreeProxyUpdateColumns(): array
83
    {
84
        return [$this->getUpdatedAtColumn()];
0 ignored issues
show
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
    public function getLtreeProxyDeleteColumns(): array
88
    {
89
        return [$this->getDeletedAtColumn()];
0 ignored issues
show
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
    public function getAncestorByLevel(int $level = 1)
93
    {
94
        return static::ancestorByLevel($level)->first();
95
    }
96
97
    public function scopeAncestorByLevel(Builder $query, int $level = 1, ?string $path = null): Builder
98
    {
99
        return $query->whereRaw(sprintf(
100
            "({$this->getLtreePathColumn()} @> text2ltree('%s')) and nlevel({$this->getLtreePathColumn()}) = %d",
101
            $path ?: $this->getLtreePath(LTreeModelInterface::AS_STRING),
102
            $level
103
        ));
104
    }
105
}
106