Completed
Push — master ( d685b1...8967da )
by Jared
02:15
created

ValuesStatement   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 11.76 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 6
c 4
b 0
f 0
lcom 1
cbo 1
dl 6
loc 51
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A addValues() 0 6 1
A getInsertValues() 0 4 1
A build() 6 21 4

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
 * @author Jared King <[email protected]>
5
 *
6
 * @link http://jaredtking.com
7
 *
8
 * @copyright 2015 Jared King
9
 * @license MIT
10
 */
11
namespace JAQB\Statement;
12
13
class ValuesStatement extends Statement
14
{
15
    /**
16
     * @var array
17
     */
18
    protected $insertValues = [];
19
20
    /**
21
     * Adds values to the statement.
22
     *
23
     * @return self
24
     */
25
    public function addValues(array $values)
26
    {
27
        $this->insertValues = array_replace($this->insertValues, $values);
28
29
        return $this;
30
    }
31
32
    /**
33
     * Gets the values being inserted.
34
     *
35
     * @return array
36
     */
37
    public function getInsertValues()
38
    {
39
        return $this->insertValues;
40
    }
41
42
    public function build()
43
    {
44
        // reset the parameterized values
45
        $this->values = [];
46
47
        $fields = [];
48 View Code Duplication
        foreach ($this->insertValues as $key => $value) {
49
            if ($id = $this->escapeIdentifier($key)) {
50
                $fields[] = $id;
51
                $this->values[] = $value;
52
            }
53
        }
54
55
        if (count($fields) == 0) {
56
            return '';
57
        }
58
59
        // generates (`col1`,`col2`,`col3`) VALUES (?,?,?)
0 ignored issues
show
Unused Code Comprehensibility introduced by
66% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
60
        return '('.implode(', ', $fields).') VALUES ('.
61
            implode(', ', array_fill(0, count($fields), '?')).')';
62
    }
63
}
64