Passed
Push — master ( 98eee9...aa1f26 )
by Jonas
09:56
created

BelongsToManyOfDescendants::__construct()   A

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