Passed
Pull Request — master (#48)
by
unknown
14:13
created

CompilesExpressions::compileCycle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.7462

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 11
ccs 3
cts 7
cp 0.4286
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2.7462
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
use Staudenmeir\LaravelCte\Query\SingleStoreBuilder;
9
10
trait CompilesExpressions
11
{
12
    /**
13
     * Create a new grammar instance.
14
     */
15 169
    public function __construct()
16
    {
17 169
        array_unshift($this->selectComponents, 'expressions');
18
19 169
        $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...
20
    }
21
22
    /**
23
     * Compile the common table expressions.
24
     *
25
     * @param \Illuminate\Database\Query\Builder $query
26
     * @param array $expressions
27
     * @return string
28
     */
29 169
    public function compileExpressions(Builder $query, array $expressions)
30
    {
31 169
        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...
32 163
            return '';
33
        }
34
35 154
        $recursive = $this->recursiveKeyword($expressions);
36
37 154
        $statements = [];
38
39 154
        foreach ($expressions as $expression) {
40 154
            $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

40
            $columns = $expression['columns'] ? '('.$this->/** @scrutinizer ignore-call */ columnize($expression['columns']).') ' : '';
Loading history...
41
42 154
            $materialized = !is_null($expression['materialized'])
43 20
                ? ($expression['materialized'] ? 'materialized ' : 'not materialized ')
44 134
                : '';
45
46 154
            $cycle = $this->compileCycle($query, $expression);
47
48 154
            $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

48
            $statements[] = $this->/** @scrutinizer ignore-call */ wrapTable($expression['name']).' '.$columns.'as '.$materialized.'('.$expression['query'].")$cycle";
Loading history...
49
        }
50
51 154
        return 'with '.$recursive.implode(', ', $statements);
52
    }
53
54
    /**
55
     * Get the "recursive" keyword.
56
     *
57
     * @param array $expressions
58
     * @return string
59
     */
60 118
    protected function recursiveKeyword(array $expressions)
61
    {
62 118
        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

62
        return collect(/** @scrutinizer ignore-type */ $expressions)->where('recursive', true)->isNotEmpty() ? 'recursive ' : '';
Loading history...
63
    }
64
65
    /**
66
     * Compile the recursion limit.
67
     *
68
     * @param \Illuminate\Database\Query\Builder $query
69
     * @param int|null $recursionLimit
70
     * @return string
71
     */
72 19
    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

72
    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...
73
    {
74 19
        if (is_null($recursionLimit)) {
75 4
            return '';
76
        }
77
78 15
        return 'option (maxrecursion '.(int) $recursionLimit.')';
79
    }
80
81
    /**
82
     * Compile the cycle detection.
83
     *
84
     * @param \Illuminate\Database\Query\Builder $query
85
     * @param array $expression
86
     * @return string
87
     */
88 96
    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

88
    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...
89
    {
90 96
        if (!$expression['cycle']) {
91 96
            return '';
92
        }
93
94
        $columns = $this->columnize($expression['cycle']['columns']);
95
        $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

95
        /** @scrutinizer ignore-call */ 
96
        $markColumn = $this->wrap($expression['cycle']['markColumn']);
Loading history...
96
        $pathColumn = $this->wrap($expression['cycle']['pathColumn']);
97
98
        return " cycle $columns set $markColumn using $pathColumn";
99
    }
100
101
    /**
102
     * Compile a select query into SQL.
103
     *
104
     * @param \Illuminate\Database\Query\Builder $query
105
     * @return string
106
     */
107 169
    public function compileSelect(Builder $query)
108
    {
109 169
        $sql = parent::compileSelect($query);
110 169
        if ($query instanceof CteBuilder or $query instanceof SingleStoreBuilder) {
111 169
            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 169
            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 169
        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 9
    public function compileInsertUsing(Builder $query, array $columns, string $sql)
132
    {
133 9
        $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 9
        $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 9
        $compiled = parent::compileInsertUsing($query, $columns, $sql);
138
139 9
        return (string) Str::of($compiled)
140 9
            ->prepend($expressions, ' ')
141 9
            ->append(' ', $recursionLimit)
142 9
            ->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 17
    public function compileUpdate(Builder $query, array $values)
153
    {
154 17
        $compiled = parent::compileUpdate($query, $values);
155
156 17
        return (string) Str::of($compiled)
157 17
            ->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 17
            ->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 17
    public function prepareBindingsForUpdate(array $bindings, array $values)
169
    {
170 17
        $values = array_merge($bindings['expressions'], $values);
171
172 17
        unset($bindings['expressions']);
173
174 17
        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 15
    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 15
        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 18
    public function compileDelete(Builder $query)
197
    {
198 18
        $compiled = parent::compileDelete($query);
199
200 18
        return (string) Str::of($compiled)
201 18
            ->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 18
            ->trim();
203
    }
204
}
205