Passed
Push — master ( 89b134...c252a6 )
by Jonas
10:11
created

CompilesExpressions::prepareBindingsForUpdate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Staudenmeir\LaravelCte\Query\Grammars;
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 116
    public function __construct()
15
    {
16 116
        array_unshift($this->selectComponents, 'expressions');
17
18 116
        $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 116
    }
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 116
    public function compileExpressions(Builder $query, array $expressions)
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

28
    public function compileExpressions(/** @scrutinizer ignore-unused */ Builder $query, array $expressions)

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...
29
    {
30 116
        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 112
            return '';
32
        }
33
34 104
        $recursive = $this->recursiveKeyword($expressions);
35
36 104
        $statements = [];
37
38 104
        foreach ($expressions as $expression) {
39 104
            $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 104
            $materialized = !is_null($expression['materialized'])
42 18
                ? ($expression['materialized'] ? 'materialized ' : 'not materialized ')
43 86
                : '';
44
45 104
            $statements[] = $this->wrapTable($expression['name']).' '.$columns.'as '.$materialized.'('.$expression['query'].')';
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

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

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

69
    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...
70
    {
71 15
        if (is_null($recursionLimit)) {
72 3
            return '';
73
        }
74
75 12
        return 'option (maxrecursion '.(int) $recursionLimit.')';
76
    }
77
78
    /**
79
     * Compile a select query into SQL.
80
     *
81
     * @param \Illuminate\Database\Query\Builder $query
82
     * @return string
83
     */
84 116
    public function compileSelect(Builder $query)
85
    {
86 116
        $sql = parent::compileSelect($query);
87
88 116
        if ($query instanceof CteBuilder) {
89 116
            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...
90 8
                $sql = $this->compileExpressions($query, $query->unionExpressions) . " $sql";
91
            }
92
93 116
            if (!is_null($query->unionRecursionLimit)) {
0 ignored issues
show
introduced by
The condition is_null($query->unionRecursionLimit) is always false.
Loading history...
94 4
                $sql .= ' ' . $this->compileRecursionLimit($query, $query->unionRecursionLimit);
95
            }
96
        }
97
98 116
        return $sql;
99
    }
100
101
    /**
102
     * Compile an insert statement using a subquery into SQL.
103
     *
104
     * @param \Illuminate\Database\Query\Builder $query
105
     * @param array $columns
106
     * @param string $sql
107
     * @return string
108
     */
109 7
    public function compileInsertUsing(Builder $query, array $columns, string $sql)
110
    {
111 7
        $expressions = $this->compileExpressions($query, $query->expressions);
112
113 7
        $recursionLimit = $this->compileRecursionLimit($query, $query->recursionLimit);
114
115 7
        $compiled = parent::compileInsertUsing($query, $columns, $sql);
116
117 7
        return (string) Str::of($compiled)
118 7
            ->prepend($expressions, ' ')
119 7
            ->append(' ', $recursionLimit)
120 7
            ->trim();
121
    }
122
123
    /**
124
     * Compile an update statement into SQL.
125
     *
126
     * @param \Illuminate\Database\Query\Builder $query
127
     * @param array $values
128
     * @return string
129
     */
130 7
    public function compileUpdate(Builder $query, array $values)
131
    {
132 7
        $compiled = parent::compileUpdate($query, $values);
133
134 7
        return (string) Str::of($compiled)
135 7
            ->prepend($this->compileExpressions($query, $query->expressions), ' ')
136 7
            ->trim();
137
    }
138
139
    /**
140
     * Prepare the bindings for an update statement.
141
     *
142
     * @param array $bindings
143
     * @param array $values
144
     * @return array
145
     */
146 7
    public function prepareBindingsForUpdate(array $bindings, array $values)
147
    {
148 7
        $values = array_merge($bindings['expressions'], $values);
149
150 7
        unset($bindings['expressions']);
151
152 7
        return parent::prepareBindingsForUpdate($bindings, $values);
153
    }
154
155
    /**
156
     * Get the bindings for an update statement.
157
     *
158
     * @param \Illuminate\Database\Query\Builder $query
159
     * @param array $bindings
160
     * @param array $values
161
     * @return array
162
     */
163 5
    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

163
    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...
164
    {
165 5
        return $this->prepareBindingsForUpdate($bindings, $values);
166
    }
167
168
    /**
169
     * Compile a delete statement into SQL.
170
     *
171
     * @param \Illuminate\Database\Query\Builder $query
172
     * @return string
173
     */
174 7
    public function compileDelete(Builder $query)
175
    {
176 7
        $compiled = parent::compileDelete($query);
177
178 7
        return (string) Str::of($compiled)
179 7
            ->prepend($this->compileExpressions($query, $query->expressions), ' ')
180 7
            ->trim();
181
    }
182
}
183