Failed Conditions
Push — refactor/improve-static-analys... ( bdf823...46faab )
by Bas
10:08
created

BuildsGroups::havingNested()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 7
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LaravelFreelancerNL\Aranguent\Query\Concerns;
6
7
use Carbon\CarbonPeriod;
8
use Closure;
9
use Illuminate\Contracts\Database\Query\ConditionExpression;
10
use Illuminate\Contracts\Database\Query\Expression;
11
use Illuminate\Support\Arr;
12
13
trait BuildsGroups
14
{
15
    /**
16
     * Add a "group by" clause to the query.
17
     *
18
     * @param array|\Illuminate\Contracts\Database\Query\Expression|string ...$groups
19
     * @return $this
20
     */
21
    public function groupBy(...$groups)
22
    {
23
        foreach ($groups as $group) {
24
            $this->groups = array_merge(
0 ignored issues
show
Bug Best Practice introduced by
The property groups does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
25
                (array)$this->groups,
26
                Arr::wrap($group)
27
            );
28
        }
29
30
        return $this;
31
    }
32
33
    public function having($column, $operator = null, $value = null, $boolean = 'and')
34
    {
35
        $type = 'Basic';
36
37
        if ($column instanceof ConditionExpression) {
38
            $type = 'Expression';
39
40
            $this->havings[] = compact('type', 'column', 'boolean');
0 ignored issues
show
Bug Best Practice introduced by
The property havings does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
41
42
            return $this;
43
        }
44
45
        // Here we will make some assumptions about the operator. If only 2 values are
46
        // passed to the method, we will assume that the operator is an equals sign
47
        // and keep going. Otherwise, we'll require the operator to be passed in.
48
        [$value, $operator] = $this->prepareValueAndOperator(
0 ignored issues
show
Bug introduced by
It seems like prepareValueAndOperator() 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

48
        /** @scrutinizer ignore-call */ 
49
        [$value, $operator] = $this->prepareValueAndOperator(
Loading history...
49
            $value,
50
            $operator,
51
            func_num_args() === 2
52
        );
53
54
        if ($column instanceof Closure && is_null($operator)) {
55
            return $this->havingNested($column, $boolean);
56
        }
57
58
        // If the given operator is not found in the list of valid operators we will
59
        // assume that the developer is just short-cutting the '=' operators and
60
        // we will set the operators to '=' and set the values appropriately.
61
        if ($this->invalidOperator($operator)) {
0 ignored issues
show
Bug introduced by
It seems like invalidOperator() 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

61
        if ($this->/** @scrutinizer ignore-call */ invalidOperator($operator)) {
Loading history...
62
            [$value, $operator] = [$operator, '='];
63
        }
64
65
        if ($this->isBitwiseOperator($operator)) {
0 ignored issues
show
Bug introduced by
It seems like isBitwiseOperator() 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

65
        if ($this->/** @scrutinizer ignore-call */ isBitwiseOperator($operator)) {
Loading history...
66
            $type = 'Bitwise';
67
        }
68
69
        if (!$value instanceof Expression) {
70
            $this->addBinding($this->flattenValue($value), 'having');
0 ignored issues
show
Bug introduced by
It seems like flattenValue() 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

70
            $this->addBinding($this->/** @scrutinizer ignore-call */ flattenValue($value), 'having');
Loading history...
Bug introduced by
It seems like addBinding() 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

70
            $this->/** @scrutinizer ignore-call */ 
71
                   addBinding($this->flattenValue($value), 'having');
Loading history...
71
            $value = '@' . array_key_last($this->getBindings());
0 ignored issues
show
Bug introduced by
It seems like getBindings() 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

71
            $value = '@' . array_key_last($this->/** @scrutinizer ignore-call */ getBindings());
Loading history...
72
        }
73
74
        $this->havings[] = compact('type', 'column', 'operator', 'value', 'boolean');
75
76
        return $this;
77
    }
78
79
    /**
80
     * Add a raw having clause to the query.
81
     *
82
     * @param  string  $sql
83
     * @param  array  $bindings
84
     * @param  string  $boolean
85
     * @return $this
86
     */
87
    public function havingRaw($sql, array $bindings = [], $boolean = 'and')
88
    {
89
        $type = 'Raw';
90
91
        $this->havings[] = compact('type', 'sql', 'boolean');
0 ignored issues
show
Bug Best Practice introduced by
The property havings does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
92
93
        if (!empty($bindings)) {
94
            $this->addBinding($bindings, 'having');
95
        }
96
97
        return $this;
98
    }
99
100
    /**
101
     * Create a new query instance for nested where condition.
102
     *
103
     * @return \Illuminate\Database\Query\Builder
104
     */
105
    public function forNestedWhere($aliases = [])
106
    {
107
        $query = $this->newQuery();
0 ignored issues
show
Bug introduced by
It seems like newQuery() 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

107
        /** @scrutinizer ignore-call */ 
108
        $query = $this->newQuery();
Loading history...
108
        foreach($aliases as $alias) {
109
            $query->groups[] = $alias;
110
111
        }
112
        return $query->from($this->from);
113
    }
114
115
    /**
116
     * Add a nested having statement to the query.
117
     *
118
     * @param  \Closure  $callback
119
     * @param  string  $boolean
120
     * @return $this
121
     */
122
    public function havingNested(Closure $callback, $boolean = 'and')
123
    {
124
        $callback($query = $this->forNestedWhere($this->groups));
125
126
127
128
        return $this->addNestedHavingQuery($query, $boolean);
129
    }
130
131
    /**
132
     * Add another query builder as a nested having to the query builder.
133
     *
134
     * @param  \Illuminate\Database\Query\Builder  $query
135
     * @param  string  $boolean
136
     * @return $this
137
     */
138
    public function addNestedHavingQuery($query, $boolean = 'and')
139
    {
140
        if (count($query->havings)) {
141
            $type = 'Nested';
142
143
            $this->havings[] = compact('type', 'query', 'boolean');
0 ignored issues
show
Bug Best Practice introduced by
The property havings does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
144
145
            $this->mergeBindings($query);
0 ignored issues
show
Bug introduced by
It seems like mergeBindings() 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

145
            $this->/** @scrutinizer ignore-call */ 
146
                   mergeBindings($query);
Loading history...
146
        }
147
148
        return $this;
149
    }
150
151
152
    /**
153
     * Add a "having between " clause to the query.
154
     *
155
     * @param  string  $column
156
     * @param  iterable  $values
157
     * @param  string  $boolean
158
     * @param  bool  $not
159
     * @return $this
160
     */
161
    public function havingBetween($column, iterable $values, $boolean = 'and', $not = false)
162
    {
163
        $type = 'between';
164
165
        if ($values instanceof CarbonPeriod) {
166
            $values = [$values->start, $values->end];
167
        }
168
169
        $bindings = array_slice($this->cleanBindings(Arr::flatten($values)), 0, 2);
0 ignored issues
show
Bug introduced by
It seems like cleanBindings() 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

169
        $bindings = array_slice($this->/** @scrutinizer ignore-call */ cleanBindings(Arr::flatten($values)), 0, 2);
Loading history...
170
171
        $this->addBinding($bindings[0], 'having');
172
        $values[0] = '@' . array_key_last($this->getBindings());
173
174
        $this->addBinding($bindings[1], 'having');
175
        $values[1] = '@' . array_key_last($this->getBindings());
176
177
        $this->havings[] = compact('type', 'column', 'values', 'boolean', 'not');
0 ignored issues
show
Bug Best Practice introduced by
The property havings does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
178
179
        return $this;
180
    }
181
}
182