Passed
Push — master ( 426f29...1618a6 )
by Jonas
03:17
created

HasEagerLimit::newMorphToMany()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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

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\MorphMany;
18
use Staudenmeir\EloquentEagerLimit\Relations\MorphOne;
19
use Staudenmeir\EloquentEagerLimit\Relations\MorphToMany;
20
21
trait HasEagerLimit
22
{
23
    /**
24
     * Get a new query builder instance for the connection.
25
     *
26
     * @return \Illuminate\Database\Query\Builder
27
     */
28 24
    protected function newBaseQueryBuilder()
29
    {
30 24
        $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

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