Completed
Push — 3.x ( bfc500...4ede47 )
by Hari
9s
created

Update   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 162
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 2
dl 0
loc 162
ccs 31
cts 31
cp 1
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A table() 0 5 1
A build() 0 11 1
A buildTable() 0 4 1
A cols() 0 4 1
A set() 0 4 1
A buildValuesForUpdate() 0 8 2
A col() 0 4 1
A where() 0 5 1
A orWhere() 0 5 1
1
<?php
2
/**
3
 *
4
 * This file is part of Aura for PHP.
5
 *
6
 * @license http://opensource.org/licenses/bsd-license.php BSD
7
 *
8
 */
9
namespace Aura\SqlQuery\Common;
10
11
use Aura\SqlQuery\AbstractDmlQuery;
12
13
/**
14
 *
15
 * An object for UPDATE queries.
16
 *
17
 * @package Aura.SqlQuery
18
 *
19
 */
20
class Update extends AbstractDmlQuery implements UpdateInterface
21
{
22
    /**
23
     *
24
     * The table to update.
25
     *
26
     * @var string
27
     *
28
     */
29
    protected $table;
30
31
    /**
32
     *
33
     * Sets the table to update.
34
     *
35
     * @param string $table The table to update.
36
     *
37
     * @return $this
38
     *
39
     */
40 18
    public function table($table)
41
    {
42 18
        $this->table = $this->quoter->quoteName($table);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->quoter->quoteName($table) can also be of type array. However, the property $table is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

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

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
43 18
        return $this;
44
    }
45
46
    /**
47
     *
48
     * Builds this query object into a string.
49
     *
50
     * @return string
51
     *
52
     */
53 16
    protected function build()
54
    {
55
        return 'UPDATE'
56 16
            . $this->buildFlags()
57 16
            . $this->buildTable()
58 16
            . $this->buildValuesForUpdate()
59 16
            . $this->buildWhere()
60 16
            . $this->buildOrderBy()
61 16
            . $this->buildLimit()
62 16
            . $this->buildReturning();
63
    }
64
65
    /**
66
     *
67
     * Builds the table clause.
68
     *
69
     * @return null
70
     *
71
     */
72 16
    protected function buildTable()
73
    {
74 16
        return " {$this->table}";
75
    }
76
77
    /**
78
     *
79
     * Adds a WHERE condition to the query by AND. If the condition has
80
     * ?-placeholders, additional arguments to the method will be bound to
81
     * those placeholders sequentially.
82
     *
83
     * @param string $cond The WHERE condition.
84
     * @param mixed ...$bind arguments to bind to placeholders
85
     *
86
     * @return $this
87
     *
88
     */
89 15
    public function where($cond, ...$bind)
90
    {
91 15
        $this->addWhere('AND', $cond, ...$bind);
92 15
        return $this;
93
    }
94
95
    /**
96
     *
97
     * Adds a WHERE condition to the query by OR. If the condition has
98
     * ?-placeholders, additional arguments to the method will be bound to
99
     * those placeholders sequentially.
100
     *
101
     * @param string $cond The WHERE condition.
102
     * @param mixed ...$bind arguments to bind to placeholders
103
     *
104
     * @return $this
105
     *
106
     * @see where()
107
     *
108
     */
109 14
    public function orWhere($cond, ...$bind)
110
    {
111 14
        $this->addWhere('OR', $cond, ...$bind);
112 14
        return $this;
113
    }
114
115
    /**
116
     *
117
     * Sets one column value placeholder; if an optional second parameter is
118
     * passed, that value is bound to the placeholder.
119
     *
120
     * @param string $col The column name.
121
     *
122
     * @param array $value
123
     *
124
     * @return $this
125
     */
126 6
    public function col($col, ...$value)
127
    {
128 6
        return $this->addCol($col, ...$value);
129
    }
130
131
    /**
132
     *
133
     * Sets multiple column value placeholders. If an element is a key-value
134
     * pair, the key is treated as the column name and the value is bound to
135
     * that column.
136
     *
137
     * @param array $cols A list of column names, optionally as key-value
138
     *                    pairs where the key is a column name and the value is a bind value for
139
     *                    that column.
140
     *
141
     * @return $this
142
     *
143
     */
144 15
    public function cols(array $cols)
145
    {
146 15
        return $this->addCols($cols);
147
    }
148
149
    /**
150
     *
151
     * Sets a column value directly; the value will not be escaped, although
152
     * fully-qualified identifiers in the value will be quoted.
153
     *
154
     * @param string $col   The column name.
155
     *
156
     * @param string $value The column value expression.
157
     *
158
     * @return $this
159
     *
160
     */
161 14
    public function set($col, $value)
162
    {
163 14
        return $this->setCol($col, $value);
164
    }
165
166
    /**
167
     *
168
     * Builds the updated columns and values of the statement.
169
     *
170
     * @return string
171
     *
172
     */
173 16
    protected function buildValuesForUpdate()
174
    {
175 16
        $values = array();
176 16
        foreach ($this->col_values as $col => $value) {
177 16
            $values[] = "{$col} = {$value}";
178 16
        }
179 16
        return PHP_EOL . 'SET' . $this->indentCsv($values);
180
    }
181
}
182