BelongsToManyOfDescendants::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 13
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 8
crap 1

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace Staudenmeir\LaravelAdjacencyList\Eloquent\Relations;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
8
use Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Traits\IsOfDescendantsRelation;
9
10
/**
11
 * @template TRelatedModel of \Illuminate\Database\Eloquent\Model
12
 * @extends BelongsToMany<TRelatedModel>
13
 */
14
class BelongsToManyOfDescendants extends BelongsToMany
15
{
16
    use IsOfDescendantsRelation {
0 ignored issues
show
Bug introduced by
The trait Staudenmeir\LaravelAdjac...IsOfDescendantsRelation requires the property $from which is not provided by Staudenmeir\LaravelAdjac...ongsToManyOfDescendants.
Loading history...
17
        addConstraints as baseAddConstraints;
18
        getRelationExistenceQuery as baseGetRelationExistenceQuery;
19
    }
20
21
    /**
22
     * Create a new belongs to many of descendants relationship instance.
23
     *
24
     * @param \Illuminate\Database\Eloquent\Builder $query
25
     * @param \Illuminate\Database\Eloquent\Model $parent
26
     * @param string $table
27
     * @param string $foreignPivotKey
28
     * @param string $relatedPivotKey
29
     * @param string $parentKey
30
     * @param string $relatedKey
31
     * @param bool $andSelf
32
     */
33 255
    public function __construct(
34
        Builder $query,
35
        Model $parent,
36
        $table,
37
        $foreignPivotKey,
38
        $relatedPivotKey,
39
        $parentKey,
40
        $relatedKey,
41
        $andSelf
42
    ) {
43 255
        $this->andSelf = $andSelf;
44
45 255
        parent::__construct($query, $parent, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey);
46
    }
47
48
    /**
49
     * Set the base constraints on the relation query.
50
     *
51
     * @return void
52
     */
53 255
    public function addConstraints()
54
    {
55 255
        $this->performJoin();
56
57 255
        $this->baseAddConstraints();
58
    }
59
60
    /**
61
     * Set the where clause on the recursive expression query.
62
     *
63
     * @param \Illuminate\Database\Eloquent\Builder $query
64
     * @return void
65
     */
66 159
    protected function addExpressionWhereConstraints(Builder $query)
67
    {
68 159
        $column = $this->andSelf ? $this->parent->getLocalKeyName() : $this->parent->getParentKeyName();
69
70 159
        $query->where(
71 159
            $column,
72 159
            '=',
73 159
            $this->parent->{$this->parentKey}
74 159
        );
75
    }
76
77
    /**
78
     * Get the local key name for an eager load of the relation.
79
     *
80
     * @return string
81
     */
82 60
    public function getEagerLoadingLocalKeyName()
83
    {
84 60
        return $this->parentKey;
85
    }
86
87
    /**
88
     * Get the foreign key name for an eager load of the relation.
89
     *
90
     * @return string
91
     */
92 60
    public function getEagerLoadingForeignKeyName()
93
    {
94 60
        return $this->foreignPivotKey;
95
    }
96
97
    /**
98
     * Get the accessor for an eager load of the relation.
99
     *
100
     * @return string|null
101
     */
102 60
    public function getEagerLoadingAccessor()
103
    {
104 60
        return $this->accessor;
105
    }
106
107
    /**
108
     * Add the constraints for a relationship query.
109
     *
110
     * @param \Illuminate\Database\Eloquent\Builder $query
111
     * @param \Illuminate\Database\Eloquent\Builder $parentQuery
112
     * @param array|mixed $columns
113
     * @return \Illuminate\Database\Eloquent\Builder
114
     */
115 36
    public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, $columns = ['*'])
116
    {
117 36
        $this->performJoin($query);
118
119 36
        return $this->baseGetRelationExistenceQuery($query, $parentQuery, $columns);
120
    }
121
122
    /**
123
     * Get the local key name for the recursion expression.
124
     *
125
     * @return string
126
     */
127 225
    public function getExpressionLocalKeyName()
128
    {
129 225
        return $this->parentKey;
130
    }
131
132
    /**
133
     * Get the foreign key name for the recursion expression.
134
     *
135
     * @return string
136
     */
137 225
    public function getExpressionForeignKeyName()
138
    {
139 225
        return $this->foreignPivotKey;
140
    }
141
}
142