Completed
Pull Request — master (#24)
by yuuki
03:05
created

Grammar   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 136
Duplicated Lines 32.35 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 92.59%

Importance

Changes 6
Bugs 0 Features 3
Metric Value
c 6
b 0
f 3
dl 44
loc 136
ccs 50
cts 54
cp 0.9259
rs 10
wmc 20
lcom 1
cbo 3

6 Methods

Rating   Name   Duplication   Size   Complexity  
A wrapValue() 0 8 2
A wrapKey() 0 8 2
B compileUpdate() 0 25 3
B compileInsert() 22 22 5
A compileDelete() 0 16 3
B compileUpsert() 22 22 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
5
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
6
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
7
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
8
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
9
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
10
 * THE SOFTWARE.
11
 */
12
13
namespace Ytake\LaravelCouchbase\Query;
14
15
use Illuminate\Database\Query\Builder;
16
use Illuminate\Database\Query\Grammars\Grammar as IlluminateGrammar;
17
use Ytake\LaravelCouchbase\Database\QueryBuilder;
18
19
/**
20
 * Class Grammar.
21
 *
22
 * @author Yuuki Takezawa<[email protected]>
23
 */
24
class Grammar extends IlluminateGrammar
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29 7
    protected function wrapValue($value)
30
    {
31 7
        if ($value === '*') {
32 1
            return $value;
33
        }
34
35 7
        return $value;
36
    }
37
38
    /**
39
     * @param $value
40
     *
41
     * @return string
42
     */
43 6
    protected function wrapKey($value)
44
    {
45 6
        if (is_null($value)) {
46
            return;
47
        }
48
49 6
        return '"' . str_replace('"', '""', $value) . '"';
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     *
55
     * notice: supported set query only
56
     */
57 1
    public function compileUpdate(Builder $query, $values)
58
    {
59
        // keyspace-ref:
60 1
        $table = $this->wrapTable($query->from);
61
        // use-keys-clause:
62 1
        $keyClause = $this->wrapKey($query->key);
0 ignored issues
show
Bug introduced by
The property key does not seem to exist in Illuminate\Database\Query\Builder.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
63
        // returning-clause
64 1
        $returning = implode(', ', $query->returning);
0 ignored issues
show
Bug introduced by
The property returning does not seem to exist in Illuminate\Database\Query\Builder.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
65
66 1
        $columns = [];
67
68 1
        foreach ($values as $key => $value) {
69 1
            $columns[] = $this->wrap($key) . ' = ' . $this->parameter($value);
70
        }
71
72 1
        $columns = implode(', ', $columns);
73
74 1
        $joins = '';
75 1
        if (isset($query->joins)) {
76
            $joins = ' ' . $this->compileJoins($query, $query->joins);
77
        }
78 1
        $where = $this->compileWheres($query);
79
80 1
        return trim("update {$table} USE KEYS {$keyClause} {$joins} set $columns $where RETURNING {$returning}");
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86 4 View Code Duplication
    public function compileInsert(Builder $query, array $values)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
87
    {
88
        // keyspace-ref:
89 4
        $table = $this->wrapTable($query->from);
90
        // use-keys-clause:
91 4
        $keyClause = $this->wrapKey($query->key);
0 ignored issues
show
Bug introduced by
The property key does not seem to exist in Illuminate\Database\Query\Builder.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
92
        // returning-clause
93 4
        $returning = implode(', ', $query->returning);
0 ignored issues
show
Bug introduced by
The property returning does not seem to exist in Illuminate\Database\Query\Builder.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
94
95 4
        if (!is_array(reset($values))) {
96
            $values = [$values];
97
        }
98 4
        $parameters = [];
99
100 4
        foreach ($values as $record) {
101 4
            $parameters[] = '(' . $this->parameterize($record) . ')';
102
        }
103 4
        $parameters = (!$keyClause) ? implode(', ', $parameters) : "({$keyClause}, \$parameters)";
104 4
        $keyValue = (!$keyClause) ? null : '(KEY, VALUE)';
105
106 4
        return "insert into {$table} {$keyValue} values $parameters RETURNING {$returning}";
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     *
112
     * @see http://developer.couchbase.com/documentation/server/4.1/n1ql/n1ql-language-reference/delete.html
113
     */
114 7
    public function compileDelete(Builder $query)
115
    {
116
        // keyspace-ref:
117 7
        $table = $this->wrapTable($query->from);
118
        // use-keys-clause:
119 7
        $keyClause = null;
120 7
        if ($query->key) {
121 6
            $key = $this->wrapKey($query->key);
0 ignored issues
show
Bug introduced by
The property key does not seem to exist in Illuminate\Database\Query\Builder.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
122 6
            $keyClause = "USE KEYS {$key}";
123
        }
124
        // returning-clause
125 7
        $returning = implode(', ', $query->returning);
0 ignored issues
show
Bug introduced by
The property returning does not seem to exist in Illuminate\Database\Query\Builder.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
126 7
        $where = is_array($query->wheres) ? $this->compileWheres($query) : '';
127
128 7
        return trim("delete from {$table} {$keyClause} {$where} RETURNING {$returning}");
129
    }
130
131
    /**
132
     * @param QueryBuilder $query
133
     * @param array        $values
134
     *
135
     * @return string
136
     */
137 1 View Code Duplication
    public function compileUpsert(QueryBuilder $query, array $values)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
138
    {
139
        // keyspace-ref:
140 1
        $table = $this->wrapTable($query->from);
141
        // use-keys-clause:
142 1
        $keyClause = $this->wrapKey($query->key);
143
        // returning-clause
144 1
        $returning = implode(', ', $query->returning);
145
146 1
        if (!is_array(reset($values))) {
147
            $values = [$values];
148
        }
149 1
        $parameters = [];
150
151 1
        foreach ($values as $record) {
152 1
            $parameters[] = '(' . $this->parameterize($record) . ')';
153
        }
154 1
        $parameters = (!$keyClause) ? implode(', ', $parameters) : "({$keyClause}, \$parameters)";
155 1
        $keyValue = (!$keyClause) ? null : '(KEY, VALUE)';
156
157 1
        return "UPSERT INTO {$table} {$keyValue} VALUES $parameters RETURNING {$returning}";
158
    }
159
}
160