Passed
Push — master ( e96f8e...772e1a )
by Jonas
06:03
created

HasEagerLimit::newMorphToMany()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 24
ccs 3
cts 3
cp 1
rs 9.9
cc 1
nc 1
nop 10
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\EloquentEagerLimit;
4
5
use Illuminate\Database\Connection;
6
use Illuminate\Database\Eloquent\Builder;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Staudenmeir\EloquentEagerLimit\Builder. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
7
use Illuminate\Database\Eloquent\Model;
8
use RuntimeException;
9
use Staudenmeir\EloquentEagerLimit\Grammars\MySqlGrammar;
10
use Staudenmeir\EloquentEagerLimit\Grammars\PostgresGrammar;
11
use Staudenmeir\EloquentEagerLimit\Grammars\SQLiteGrammar;
12
use Staudenmeir\EloquentEagerLimit\Grammars\SqlServerGrammar;
13
use Staudenmeir\EloquentEagerLimit\Relations\BelongsToMany;
14
use Staudenmeir\EloquentEagerLimit\Relations\HasMany;
15
use Staudenmeir\EloquentEagerLimit\Relations\HasManyThrough;
16
use Staudenmeir\EloquentEagerLimit\Relations\HasOne;
17
use Staudenmeir\EloquentEagerLimit\Relations\HasOneThrough;
18
use Staudenmeir\EloquentEagerLimit\Relations\MorphMany;
19
use Staudenmeir\EloquentEagerLimit\Relations\MorphOne;
20
use Staudenmeir\EloquentEagerLimit\Relations\MorphToMany;
21
22
trait HasEagerLimit
23
{
24
    /**
25
     * Get a new query builder instance for the connection.
26
     *
27
     * @return \Illuminate\Database\Query\Builder
28
     */
29 148
    protected function newBaseQueryBuilder()
30
    {
31 148
        $connection = $this->getConnection();
0 ignored issues
show
Bug introduced by
It seems like getConnection() 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

31
        /** @scrutinizer ignore-call */ 
32
        $connection = $this->getConnection();
Loading history...
32
33 148
        $grammar = $connection->withTablePrefix($this->getQueryGrammar($connection));
34
35 148
        return new \Staudenmeir\EloquentEagerLimit\Builder(
36 148
            $connection,
37
            $grammar,
38 148
            $connection->getPostProcessor()
39
        );
40
    }
41
42
    /**
43
     * Get the query grammar.
44
     *
45
     * @param \Illuminate\Database\Connection $connection
46
     * @return \Illuminate\Database\Query\Grammars\Grammar
47
     */
48 148
    protected function getQueryGrammar(Connection $connection)
49
    {
50 148
        $driver = $connection->getDriverName();
51
52 148
        switch ($driver) {
53 148
            case 'mysql':
54 37
                return new MySqlGrammar();
55 111
            case 'pgsql':
56 37
                return new PostgresGrammar();
57 74
            case 'sqlite':
58 37
                return new SQLiteGrammar();
59 37
            case 'sqlsrv':
60 37
                return new SqlServerGrammar();
61
        }
62
63
        throw new RuntimeException('This database is not supported.'); // @codeCoverageIgnore
64
    }
65
66
    /**
67
     * Instantiate a new HasOne relationship.
68
     *
69
     * @param \Illuminate\Database\Eloquent\Builder $query
70
     * @param \Illuminate\Database\Eloquent\Model $parent
71
     * @param string $foreignKey
72
     * @param string $localKey
73
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
74
     */
75 24
    protected function newHasOne(Builder $query, Model $parent, $foreignKey, $localKey)
76
    {
77 24
        return new HasOne($query, $parent, $foreignKey, $localKey);
78
    }
79
80
    /**
81
     * Instantiate a new HasOneThrough relationship.
82
     *
83
     * @param \Illuminate\Database\Eloquent\Builder $query
84
     * @param \Illuminate\Database\Eloquent\Model $farParent
85
     * @param \Illuminate\Database\Eloquent\Model $throughParent
86
     * @param string $firstKey
87
     * @param string $secondKey
88
     * @param string $localKey
89
     * @param string $secondLocalKey
90
     * @return \Illuminate\Database\Eloquent\Relations\HasOneThrough
91
     */
92 16
    protected function newHasOneThrough(Builder $query, Model $farParent, Model $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey)
93
    {
94 16
        return new HasOneThrough($query, $farParent, $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey);
95
    }
96
97
    /**
98
     * Instantiate a new MorphOne relationship.
99
     *
100
     * @param \Illuminate\Database\Eloquent\Builder $query
101
     * @param \Illuminate\Database\Eloquent\Model $parent
102
     * @param string $type
103
     * @param string $id
104
     * @param string $localKey
105
     * @return \Illuminate\Database\Eloquent\Relations\MorphOne
106
     */
107 16
    protected function newMorphOne(Builder $query, Model $parent, $type, $id, $localKey)
108
    {
109 16
        return new MorphOne($query, $parent, $type, $id, $localKey);
110
    }
111
112
    /**
113
     * Instantiate a new HasMany relationship.
114
     *
115
     * @param \Illuminate\Database\Eloquent\Builder $query
116
     * @param \Illuminate\Database\Eloquent\Model $parent
117
     * @param string $foreignKey
118
     * @param string $localKey
119
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
120
     */
121 16
    protected function newHasMany(Builder $query, Model $parent, $foreignKey, $localKey)
122
    {
123 16
        return new HasMany($query, $parent, $foreignKey, $localKey);
124
    }
125
126
    /**
127
     * Instantiate a new HasManyThrough relationship.
128
     *
129
     * @param \Illuminate\Database\Eloquent\Builder $query
130
     * @param \Illuminate\Database\Eloquent\Model $farParent
131
     * @param \Illuminate\Database\Eloquent\Model $throughParent
132
     * @param string $firstKey
133
     * @param string $secondKey
134
     * @param string $localKey
135
     * @param string $secondLocalKey
136
     * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
137
     */
138 16
    protected function newHasManyThrough(Builder $query, Model $farParent, Model $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey)
139
    {
140 16
        return new HasManyThrough($query, $farParent, $throughParent, $firstKey, $secondKey, $localKey, $secondLocalKey);
141
    }
142
143
    /**
144
     * Instantiate a new MorphMany relationship.
145
     *
146
     * @param \Illuminate\Database\Eloquent\Builder $query
147
     * @param \Illuminate\Database\Eloquent\Model $parent
148
     * @param string $type
149
     * @param string $id
150
     * @param string $localKey
151
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
152
     */
153 16
    protected function newMorphMany(Builder $query, Model $parent, $type, $id, $localKey)
154
    {
155 16
        return new MorphMany($query, $parent, $type, $id, $localKey);
156
    }
157
158
    /**
159
     * Instantiate a new BelongsToMany relationship.
160
     *
161
     * @param \Illuminate\Database\Eloquent\Builder $query
162
     * @param \Illuminate\Database\Eloquent\Model $parent
163
     * @param string $table
164
     * @param string $foreignPivotKey
165
     * @param string $relatedPivotKey
166
     * @param string $parentKey
167
     * @param string $relatedKey
168
     * @param string $relationName
169
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
170
     */
171 16
    protected function newBelongsToMany(
172
        Builder $query,
173
        Model $parent,
174
        $table,
175
        $foreignPivotKey,
176
        $relatedPivotKey,
177
        $parentKey,
178
        $relatedKey,
179
        $relationName = null
180
    )
181
    {
182 16
        return new BelongsToMany($query, $parent, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, $relationName);
183
    }
184
185
    /**
186
     * Instantiate a new MorphToMany relationship.
187
     *
188
     * @param \Illuminate\Database\Eloquent\Builder $query
189
     * @param \Illuminate\Database\Eloquent\Model $parent
190
     * @param string $name
191
     * @param string $table
192
     * @param string $foreignPivotKey
193
     * @param string $relatedPivotKey
194
     * @param string $parentKey
195
     * @param string $relatedKey
196
     * @param string $relationName
197
     * @param bool $inverse
198
     * @return \Illuminate\Database\Eloquent\Relations\MorphToMany
199
     */
200 16
    protected function newMorphToMany(
201
        Builder $query,
202
        Model $parent,
203
        $name,
204
        $table,
205
        $foreignPivotKey,
206
        $relatedPivotKey,
207
        $parentKey,
208
        $relatedKey,
209
        $relationName = null,
210
        $inverse = false
211
    )
212
    {
213 16
        return new MorphToMany(
214 16
            $query,
215
            $parent,
216
            $name,
217
            $table,
218
            $foreignPivotKey,
219
            $relatedPivotKey,
220
            $parentKey,
221
            $relatedKey,
222
            $relationName,
223
            $inverse
224
        );
225
    }
226
}
227