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

MorphToManyOfDescendants::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 23
ccs 5
cts 5
cp 1
rs 9.9
cc 1
nc 1
nop 9
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
8
class MorphToManyOfDescendants extends BelongsToManyOfDescendants
9
{
10
    /**
11
     * The type of the polymorphic relation.
12
     *
13
     * @var string
14
     */
15
    protected $morphType;
16
17
    /**
18
     * The class name of the morph type constraint.
19
     *
20
     * @var string
21
     */
22
    protected $morphClass;
23
24
    /**
25
     * Create a new morph to many of descendants relationship instance.
26
     *
27
     * @param \Illuminate\Database\Eloquent\Builder $query
28
     * @param \Illuminate\Database\Eloquent\Model $parent
29
     * @param string  $name
30
     * @param string  $table
31
     * @param string  $foreignPivotKey
32
     * @param string  $relatedPivotKey
33
     * @param string  $parentKey
34
     * @param string  $relatedKey
35
     * @param bool $andSelf
36
     */
37 72
    public function __construct(
38
        Builder $query,
39
        Model $parent,
40
        $name,
41
        $table,
42
        $foreignPivotKey,
43
        $relatedPivotKey,
44
        $parentKey,
45
        $relatedKey,
46
        $andSelf
47
    ) {
48 72
        $this->morphType = $name.'_type';
49 72
        $this->morphClass = $parent->getMorphClass();
50
51 72
        parent::__construct(
52 72
            $query,
53
            $parent,
54
            $table,
55
            $foreignPivotKey,
56
            $relatedPivotKey,
57
            $parentKey,
58
            $relatedKey,
59
            $andSelf
60
        );
61 72
    }
62
63
    /**
64
     * Set the where clause on the recursive expression query.
65
     *
66
     * @param \Illuminate\Database\Eloquent\Builder $query
67
     * @return void
68
     */
69 44
    protected function addExpressionWhereConstraints(Builder $query)
70
    {
71 44
        parent::addExpressionWhereConstraints($query);
72
73 44
        $this->query->where(
74 44
            "$this->table.$this->morphType",
75 44
            $this->morphClass
76
        );
77 44
    }
78
79
    /**
80
     * Set the where clause on the recursive expression query for an eager load of the relation.
81
     *
82
     * @param \Illuminate\Database\Eloquent\Builder $query
83
     * @param array $models
84
     * @return void
85
     */
86 16
    public function addEagerExpressionWhereConstraints(Builder $query, array $models)
87
    {
88 16
        parent::addEagerExpressionWhereConstraints($query, $models);
89
90 16
        $this->query->where(
91 16
            "$this->table.$this->morphType",
92 16
            $this->morphClass
93
        );
94 16
    }
95
96
    /**
97
     * Set the where clause on the recursive expression query for an existence query.
98
     *
99
     * @param \Illuminate\Database\Eloquent\Builder $query
100
     * @param string $table
101
     * @return void
102
     */
103 12
    public function addExistenceExpressionWhereConstraints(Builder $query, $table)
104
    {
105 12
        parent::addExistenceExpressionWhereConstraints($query, $table);
106
107 12
        $this->query->where(
108 12
            "$this->table.$this->morphType",
109 12
            $this->morphClass
110
        );
111 12
    }
112
}
113