Passed
Push — master ( af4849...ba71ff )
by y
01:17
created

Table::setName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Helix\DB;
4
5
use Helix\DB;
6
7
/**
8
 * Table manipulation using arrays.
9
 *
10
 * Accessing the table as an array produces {@link Column} instances.
11
 *
12
 * @immutable
13
 */
14
class Table extends AbstractTable {
15
16
    /**
17
     * `[name => Column]`
18
     *
19
     * @var Column[]
20
     */
21
    protected $columns = [];
22
23
    /**
24
     * @var string
25
     */
26
    protected $name;
27
28
    /**
29
     * @param DB $db
30
     * @param string $name
31
     * @param string[] $columns
32
     */
33
    public function __construct (DB $db, $name, array $columns) {
34
        parent::__construct($db);
35
        $this->name = $name;
36
        foreach ($columns as $column) {
37
            $this->columns[$column] = new Column($db, $column, $this);
38
        }
39
    }
40
41
    /**
42
     * Returns the table name.
43
     *
44
     * @return string
45
     */
46
    final public function __toString (): string {
47
        return $this->name;
48
    }
49
50
    /**
51
     * Executes an `INSERT IGNORE` using arbitrary columns.
52
     *
53
     * @param array $values
54
     * @return int Rows affected.
55
     */
56
    public function apply (array $values): int {
57
        $columns = implode(',', array_keys($values));
58
        $values = implode(',', $this->db->quote($values));
0 ignored issues
show
Bug introduced by
It seems like $this->db->quote($values) can also be of type integer and string; however, parameter $pieces of implode() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

58
        $values = implode(',', /** @scrutinizer ignore-type */ $this->db->quote($values));
Loading history...
59
        switch ($this->db->getDriver()) {
60
            case 'sqlite':
61
                return $this->db->exec("INSERT OR IGNORE INTO {$this} ($columns) VALUES ($values)");
62
            default:
63
                return $this->db->exec("INSERT IGNORE INTO {$this} ($columns) VALUES ($values)");
64
        }
65
    }
66
67
    /**
68
     * Executes a deletion using arbitrary columns.
69
     *
70
     * @see DB::match()
71
     *
72
     * @param array $match
73
     * @return int Rows affected.
74
     */
75
    public function delete (array $match): int {
76
        $match = SQL::all($this->db->match($match));
0 ignored issues
show
Bug introduced by
It seems like $this->db->match($match) can also be of type string; however, parameter $conditions of Helix\DB\SQL::all() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

76
        $match = SQL::all(/** @scrutinizer ignore-type */ $this->db->match($match));
Loading history...
77
        return $this->db->exec("DELETE FROM {$this} WHERE {$match}");
78
    }
79
80
    /**
81
     * @return Column[]
82
     */
83
    final public function getColumns (): array {
84
        return $this->columns;
85
    }
86
87
    /**
88
     * @return string
89
     */
90
    final public function getName (): string {
91
        return $this->name;
92
    }
93
94
    /**
95
     * Executes an insertion using arbitrary columns.
96
     *
97
     * @param array $values
98
     * @return int Insertion ID.
99
     */
100
    public function insert (array $values): int {
101
        $columns = implode(',', array_keys($values));
102
        $values = implode(',', $this->db->quote($values));
0 ignored issues
show
Bug introduced by
It seems like $this->db->quote($values) can also be of type integer and string; however, parameter $pieces of implode() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

102
        $values = implode(',', /** @scrutinizer ignore-type */ $this->db->quote($values));
Loading history...
103
        $this->db->exec("INSERT INTO {$this} ($columns) VALUES ($values)");
104
        return $this->db->lastInsertId();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->db->lastInsertId() returns the type string which is incompatible with the type-hinted return integer.
Loading history...
105
    }
106
107
    /**
108
     * @param string $name
109
     * @return bool
110
     */
111
    public function offsetExists ($name): bool {
112
        return isset($this->columns[$name]);
113
    }
114
115
    /**
116
     * @param string $name
117
     * @return Column
118
     */
119
    public function offsetGet ($name): Column {
120
        return $this->columns[$name];
121
    }
122
123
    /**
124
     * Returns a selection object for columns in the table.
125
     *
126
     * @param string[] $columns Defaults to all columns.
127
     * @return Select
128
     */
129
    public function select (array $columns = []): Select {
130
        if (!$columns) {
131
            $columns = $this->columns;
132
        }
133
        return new Select($this->db, $this, $columns);
134
    }
135
136
    /**
137
     * Returns a clone with a different name. Columns are also re-qualified.
138
     *
139
     * @param string $name
140
     * @return Table
141
     */
142
    public function setName (string $name) {
143
        $clone = clone $this;
144
        $clone->name = $name;
145
        foreach ($this->columns as $name => $column) {
146
            $clone->columns[$name] = $column->setQualifier($clone);
147
        }
148
        return $clone;
149
    }
150
151
    /**
152
     * Executes an update using arbitrary columns.
153
     *
154
     * @see DB::match()
155
     *
156
     * @param array $values
157
     * @param array $match
158
     * @return int Rows affected.
159
     */
160
    public function update (array $values, array $match): int {
161
        $values = implode(', ', SQL::isEqual($this->db->quote($values)));
0 ignored issues
show
Bug introduced by
It seems like Helix\DB\SQL::isEqual($this->db->quote($values)) can also be of type string; however, parameter $pieces of implode() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

161
        $values = implode(', ', /** @scrutinizer ignore-type */ SQL::isEqual($this->db->quote($values)));
Loading history...
162
        $match = SQL::all($this->db->match($match));
0 ignored issues
show
Bug introduced by
It seems like $this->db->match($match) can also be of type string; however, parameter $conditions of Helix\DB\SQL::all() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

162
        $match = SQL::all(/** @scrutinizer ignore-type */ $this->db->match($match));
Loading history...
163
        return $this->db->exec("UPDATE {$this} SET {$values} WHERE {$match}");
164
    }
165
}