Passed
Push — master ( 00bef5...064b0e )
by Jonas
02:59
created

CompilesExpressions::compileSelect()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 7
c 0
b 0
f 0
dl 0
loc 15
ccs 8
cts 8
cp 1
rs 10
cc 4
nc 5
nop 1
crap 4
1
<?php
2
3
namespace Staudenmeir\LaravelCte\Query\Grammars\Traits;
4
5
use Illuminate\Database\Query\Builder;
6
use Illuminate\Support\Str;
7
use Staudenmeir\LaravelCte\Query\Builder as CteBuilder;
8
9
trait CompilesExpressions
10
{
11
    /**
12
     * Create a new grammar instance.
13
     */
14 175
    public function __construct()
15
    {
16 175
        array_unshift($this->selectComponents, 'expressions');
17
18 175
        $this->selectComponents[] = 'recursionLimit';
0 ignored issues
show
Bug Best Practice introduced by
The property selectComponents does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
19
    }
20
21
    /**
22
     * Compile the common table expressions.
23
     *
24
     * @param \Illuminate\Database\Query\Builder $query
25
     * @param array $expressions
26
     * @return string
27
     */
28 175
    public function compileExpressions(Builder $query, array $expressions)
29
    {
30 175
        if (!$expressions) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $expressions of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
31 168
            return '';
32
        }
33
34 160
        $recursive = $this->recursiveKeyword($expressions);
35
36 160
        $statements = [];
37
38 160
        foreach ($expressions as $expression) {
39 160
            $columns = $expression['columns'] ? '('.$this->columnize($expression['columns']).') ' : '';
0 ignored issues
show
Bug introduced by
It seems like columnize() 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

39
            $columns = $expression['columns'] ? '('.$this->/** @scrutinizer ignore-call */ columnize($expression['columns']).') ' : '';
Loading history...
40
41 160
            $materialized = !is_null($expression['materialized'])
42 22
                ? ($expression['materialized'] ? 'materialized ' : 'not materialized ')
43 138
                : '';
44
45 160
            $cycle = $this->compileCycle($query, $expression);
46
47 160
            $statements[] = $this->wrapTable($expression['name']).' '.$columns.'as '.$materialized.'('.$expression['query'].")$cycle";
0 ignored issues
show
Bug introduced by
It seems like wrapTable() 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

47
            $statements[] = $this->/** @scrutinizer ignore-call */ wrapTable($expression['name']).' '.$columns.'as '.$materialized.'('.$expression['query'].")$cycle";
Loading history...
48
        }
49
50 160
        return 'with '.$recursive.implode(', ', $statements);
51
    }
52
53
    /**
54
     * Get the "recursive" keyword.
55
     *
56
     * @param array $expressions
57
     * @return string
58
     */
59 124
    protected function recursiveKeyword(array $expressions)
60
    {
61 124
        return collect($expressions)->where('recursive', true)->isNotEmpty() ? 'recursive ' : '';
0 ignored issues
show
Bug introduced by
$expressions of type array is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

61
        return collect(/** @scrutinizer ignore-type */ $expressions)->where('recursive', true)->isNotEmpty() ? 'recursive ' : '';
Loading history...
62
    }
63
64
    /**
65
     * Compile the recursion limit.
66
     *
67
     * @param \Illuminate\Database\Query\Builder $query
68
     * @param int|null $recursionLimit
69
     * @return string
70
     */
71 21
    public function compileRecursionLimit(Builder $query, $recursionLimit)
0 ignored issues
show
Unused Code introduced by
The parameter $query is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

71
    public function compileRecursionLimit(/** @scrutinizer ignore-unused */ Builder $query, $recursionLimit)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
72
    {
73 21
        if (is_null($recursionLimit)) {
74 6
            return '';
75
        }
76
77 15
        return 'option (maxrecursion '.(int) $recursionLimit.')';
78
    }
79
80
    /**
81
     * Compile the cycle detection.
82
     *
83
     * @param \Illuminate\Database\Query\Builder $query
84
     * @param array $expression
85
     * @return string
86
     */
87 120
    public function compileCycle(Builder $query, array $expression)
0 ignored issues
show
Unused Code introduced by
The parameter $query is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

87
    public function compileCycle(/** @scrutinizer ignore-unused */ Builder $query, array $expression)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
88
    {
89 120
        if (!$expression['cycle']) {
90 118
            return '';
91
        }
92
93 2
        $columns = $this->columnize($expression['cycle']['columns']);
94 2
        $markColumn = $this->wrap($expression['cycle']['markColumn']);
0 ignored issues
show
Bug introduced by
It seems like wrap() 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

94
        /** @scrutinizer ignore-call */ 
95
        $markColumn = $this->wrap($expression['cycle']['markColumn']);
Loading history...
95 2
        $pathColumn = $this->wrap($expression['cycle']['pathColumn']);
96
97 2
        return " cycle $columns set $markColumn using $pathColumn";
98
    }
99
100
    /**
101
     * Compile a select query into SQL.
102
     *
103
     * @param \Illuminate\Database\Query\Builder $query
104
     * @return string
105
     */
106 175
    public function compileSelect(Builder $query)
107
    {
108 175
        $sql = parent::compileSelect($query);
109
110 175
        if ($query instanceof CteBuilder) {
111 175
            if ($query->unionExpressions) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $query->unionExpressions of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
112 15
                $sql = $this->compileExpressions($query, $query->unionExpressions) . " $sql";
113
            }
114
115 175
            if (!is_null($query->unionRecursionLimit)) {
0 ignored issues
show
introduced by
The condition is_null($query->unionRecursionLimit) is always false.
Loading history...
116 5
                $sql .= ' ' . $this->compileRecursionLimit($query, $query->unionRecursionLimit);
117
            }
118
        }
119
120 175
        return $sql;
121
    }
122
123
    /**
124
     * Compile an insert statement using a subquery into SQL.
125
     *
126
     * @param \Illuminate\Database\Query\Builder $query
127
     * @param array $columns
128
     * @param string $sql
129
     * @return string
130
     */
131 11
    public function compileInsertUsing(Builder $query, array $columns, string $sql)
132
    {
133 11
        $expressions = $this->compileExpressions($query, $query->expressions);
0 ignored issues
show
Bug introduced by
The property expressions does not seem to exist on Illuminate\Database\Query\Builder.
Loading history...
134
135 11
        $recursionLimit = $this->compileRecursionLimit($query, $query->recursionLimit);
0 ignored issues
show
Bug introduced by
The property recursionLimit does not seem to exist on Illuminate\Database\Query\Builder.
Loading history...
136
137 11
        $compiled = parent::compileInsertUsing($query, $columns, $sql);
138
139 11
        return (string) Str::of($compiled)
140 11
            ->prepend($expressions, ' ')
141 11
            ->append(' ', $recursionLimit)
142 11
            ->trim();
143
    }
144
145
    /**
146
     * Compile an update statement into SQL.
147
     *
148
     * @param \Illuminate\Database\Query\Builder $query
149
     * @param array $values
150
     * @return string
151
     */
152 16
    public function compileUpdate(Builder $query, array $values)
153
    {
154 16
        $compiled = parent::compileUpdate($query, $values);
155
156 16
        return (string) Str::of($compiled)
157 16
            ->prepend($this->compileExpressions($query, $query->expressions), ' ')
0 ignored issues
show
Bug introduced by
The property expressions does not seem to exist on Illuminate\Database\Query\Builder.
Loading history...
158 16
            ->trim();
159
    }
160
161
    /**
162
     * Prepare the bindings for an update statement.
163
     *
164
     * @param array $bindings
165
     * @param array $values
166
     * @return array
167
     */
168 16
    public function prepareBindingsForUpdate(array $bindings, array $values)
169
    {
170 16
        $values = array_merge($bindings['expressions'], $values);
171
172 16
        unset($bindings['expressions']);
173
174 16
        return parent::prepareBindingsForUpdate($bindings, $values);
175
    }
176
177
    /**
178
     * Get the bindings for an update statement.
179
     *
180
     * @param \Illuminate\Database\Query\Builder $query
181
     * @param array $bindings
182
     * @param array $values
183
     * @return array
184
     */
185 11
    public function getBindingsForUpdate(Builder $query, array $bindings, array $values)
0 ignored issues
show
Unused Code introduced by
The parameter $query is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

185
    public function getBindingsForUpdate(/** @scrutinizer ignore-unused */ Builder $query, array $bindings, array $values)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
186
    {
187 11
        return $this->prepareBindingsForUpdate($bindings, $values);
188
    }
189
190
    /**
191
     * Compile a delete statement into SQL.
192
     *
193
     * @param \Illuminate\Database\Query\Builder $query
194
     * @return string
195
     */
196 14
    public function compileDelete(Builder $query)
197
    {
198 14
        $compiled = parent::compileDelete($query);
199
200 14
        return (string) Str::of($compiled)
201 14
            ->prepend($this->compileExpressions($query, $query->expressions), ' ')
0 ignored issues
show
Bug introduced by
The property expressions does not seem to exist on Illuminate\Database\Query\Builder.
Loading history...
202 14
            ->trim();
203
    }
204
}
205