Issues (121)

src/Database/Eloquent/RelationMixin.php (29 issues)

1
<?php
2
3
namespace BiiiiiigMonster\Hasin\Database\Eloquent;
4
5
use Closure;
6
use Illuminate\Database\Eloquent\Builder;
7
use Illuminate\Database\Eloquent\Relations\BelongsTo;
8
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
9
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
10
use Illuminate\Database\Eloquent\Relations\HasOne;
11
use Illuminate\Database\Eloquent\Relations\HasOneOrMany;
12
use Illuminate\Database\Eloquent\Relations\MorphOne;
13
use Illuminate\Database\Eloquent\Relations\MorphOneOrMany;
14
use Illuminate\Database\Eloquent\Relations\MorphToMany;
15
use LogicException;
16
17
class RelationMixin
18
{
19
    public function getRelationExistenceInQuery(): Closure
20
    {
21
        return function (Builder $query, Builder $parentQuery, $columns = ['*']): Builder {
0 ignored issues
show
The parameter $columns is not used and could be removed. ( Ignorable by Annotation )

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

21
        return function (Builder $query, Builder $parentQuery, /** @scrutinizer ignore-unused */ $columns = ['*']): Builder {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
22
            $relation = function (Builder $query, Builder $parentQuery, $columns = ['*']): Builder {
23
                return $query->select($columns);
0 ignored issues
show
The method select() 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

23
                return $query->/** @scrutinizer ignore-call */ select($columns);

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...
24
            };
25
            // basic builder
26
            $belongsTo = function (Builder $query, Builder $parentQuery) use ($relation): Builder {
27
                $columns = $query->qualifyColumn($this->ownerKey);
0 ignored issues
show
Bug Best Practice introduced by
The property ownerKey does not exist on BiiiiiigMonster\Hasin\Da...\Eloquent\RelationMixin. Did you maybe forget to declare it?
Loading history...
The method qualifyColumn() 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

27
                /** @scrutinizer ignore-call */ 
28
                $columns = $query->qualifyColumn($this->ownerKey);

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...
28
29
                $relationQuery = $relation($query, $parentQuery, $columns);
30
31
                if ($parentQuery->getQuery()->from == $query->getQuery()->from) {
0 ignored issues
show
The method getQuery() 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

31
                if ($parentQuery->/** @scrutinizer ignore-call */ getQuery()->from == $query->getQuery()->from) {

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...
32
                    $relationQuery->from(
33
                        $query->getModel()->getTable().' as '.$hash = $this->getRelationCountHash()
0 ignored issues
show
The method getModel() 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

33
                        $query->/** @scrutinizer ignore-call */ 
34
                                getModel()->getTable().' as '.$hash = $this->getRelationCountHash()

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...
The method getRelationCountHash() does not exist on BiiiiiigMonster\Hasin\Da...\Eloquent\RelationMixin. ( Ignorable by Annotation )

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

33
                        $query->getModel()->getTable().' as '.$hash = $this->/** @scrutinizer ignore-call */ getRelationCountHash()

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...
34
                    );
35
36
                    $relationQuery->getModel()->setTable($hash);
37
                }
38
39
                return $relationQuery;
40
            };
41
            $belongsToMany = function (Builder $query, Builder $parentQuery) use ($relation): Builder {
42
                $columns = $this->getExistenceCompareKey();
0 ignored issues
show
The method getExistenceCompareKey() does not exist on BiiiiiigMonster\Hasin\Da...\Eloquent\RelationMixin. ( Ignorable by Annotation )

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

42
                /** @scrutinizer ignore-call */ 
43
                $columns = $this->getExistenceCompareKey();

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...
43
                if ($parentQuery->getQuery()->from == $query->getQuery()->from) {
44
                    $query->select($columns);
45
46
                    $query->from($this->related->getTable().' as '.$hash = $this->getRelationCountHash());
0 ignored issues
show
The method from() 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

46
                    $query->/** @scrutinizer ignore-call */ 
47
                            from($this->related->getTable().' as '.$hash = $this->getRelationCountHash());

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...
Bug Best Practice introduced by
The property related does not exist on BiiiiiigMonster\Hasin\Da...\Eloquent\RelationMixin. Did you maybe forget to declare it?
Loading history...
47
48
                    $this->related->setTable($hash);
49
                }
50
51
                $this->performJoin($query);
0 ignored issues
show
The method performJoin() does not exist on BiiiiiigMonster\Hasin\Da...\Eloquent\RelationMixin. ( Ignorable by Annotation )

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

51
                $this->/** @scrutinizer ignore-call */ 
52
                       performJoin($query);

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...
52
53
                return $relation($query, $parentQuery, $columns);
54
            };
55
            $hasOneOrMany = function (Builder $query, Builder $parentQuery) use ($relation): Builder {
56
                $columns = $this->getExistenceCompareKey();
57
                if ($query->getQuery()->from == $parentQuery->getQuery()->from) {
58
                    $query->from($query->getModel()->getTable().' as '.$hash = $this->getRelationCountHash());
59
60
                    $query->getModel()->setTable($hash);
61
                }
62
63
                return $relation($query, $parentQuery, $columns);
64
            };
65
            $hasManyThrough = function (Builder $query, Builder $parentQuery) use ($relation): Builder {
66
                $columns = $this->getQualifiedFirstKeyName();
0 ignored issues
show
The method getQualifiedFirstKeyName() does not exist on BiiiiiigMonster\Hasin\Da...\Eloquent\RelationMixin. ( Ignorable by Annotation )

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

66
                /** @scrutinizer ignore-call */ 
67
                $columns = $this->getQualifiedFirstKeyName();

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...
67
                if ($parentQuery->getQuery()->from === $query->getQuery()->from) {
68
                    $query->from($query->getModel()->getTable().' as '.$hash = $this->getRelationCountHash());
69
70
                    $query->join($this->throughParent->getTable(), $this->getQualifiedParentKeyName(), '=', $hash.'.'.$this->secondKey);
0 ignored issues
show
The method getQualifiedParentKeyName() does not exist on BiiiiiigMonster\Hasin\Da...\Eloquent\RelationMixin. ( Ignorable by Annotation )

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

70
                    $query->join($this->throughParent->getTable(), $this->/** @scrutinizer ignore-call */ getQualifiedParentKeyName(), '=', $hash.'.'.$this->secondKey);

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...
The method join() 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

70
                    $query->/** @scrutinizer ignore-call */ 
71
                            join($this->throughParent->getTable(), $this->getQualifiedParentKeyName(), '=', $hash.'.'.$this->secondKey);

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...
Bug Best Practice introduced by
The property secondKey does not exist on BiiiiiigMonster\Hasin\Da...\Eloquent\RelationMixin. Did you maybe forget to declare it?
Loading history...
Bug Best Practice introduced by
The property throughParent does not exist on BiiiiiigMonster\Hasin\Da...\Eloquent\RelationMixin. Did you maybe forget to declare it?
Loading history...
71
72
                    if ($this->throughParentSoftDeletes()) {
0 ignored issues
show
The method throughParentSoftDeletes() does not exist on BiiiiiigMonster\Hasin\Da...\Eloquent\RelationMixin. ( Ignorable by Annotation )

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

72
                    if ($this->/** @scrutinizer ignore-call */ throughParentSoftDeletes()) {

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...
73
                        $query->whereNull($this->throughParent->getQualifiedDeletedAtColumn());
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

73
                        $query->/** @scrutinizer ignore-call */ 
74
                                whereNull($this->throughParent->getQualifiedDeletedAtColumn());

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...
74
                    }
75
76
                    $query->getModel()->setTable($hash);
77
78
                    return $relation($query, $parentQuery, $columns);
79
                }
80
81
                if ($parentQuery->getQuery()->from === $this->throughParent->getTable()) {
82
                    $table = $this->throughParent->getTable().' as '.$hash = $this->getRelationCountHash();
83
84
                    $query->join($table, $hash.'.'.$this->secondLocalKey, '=', $this->getQualifiedFarKeyName());
0 ignored issues
show
The method getQualifiedFarKeyName() does not exist on BiiiiiigMonster\Hasin\Da...\Eloquent\RelationMixin. ( Ignorable by Annotation )

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

84
                    $query->join($table, $hash.'.'.$this->secondLocalKey, '=', $this->/** @scrutinizer ignore-call */ getQualifiedFarKeyName());

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...
Bug Best Practice introduced by
The property secondLocalKey does not exist on BiiiiiigMonster\Hasin\Da...\Eloquent\RelationMixin. Did you maybe forget to declare it?
Loading history...
85
86
                    if ($this->throughParentSoftDeletes()) {
87
                        $query->whereNull($hash.'.'.$this->throughParent->getDeletedAtColumn());
88
                    }
89
90
                    return $relation($query, $parentQuery, $columns);
91
                }
92
93
                $this->performJoin($query);
94
95
                return $relation($query, $parentQuery, $columns);
96
            };
97
            // extended builder
98
            $hasOne = function (Builder $query, Builder $parentQuery) use ($hasOneOrMany): Builder {
99
                if ($this->isOneOfMany()) {
0 ignored issues
show
The method isOneOfMany() does not exist on BiiiiiigMonster\Hasin\Da...\Eloquent\RelationMixin. ( Ignorable by Annotation )

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

99
                if ($this->/** @scrutinizer ignore-call */ isOneOfMany()) {

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...
100
                    $this->mergeOneOfManyJoinsTo($query);
0 ignored issues
show
The method mergeOneOfManyJoinsTo() does not exist on BiiiiiigMonster\Hasin\Da...\Eloquent\RelationMixin. ( Ignorable by Annotation )

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

100
                    $this->/** @scrutinizer ignore-call */ 
101
                           mergeOneOfManyJoinsTo($query);

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...
101
                }
102
103
                return $hasOneOrMany($query, $parentQuery);
104
            };
105
            $morphOneOrMany = function (Builder $query, Builder $parentQuery) use ($hasOneOrMany): Builder {
106
                return $hasOneOrMany($query, $parentQuery)->where(
107
                    $query->qualifyColumn($this->getMorphType()),
0 ignored issues
show
The method getMorphType() does not exist on BiiiiiigMonster\Hasin\Da...\Eloquent\RelationMixin. ( Ignorable by Annotation )

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

107
                    $query->qualifyColumn($this->/** @scrutinizer ignore-call */ getMorphType()),

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...
108
                    $this->morphClass
0 ignored issues
show
Bug Best Practice introduced by
The property morphClass does not exist on BiiiiiigMonster\Hasin\Da...\Eloquent\RelationMixin. Did you maybe forget to declare it?
Loading history...
109
                );
110
            };
111
            $morphOne = function (Builder $query, Builder $parentQuery) use ($morphOneOrMany): Builder {
112
                if ($this->isOneOfMany()) {
113
                    $this->mergeOneOfManyJoinsTo($query);
114
                }
115
116
                return $morphOneOrMany($query, $parentQuery);
117
            };
118
            $morphToMany = function (Builder $query, Builder $parentQuery) use ($belongsToMany): Builder {
119
                return $belongsToMany($query, $parentQuery)->where(
120
                    $this->qualifyPivotColumn($this->morphType),
0 ignored issues
show
Bug Best Practice introduced by
The property morphType does not exist on BiiiiiigMonster\Hasin\Da...\Eloquent\RelationMixin. Did you maybe forget to declare it?
Loading history...
The method qualifyPivotColumn() does not exist on BiiiiiigMonster\Hasin\Da...\Eloquent\RelationMixin. ( Ignorable by Annotation )

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

120
                    $this->/** @scrutinizer ignore-call */ 
121
                           qualifyPivotColumn($this->morphType),

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...
121
                    $this->morphClass
0 ignored issues
show
Bug Best Practice introduced by
The property morphClass does not exist on BiiiiiigMonster\Hasin\Da...\Eloquent\RelationMixin. Did you maybe forget to declare it?
Loading history...
122
                );
123
            };
124
125
            return match (true) {
126
                // extended relation
127
                $this instanceof HasOne => $hasOne($query, $parentQuery),
128
                $this instanceof MorphOne => $morphOne($query, $parentQuery),
129
                $this instanceof MorphOneOrMany => $morphOneOrMany($query, $parentQuery),
130
                $this instanceof MorphToMany => $morphToMany($query, $parentQuery),
131
                // basic relation
132
                $this instanceof BelongsTo => $belongsTo($query, $parentQuery),
133
                $this instanceof BelongsToMany => $belongsToMany($query, $parentQuery),
134
                $this instanceof HasOneOrMany => $hasOneOrMany($query, $parentQuery),
135
                $this instanceof HasManyThrough => $hasManyThrough($query, $parentQuery),
136
                default => throw new LogicException(
137
                    sprintf('%s must be a relationship instance.', $this::class)
138
                )
139
            };
140
        };
141
    }
142
143
    public function getRelationWhereInKey(): Closure
144
    {
145
        return fn (): string => match (true) {
146
            $this instanceof BelongsTo => $this->getQualifiedForeignKeyName(),
0 ignored issues
show
The method getQualifiedForeignKeyName() does not exist on BiiiiiigMonster\Hasin\Da...\Eloquent\RelationMixin. ( Ignorable by Annotation )

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

146
            $this instanceof BelongsTo => $this->/** @scrutinizer ignore-call */ getQualifiedForeignKeyName(),

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...
147
            $this instanceof HasOneOrMany, $this instanceof BelongsToMany => $this->getQualifiedParentKeyName(),
148
            $this instanceof HasManyThrough => $this->getQualifiedLocalKeyName(),
0 ignored issues
show
The method getQualifiedLocalKeyName() does not exist on BiiiiiigMonster\Hasin\Da...\Eloquent\RelationMixin. ( Ignorable by Annotation )

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

148
            $this instanceof HasManyThrough => $this->/** @scrutinizer ignore-call */ getQualifiedLocalKeyName(),

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...
149
            default => throw new LogicException(
150
                sprintf('%s must be a relationship instance.', $this::class)
151
            )
152
        };
153
    }
154
}
155