Completed
Push — master ( 0ced85...cabf1d )
by recca
10:07
created

QueriesRelationships::withAvg()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 4
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Recca0120\Repository\Concerns;
4
5
use Closure;
6
use Illuminate\Database\Eloquent\Builder;
7
use Recca0120\Repository\Method;
8
9
trait QueriesRelationships
10
{
11
    /**
12
     * Add a relationship count / exists condition to the query.
13
     *
14
     * @param string $relation
15
     * @param string $operator
16
     * @param int $count
17
     * @param string $boolean
18
     * @param \Closure|null $callback
19
     * @return $this
20
     */
21 1
    public function has($relation, $operator = '>=', $count = 1, $boolean = 'and', Closure $callback = null)
22
    {
23 1
        $this->methods[] = new Method(__FUNCTION__, [$relation, $operator, $count, $boolean, $callback]);
0 ignored issues
show
Bug introduced by
The property methods does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
24
25 1
        return $this;
26
    }
27
28
    /**
29
     * Add a relationship count / exists condition to the query with an "or".
30
     *
31
     * @param string $relation
32
     * @param string $operator
33
     * @param int $count
34
     * @return $this
35
     */
36 1
    public function orHas($relation, $operator = '>=', $count = 1)
37
    {
38 1
        $this->methods[] = new Method(__FUNCTION__, [$relation, $operator, $count]);
39
40 1
        return $this;
41
    }
42
43
    /**
44
     * Add a relationship count / exists condition to the query.
45
     *
46
     * @param string $relation
47
     * @param string $boolean
48
     * @param \Closure|null $callback
49
     * @return $this
50
     */
51 1
    public function doesntHave($relation, $boolean = 'and', Closure $callback = null)
52
    {
53 1
        $this->methods[] = new Method(__FUNCTION__, [$relation, $boolean, $callback]);
54
55 1
        return $this;
56
    }
57
58
    /**
59
     * Add a relationship count / exists condition to the query with where clauses.
60
     *
61
     * @param string $relation
62
     * @param \Closure|null $callback
63
     * @param string $operator
64
     * @param int $count
65
     * @return $this
66
     */
67 1
    public function whereHas($relation, Closure $callback = null, $operator = '>=', $count = 1)
68
    {
69 1
        $this->methods[] = new Method(__FUNCTION__, [$relation, $callback, $operator, $count]);
70
71 1
        return $this;
72
    }
73
74
    /**
75
     * Add a relationship count / exists condition to the query with where clauses and an "or".
76
     *
77
     * @param string $relation
78
     * @param \Closure|null $callback
79
     * @param string $operator
80
     * @param int $count
81
     * @return $this
82
     */
83 1
    public function orWhereHas($relation, Closure $callback = null, $operator = '>=', $count = 1)
84
    {
85 1
        $this->methods[] = new Method(__FUNCTION__, [$relation, $callback, $operator, $count]);
86
87 1
        return $this;
88
    }
89
90
    /**
91
     * Add a relationship count / exists condition to the query with where clauses.
92
     *
93
     * @param string $relation
94
     * @param \Closure|null $callback
95
     * @return $this
96
     */
97 1
    public function whereDoesntHave($relation, Closure $callback = null)
98
    {
99 1
        $this->methods[] = new Method(__FUNCTION__, [$relation, $callback]);
100
101 1
        return $this;
102
    }
103
104
    /**
105
     * Add subselect queries to count the relations.
106
     *
107
     * @param mixed $relations
108
     * @return $this
109
     */
110 1
    public function withCount($relations)
111
    {
112 1
        $this->methods[] = new Method(__FUNCTION__, [$relations]);
113
114 1
        return $this;
115
    }
116
117
    /**
118
     * Add subselect queries to include the max of the relation's column.
119
     *
120
     * @param string|array $relation
121
     * @param string $column
122
     * @return $this
123 1
     */
124
    public function withMax($relation, $column)
125 1
    {
126
        $this->methods[] = new Method(__FUNCTION__, [$relation, $column]);
127 1
128
        return $this;
129
    }
130
131
    /**
132
     * Add subselect queries to include the min of the relation's column.
133
     *
134
     * @param string|array $relation
135
     * @param string $column
136
     * @return $this
137
     */
138
    public function withMin($relation, $column)
139
    {
140
        $this->methods[] = new Method(__FUNCTION__, [$relation, $column]);
141
142
        return $this;
143
    }
144
145
    /**
146
     * Add subselect queries to include the sum of the relation's column.
147
     *
148
     * @param string|array $relation
149
     * @param string $column
150
     * @return $this
151
     */
152
    public function withSum($relation, $column)
153
    {
154
        $this->methods[] = new Method(__FUNCTION__, [$relation, $column]);
155
156
        return $this;
157
    }
158
159
    /**
160
     * Add subselect queries to include the average of the relation's column.
161
     *
162
     * @param string|array $relation
163
     * @param string $column
164
     * @return $this
165
     */
166
    public function withAvg($relation, $column)
167
    {
168
        return $this->withAggregate($relation, $column, 'avg');
0 ignored issues
show
Bug introduced by
It seems like withAggregate() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
169
    }
170
171
    /**
172
     * Merge the where constraints from another query to the current query.
173
     *
174
     * @param \Illuminate\Database\Eloquent\Builder $from
175
     * @return $this
176
     */
177
    public function mergeConstraintsFrom(Builder $from)
178
    {
179
        $this->methods[] = new Method(__FUNCTION__, [$from]);
180
181
        return $this;
182
    }
183
}
184