Passed
Pull Request — master (#399)
by Def
02:38
created

TableSchema::getFullName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Schema;
6
7
use Yiisoft\Db\Exception\NotSupportedException;
8
9
use function array_keys;
10
11
/**
12
 * TableSchema represents the metadata of a database table.
13
 *
14
 * @property array $columnNames List of column names. This property is read-only.
15
 */
16
abstract class TableSchema implements TableSchemaInterface
17
{
18
    private string|null $schemaName = null;
19
    private string $name = '';
20
    private string|null $fullName = null;
21
    private string|null $comment = null;
22
    private string|null $sequenceName = null;
23
    /** @psalm-var string[] */
24
    private array $primaryKey = [];
25
    /** @psalm-var ColumnSchemaInterface[] */
26
    private array $columns = [];
27
    /** @psalm-var array<array-key, array> */
28
    protected array $foreignKeys = [];
29
    protected string|null $createSql = null;
30
    private string|null $catalogName = null;
31
    private string|null $serverName = null;
32
33
    /**
34 297
     * Gets the named column metadata.
35
     *
36 297
     * This is a convenient method for retrieving a named column even if it does not exist.
37
     *
38
     * @param string $name column name
39
     *
40
     * @return ColumnSchemaInterface|null metadata of the named column. Null if the named column does not exist.
41
     */
42
    public function getColumn(string $name): ColumnSchemaInterface|null
43
    {
44 140
        return $this->columns[$name] ?? null;
45
    }
46 140
47
    /**
48
     * Returns the names of all columns in this table.
49
     *
50
     * @return array list of column names
51
     */
52
    public function getColumnNames(): array
53
    {
54
        return array_keys($this->columns);
55
    }
56
57
    /**
58
     * @return string|null the name of the schema that this table belongs to.
59
     */
60
    public function getSchemaName(): string|null
61
    {
62
        return $this->schemaName;
63
    }
64
65
    /**
66
     * @return string the name of this table. The schema name is not included. Use {@see fullName} to get the name with
67
     * schema name prefix.
68
     */
69
    public function getName(): string
70
    {
71
        return $this->name;
72
    }
73
74
    /**
75
     * @return string|null the full name of this table, which includes the schema name prefix, if any. Note that if the
76
     * schema name is the same as the {@see Schema::defaultSchema|default schema name}, the schema name will not be
77 1197
     * included.
78
     */
79 1197
    public function getFullName(): string|null
80
    {
81
        return $this->fullName;
82
    }
83
84
    /**
85
     * @return string|null sequence name for the primary key. Null if no sequence.
86 1511
     */
87
    public function getSequenceName(): string|null
88 1511
    {
89
        return $this->sequenceName;
90
    }
91
92
    /**
93
     * @return array primary keys of this table.
94
     *
95
     * @psalm-return string[]
96 354
     */
97
    public function getPrimaryKey(): array
98 354
    {
99
        return $this->primaryKey;
100
    }
101
102
    /**
103
     * @return array column metadata of this table. Each array element is a {@see ColumnSchemaInterface} object, indexed by
104 489
     * column names.
105
     *
106 489
     * @psalm-return ColumnSchemaInterface[]
107
     */
108
    public function getColumns(): array
109
    {
110
        return $this->columns;
111
    }
112 741
113
    public function getComment(): string|null
114 741
    {
115
        return $this->comment;
116
    }
117
118
    public function schemaName(string|null $value): void
119
    {
120
        $this->schemaName = $value;
121 926
    }
122
123 926
    public function name(string $value): void
124
    {
125
        $this->name = $value;
126 1017
    }
127
128 1017
    public function fullName(string|null $value): void
129 1017
    {
130
        $this->fullName = $value;
131 1514
    }
132
133 1514
    public function comment(string|null $value): void
134 1514
    {
135
        $this->comment = $value;
136 1514
    }
137
138 1514
    public function sequenceName(string|null $value): void
139 1514
    {
140
        $this->sequenceName = $value;
141 899
    }
142
143 899
    public function primaryKey(string $value): void
144 899
    {
145
        $this->primaryKey[] = $value;
146 1130
    }
147
148 1130
    public function columns(string $index, ColumnSchemaInterface $value): void
149 1130
    {
150
        $this->columns[$index] = $value;
151 1279
    }
152
153 1279
    public function getCatalogName(): string|null
154 1279
    {
155
        return $this->catalogName;
156
    }
157
158
    /**
159
     * @param string|null name of the catalog (database) that this table belongs to. Defaults to null, meaning no
0 ignored issues
show
Bug introduced by
The type Yiisoft\Db\Schema\name was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
160
     * catalog (or the current database).
161
     */
162
    public function catalogName(string|null $value): void
163
    {
164
        $this->catalogName = $value;
165
    }
166
167
    public function getServerName(): string|null
168
    {
169
        return $this->serverName;
170
    }
171
172
    /**
173
     * @param string|null name of the server
174
     */
175
    public function serverName(string|null $value): void
176
    {
177
        $this->serverName = $value;
178
    }
179
180
    public function getCreateSql(): string|null
181
    {
182
        return $this->createSql;
183
    }
184
185
    public function createSql(string $sql): void
186
    {
187
        $this->createSql = $sql;
188
    }
189
190
    /**
191
     * ```php
192
     * [
193
     *  'ForeignTableName',
194
     *  'fk1' => 'pk1',  // pk1 is in foreign table
195
     *  'fk2' => 'pk2',  // if composite foreign key
196
     * ]
197
     * ```
198
     *
199
     * @return array foreign keys of this table. Each array element is of the following structure:
200
     * @psalm-return array<array-key, array>
201
     */
202
    public function getForeignKeys(): array
203
    {
204
        return $this->foreignKeys;
205
    }
206
207
    /**
208
     * @psalm-param array<array-key, array> $value
209
     */
210
    public function foreignKeys(array $value): void
211
    {
212
        $this->foreignKeys = $value;
213
    }
214
215
    public function foreignKey(string|int $id, array $to): void
216
    {
217
        $this->foreignKeys[$id] = $to;
218
    }
219
220
    public function compositeFK(int $id, string $from, string $to): void
221
    {
222
        throw new NotSupportedException(static::class . ' does not support composite FK.');
223
    }
224
}
225