Completed
Pull Request — 3.x (#116)
by
unknown
01:52
created

Update::where()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A Update::col() 0 4 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
    use WhereTrait;
23
24
    /**
25
     *
26
     * The table to update.
27
     *
28
     * @var string
29
     *
30
     */
31
    protected $table;
32
33
    /**
34
     *
35
     * Sets the table to update.
36
     *
37
     * @param string $table The table to update.
38
     *
39
     * @return $this
40
     *
41
     */
42 18
    public function table($table)
43
    {
44 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...
45 18
        return $this;
46
    }
47
48
    /**
49
     *
50
     * Builds this query object into a string.
51
     *
52
     * @return string
53
     *
54
     */
55 16
    protected function build()
56
    {
57
        return 'UPDATE'
58 16
            . $this->buildFlags()
59 16
            . $this->buildTable()
60 16
            . $this->buildValuesForUpdate()
61 16
            . $this->buildWhere()
62 16
            . $this->buildOrderBy()
63 16
            . $this->buildLimit()
64 16
            . $this->buildReturning();
65
    }
66
67
    /**
68
     *
69
     * Builds the table clause.
70
     *
71
     * @return null
72
     *
73
     */
74 16
    protected function buildTable()
75
    {
76 16
        return " {$this->table}";
77
    }
78
79
    /**
80
     *
81
     * Sets one column value placeholder; if an optional second parameter is
82
     * passed, that value is bound to the placeholder.
83
     *
84
     * @param string $col The column name.
85
     *
86
     * @param array $value
87
     *
88
     * @return $this
89
     */
90 6
    public function col($col, ...$value)
91
    {
92 6
        return $this->addCol($col, ...$value);
93
    }
94
95
    /**
96
     *
97
     * Sets multiple column value placeholders. If an element is a key-value
98
     * pair, the key is treated as the column name and the value is bound to
99
     * that column.
100
     *
101
     * @param array $cols A list of column names, optionally as key-value
102
     *                    pairs where the key is a column name and the value is a bind value for
103
     *                    that column.
104
     *
105
     * @return $this
106
     *
107
     */
108 15
    public function cols(array $cols)
109
    {
110 15
        return $this->addCols($cols);
111
    }
112
113
    /**
114
     *
115
     * Sets a column value directly; the value will not be escaped, although
116
     * fully-qualified identifiers in the value will be quoted.
117
     *
118
     * @param string $col   The column name.
119
     *
120
     * @param string $value The column value expression.
121
     *
122
     * @return $this
123
     *
124
     */
125 14
    public function set($col, $value)
126
    {
127 14
        return $this->setCol($col, $value);
128
    }
129
130
    /**
131
     *
132
     * Builds the updated columns and values of the statement.
133
     *
134
     * @return string
135
     *
136
     */
137 16
    protected function buildValuesForUpdate()
138
    {
139 16
        $values = array();
140 16
        foreach ($this->col_values as $col => $value) {
141 16
            $values[] = "{$col} = {$value}";
142 16
        }
143 16
        return PHP_EOL . 'SET' . $this->indentCsv($values);
144
    }
145
}
146