Completed
Push — master ( 07905b...790544 )
by yuuki
10s
created

Builder   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 115
Duplicated Lines 29.57 %

Coupling/Cohesion

Components 3
Dependencies 3

Test Coverage

Coverage 84.85%

Importance

Changes 0
Metric Value
dl 34
loc 115
ccs 28
cts 33
cp 0.8485
rs 10
c 0
b 0
f 0
wmc 13
lcom 3
cbo 3

5 Methods

Rating   Name   Duplication   Size   Complexity  
A key() 0 6 1
A returning() 0 6 1
A insert() 17 17 4
A upsert() 17 17 4
A detectValues() 0 13 3

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
/**
16
 * Class Builder
17
 */
18
class Builder extends \Illuminate\Database\Query\Builder
19
{
20
    /**
21
     * The database connection instance.
22
     *
23
     * @var \Ytake\LaravelCouchbase\Database\CouchbaseConnection
24
     */
25
    protected $connection;
26
27
    /**
28
     * The database query grammar instance.
29
     *
30
     * @var \Ytake\LaravelCouchbase\Query\Grammar
31
     */
32
    protected $grammar;
33
34
    /** @var string  use-keys-clause */
35
    public $key;
36
37
    /** @var string[]  returning-clause */
38
    public $returning = ['*'];
39
40
    /**
41
     * @param $key
42
     *
43
     * @return $this
44
     */
45 7
    public function key($key)
46
    {
47 7
        $this->key = $key;
48
49 7
        return $this;
50
    }
51
52
    /**
53
     * @param array $column
54
     *
55
     * @return $this
56
     */
57 3
    public function returning(array $column = ['*'])
58
    {
59 3
        $this->returning = $column;
60
61 3
        return $this;
62
    }
63
64
    /**
65
     * Insert a new record into the database.
66
     *
67
     * @param array $values
68
     *
69
     * @return bool
70
     */
71 5 View Code Duplication
    public function insert(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...
72
    {
73 5
        if (empty($values)) {
74
            return true;
75
        }
76 5
        $values = $this->detectValues($values);
77 5
        $bindings = [];
78 5
        foreach ($values as $record) {
79 5
            foreach ($record as $key => $value) {
80 5
                $bindings[$key] = $value;
81
            }
82
        }
83
84 5
        $sql = $this->grammar->compileInsert($this, $values);
85
86 5
        return $this->connection->insert($sql, $bindings);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->connection...nsert($sql, $bindings); (integer) is incompatible with the return type of the parent method Illuminate\Database\Query\Builder::insert of type boolean.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
87
    }
88
89
    /**
90
     * supported N1QL upsert query.
91
     *
92
     * @param array $values
93
     *
94
     * @return bool|mixed
95
     */
96 1 View Code Duplication
    public function upsert(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...
97
    {
98 1
        if (empty($values)) {
99
            return true;
100
        }
101 1
        $values = $this->detectValues($values);
102 1
        $bindings = [];
103 1
        foreach ($values as $record) {
104 1
            foreach ($record as $key => $value) {
105 1
                $bindings[$key] = $value;
106
            }
107
        }
108
109 1
        $sql = $this->grammar->compileUpsert($this, $values);
110
111 1
        return $this->connection->upsert($sql, $bindings);
112
    }
113
114
    /**
115
     * @param string|int|array $values
116
     *
117
     * @return array
118
     */
119 6
    protected function detectValues($values)
120
    {
121 6
        if (!is_array(reset($values))) {
122 6
            $values = [$values];
123
        } else {
124
            foreach ($values as $key => $value) {
125
                ksort($value);
126
                $values[$key] = $value;
127
            }
128
        }
129
130 6
        return $values;
131
    }
132
}
133