Passed
Push — master ( edb396...4eca28 )
by Jonas
10:14
created

CompilesExpressions::prepareBindingsForUpdate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 7
ccs 0
cts 0
cp 0
crap 2
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
8
trait CompilesExpressions
9
{
10
    /**
11
     * Create a new grammar instance.
12
     */
13 94
    public function __construct()
14
    {
15 94
        array_unshift($this->selectComponents, 'expressions');
16
17 94
        $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...
18 94
    }
19
20
    /**
21
     * Compile the common table expressions.
22
     *
23
     * @param \Illuminate\Database\Query\Builder $query
24
     * @param array $expressions
25
     * @return string
26 94
     */
27
    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

27
    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...
28 94
    {
29 90
        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...
30
            return '';
31
        }
32 82
33
        $recursive = $this->recursiveKeyword($expressions);
34 82
35
        $statements = [];
36 82
37 82
        foreach ($expressions as $expression) {
38
            $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

38
            $columns = $expression['columns'] ? '('.$this->/** @scrutinizer ignore-call */ columnize($expression['columns']).') ' : '';
Loading history...
39 82
40 18
            $materialized = !is_null($expression['materialized'])
41 64
                ? ($expression['materialized'] ? 'materialized ' : 'not materialized ')
42
                : '';
43 82
44
            $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

44
            $statements[] = $this->/** @scrutinizer ignore-call */ wrapTable($expression['name']).' '.$columns.'as '.$materialized.'('.$expression['query'].')';
Loading history...
45
        }
46 82
47
        return 'with '.$recursive.implode(', ', $statements);
48
    }
49
50
    /**
51
     * Get the "recursive" keyword.
52
     *
53
     * @param array $expressions
54
     * @return string
55 63
     */
56
    protected function recursiveKeyword(array $expressions)
57 63
    {
58
        return collect($expressions)->where('recursive', true)->isNotEmpty() ? 'recursive ' : '';
59
    }
60
61
    /**
62
     * Compile the recursion limit.
63
     *
64
     * @param \Illuminate\Database\Query\Builder $query
65
     * @param int|null $recursionLimit
66 11
     * @return string
67
     */
68 11
    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

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