Passed
Push — master ( 4eca28...80e796 )
by Jonas
04:48
created

CompilesExpressions::compileRecursionLimit()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
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 102
    public function __construct()
14
    {
15 102
        array_unshift($this->selectComponents, 'expressions');
16
17 102
        $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 102
    }
19
20
    /**
21
     * Compile the common table expressions.
22
     *
23
     * @param \Illuminate\Database\Query\Builder $query
24
     * @param array $expressions
25
     * @return string
26
     */
27 102
    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
    {
29 102
        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 98
            return '';
31
        }
32
33 90
        $recursive = $this->recursiveKeyword($expressions);
34
35 90
        $statements = [];
36
37 90
        foreach ($expressions as $expression) {
38 90
            $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
40 90
            $materialized = !is_null($expression['materialized'])
41 18
                ? ($expression['materialized'] ? 'materialized ' : 'not materialized ')
42 72
                : '';
43
44 90
            $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
47 90
        return 'with '.$recursive.implode(', ', $statements);
48
    }
49
50
    /**
51
     * Get the "recursive" keyword.
52
     *
53
     * @param array $expressions
54
     * @return string
55
     */
56 66
    protected function recursiveKeyword(array $expressions)
57
    {
58 66
        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
     * @return string
67
     */
68 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

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
    {
70 15
        if (is_null($recursionLimit)) {
71 3
            return '';
72
        }
73
74 12
        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 102
    public function compileSelect(Builder $query)
84
    {
85 102
        $sql = parent::compileSelect($query);
86
87 102
        if ($query->unionExpressions) {
88 8
            $sql = $this->compileExpressions($query, $query->unionExpressions) . " $sql";
89
        }
90
91 102
        if (!is_null($query->unionRecursionLimit)) {
92 4
            $sql .= ' ' . $this->compileRecursionLimit($query, $query->unionRecursionLimit);
93
        }
94
95 102
        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
     * @return string
105
     */
106 7
    public function compileInsertUsing(Builder $query, array $columns, string $sql)
107
    {
108 7
        $expressions = $this->compileExpressions($query, $query->expressions);
109
110 7
        $recursionLimit = $this->compileRecursionLimit($query, $query->recursionLimit);
111
112 7
        $compiled = parent::compileInsertUsing($query, $columns, $sql);
113
114 7
        return (string) Str::of($compiled)
115 7
            ->prepend($expressions, ' ')
116 7
            ->append(' ', $recursionLimit)
117 7
            ->trim();
118
    }
119
120
    /**
121
     * Compile an update statement into SQL.
122
     *
123
     * @param \Illuminate\Database\Query\Builder $query
124
     * @param array $values
125
     * @return string
126
     */
127 4
    public function compileUpdate(Builder $query, array $values)
128
    {
129 4
        $compiled = parent::compileUpdate($query, $values);
130
131 4
        return (string) Str::of($compiled)
132 4
            ->prepend($this->compileExpressions($query, $query->expressions), ' ')
133 4
            ->trim();
134
    }
135
136
    /**
137
     * Prepare the bindings for an update statement.
138
     *
139
     * @param array $bindings
140
     * @param array $values
141
     * @return array
142
     */
143 4
    public function prepareBindingsForUpdate(array $bindings, array $values)
144
    {
145 4
        $values = array_merge($bindings['expressions'], $values);
146
147 4
        unset($bindings['expressions']);
148
149 4
        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 4
    public function compileDelete(Builder $query)
159
    {
160 4
        $compiled = parent::compileDelete($query);
161
162 4
        return (string) Str::of($compiled)
163 4
            ->prepend($this->compileExpressions($query, $query->expressions), ' ')
164 4
            ->trim();
165
    }
166
}
167