Completed
Push — master ( 0d9cbc...8d7e77 )
by yuuki
02:23
created

Grammar   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 132
Duplicated Lines 33.33 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 87.72%

Importance

Changes 5
Bugs 0 Features 3
Metric Value
wmc 19
c 5
b 0
f 3
lcom 1
cbo 3
dl 44
loc 132
ccs 50
cts 57
cp 0.8772
rs 10

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 12 2
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 5
    protected function wrapValue($value)
30
    {
31 5
        if ($value === '*') {
32 2
            return $value;
33
        }
34
35 5
        return $value;
36
    }
37
38
    /**
39
     * @param $value
40
     *
41
     * @return string
42
     */
43 5
    protected function wrapKey($value)
44
    {
45 5
        if (is_null($value)) {
46
            return;
47
        }
48
49 5
        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 1
        }
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 4
        }
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 5
    public function compileDelete(Builder $query)
115
    {
116
        // keyspace-ref:
117 5
        $table = $this->wrapTable($query->from);
118
        // use-keys-clause:
119 5
        $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...
120
        // returning-clause
121 5
        $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...
122 5
        $where = is_array($query->wheres) ? $this->compileWheres($query) : '';
123
124 5
        return trim("delete from {$table} USE KEYS {$keyClause} {$where} RETURNING {$returning}");
125
    }
126
127
    /**
128
     * @param QueryBuilder $query
129
     * @param array        $values
130
     *
131
     * @return string
132
     */
133 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...
134
    {
135
        // keyspace-ref:
136 1
        $table = $this->wrapTable($query->from);
137
        // use-keys-clause:
138 1
        $keyClause = $this->wrapKey($query->key);
139
        // returning-clause
140 1
        $returning = implode(', ', $query->returning);
141
142 1
        if (!is_array(reset($values))) {
143
            $values = [$values];
144
        }
145 1
        $parameters = [];
146
147 1
        foreach ($values as $record) {
148 1
            $parameters[] = '('.$this->parameterize($record).')';
149 1
        }
150 1
        $parameters = (!$keyClause) ? implode(', ', $parameters) : "({$keyClause}, \$parameters)";
151 1
        $keyValue = (!$keyClause) ? null : '(KEY, VALUE)';
152
153 1
        return "UPSERT INTO {$table} {$keyValue} VALUES $parameters RETURNING {$returning}";
154
    }
155
}
156