Passed
Pull Request — master (#347)
by Def
13:54 queued 02:09
created

Schema::getSchemaPrimaryKeys()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Schema;
6
7
use PDO;
8
use Throwable;
9
use Yiisoft\Cache\Dependency\TagDependency;
10
use Yiisoft\Db\Cache\SchemaCache;
11
use Yiisoft\Db\Connection\ConnectionInterface;
12
use Yiisoft\Db\Constraint\Constraint;
13
use Yiisoft\Db\Exception\NotSupportedException;
14
15
use function gettype;
16
use function is_array;
17
use function preg_match;
18
19
abstract class Schema implements SchemaInterface
20
{
21
    public const SCHEMA = 'schema';
22
    public const PRIMARY_KEY = 'primaryKey';
23
    public const INDEXES = 'indexes';
24
    public const CHECKS = 'checks';
25
    public const FOREIGN_KEYS = 'foreignKeys';
26
    public const DEFAULT_VALUES = 'defaultValues';
27
    public const UNIQUES = 'uniques';
28
29
    public const TYPE_PK = 'pk';
30
    public const TYPE_UPK = 'upk';
31
    public const TYPE_BIGPK = 'bigpk';
32
    public const TYPE_UBIGPK = 'ubigpk';
33
    public const TYPE_CHAR = 'char';
34
    public const TYPE_STRING = 'string';
35
    public const TYPE_TEXT = 'text';
36
    public const TYPE_TINYINT = 'tinyint';
37
    public const TYPE_SMALLINT = 'smallint';
38
    public const TYPE_INTEGER = 'integer';
39
    public const TYPE_BIGINT = 'bigint';
40
    public const TYPE_FLOAT = 'float';
41
    public const TYPE_DOUBLE = 'double';
42
    public const TYPE_DECIMAL = 'decimal';
43
    public const TYPE_DATETIME = 'datetime';
44
    public const TYPE_TIMESTAMP = 'timestamp';
45
    public const TYPE_TIME = 'time';
46
    public const TYPE_DATE = 'date';
47
    public const TYPE_BINARY = 'binary';
48
    public const TYPE_BOOLEAN = 'boolean';
49
    public const TYPE_MONEY = 'money';
50
    public const TYPE_JSON = 'json';
51
52
    public const PHP_TYPE_INTEGER = 'integer';
53
    public const PHP_TYPE_STRING = 'string';
54
    public const PHP_TYPE_BOOLEAN = 'boolean';
55
    public const PHP_TYPE_DOUBLE = 'double';
56
    public const PHP_TYPE_RESOURCE = 'resource';
57
    public const PHP_TYPE_ARRAY = 'array';
58
    public const PHP_TYPE_NULL = 'NULL';
59
60
    public const CACHE_VERSION = 'cacheVersion';
61
62
    /**
63
     * Schema cache version, to detect incompatibilities in cached values when the data format of the cache changes.
64
     */
65
    protected const SCHEMA_CACHE_VERSION = 1;
66
67
    /**
68
     * @var string|null the default schema name used for the current session.
69
     */
70
    protected ?string $defaultSchema = null;
71
    private array $schemaNames = [];
72
    private array $tableNames = [];
73
    protected array $viewNames = [];
74
    private array $tableMetadata = [];
75
76
    public function __construct(protected ConnectionInterface $db, private SchemaCache $schemaCache)
77
    {
78
    }
79
80
    /**
81
     * Returns the cache key for the specified table name.
82
     *
83
     * @param string $name the table name.
84
     *
85
     * @return array the cache key.
86
     */
87
    abstract protected function getCacheKey(string $name): array;
88
89
    /**
90
     * Returns the cache tag name.
91
     *
92
     * This allows {@see refresh()} to invalidate all cached table schemas.
93
     *
94
     * @return string the cache tag name.
95
     */
96
    abstract protected function getCacheTag(): string;
97
98
    /**
99
     * Loads all check constraints for the given table.
100
     *
101
     * @param TableNameInterface $tableName table name.
102
     *
103
     * @return array check constraints for the given table.
104
     */
105
    abstract protected function loadTableChecks(TableNameInterface $tableName): array;
106
107
    /**
108
     * Loads all default value constraints for the given table.
109
     *
110 2532
     * @param TableNameInterface $tableName table name.
111
     *
112 2532
     * @return array default value constraints for the given table.
113 2532
     */
114 2532
    abstract protected function loadTableDefaultValues(TableNameInterface $tableName): array;
115
116
    /**
117
     * Loads all foreign keys for the given table.
118
     *
119
     * @param TableNameInterface $tableName table name.
120
     *
121 963
     * @return array foreign keys for the given table.
122
     */
123 963
    abstract protected function loadTableForeignKeys(TableNameInterface $tableName): array;
124 963
125
    /**
126
     * Loads all indexes for the given table.
127 963
     *
128
     * @param TableNameInterface $tableName table name.
129
     *
130 1665
     * @return array indexes for the given table.
131
     */
132 1665
    abstract protected function loadTableIndexes(TableNameInterface $tableName): array;
133
134
    /**
135
     * Loads a primary key for the given table.
136
     *
137
     * @param TableNameInterface $tableName table name.
138
     *
139
     * @return Constraint|null primary key for the given table, `null` if the table has no primary key.
140 18
     */
141
    abstract protected function loadTablePrimaryKey(TableNameInterface $tableName): ?Constraint;
142 18
143
    /**
144
     * Loads all unique constraints for the given table.
145
     *
146
     * @param TableNameInterface $tableName table name.
147
     *
148 1322
     * @return array unique constraints for the given table.
149
     */
150 1322
    abstract protected function loadTableUniques(TableNameInterface $tableName): array;
151
152
    /**
153
     * Loads the metadata for the specified table.
154
     *
155
     * @param TableNameInterface $name table name.
156 13
     *
157
     * @return TableSchemaInterface|null DBMS-dependent table metadata, `null` if the table does not exist.
158 13
     */
159
    abstract protected function loadTableSchema(TableNameInterface $name): ?TableSchemaInterface;
160
161
    public function getDefaultSchema(): ?string
162
    {
163
        return $this->defaultSchema;
164 4
    }
165
166 4
    public function getPdoType(mixed $data): int
167 4
    {
168
        /** @psalm-var array<string, int> */
169
        $typeMap = [
170 4
            // php type => PDO type
171
            self::PHP_TYPE_BOOLEAN => PDO::PARAM_BOOL,
172
            self::PHP_TYPE_INTEGER => PDO::PARAM_INT,
173
            self::PHP_TYPE_STRING => PDO::PARAM_STR,
174
            self::PHP_TYPE_RESOURCE => PDO::PARAM_LOB,
175
            self::PHP_TYPE_NULL => PDO::PARAM_NULL,
176 23
        ];
177
178 23
        $type = gettype($data);
179 23
180
        return $typeMap[$type] ?? PDO::PARAM_STR;
181
    }
182 23
183
    public function getSchemaCache(): SchemaCache
184
    {
185
        return $this->schemaCache;
186
    }
187
188 1360
    /**
189
     * @throws NotSupportedException
190 1360
     */
191
    public function getSchemaChecks(string $schema = '', bool $refresh = false): array
192
    {
193
        return $this->getSchemaMetadata($schema, self::CHECKS, $refresh);
194
    }
195
196
    /**
197
     * @throws NotSupportedException
198
     */
199 1360
    public function getSchemaDefaultValues(string $schema = '', bool $refresh = false): array
200
    {
201 1360
        return $this->getSchemaMetadata($schema, self::DEFAULT_VALUES, $refresh);
202
    }
203
204
    /**
205
     * @throws NotSupportedException
206
     */
207 96
    public function getSchemaForeignKeys(string $schema = '', bool $refresh = false): array
208
    {
209 96
        return $this->getSchemaMetadata($schema, self::FOREIGN_KEYS, $refresh);
210 96
    }
211
212
    /**
213 96
     * @throws NotSupportedException
214 96
     */
215 96
    public function getSchemaIndexes(string $schema = '', bool $refresh = false): array
216
    {
217
        return $this->getSchemaMetadata($schema, self::INDEXES, $refresh);
218
    }
219
220 103
    public function getSchemaNames(bool $refresh = false): array
221
    {
222 103
        if (empty($this->schemaNames) || $refresh) {
223
            $this->schemaNames = $this->findSchemaNames();
224 103
        }
225
226 103
        return $this->schemaNames;
227
    }
228 103
229 103
    /**
230
     * @throws NotSupportedException
231 103
     */
232
    public function getSchemaPrimaryKeys(string $schema = '', bool $refresh = false): array
233
    {
234
        return $this->getSchemaMetadata($schema, self::PRIMARY_KEY, $refresh);
235
    }
236 36
237
    /**
238 36
     * @throws NotSupportedException
239 36
     */
240 36
    public function getSchemaUniques(string $schema = '', bool $refresh = false): array
241
    {
242
        return $this->getSchemaMetadata($schema, self::UNIQUES, $refresh);
243
    }
244
245
    public function getTableChecks(string|TableNameInterface $name, bool $refresh = false): array
246
    {
247
        /** @var mixed */
248
        $tableChecks = $this->getTableMetadata($name, self::CHECKS, $refresh);
249
        return is_array($tableChecks) ? $tableChecks : [];
250 10
    }
251
252 10
    public function getTableDefaultValues(string|TableNameInterface $name, bool $refresh = false): array
253
    {
254
        /** @var mixed */
255
        $tableDefaultValues = $this->getTableMetadata($name, self::DEFAULT_VALUES, $refresh);
256
        return is_array($tableDefaultValues) ? $tableDefaultValues : [];
257
    }
258 4
259
    public function getTableForeignKeys(string|TableNameInterface $name, bool $refresh = false): array
260 4
    {
261 4
        /** @var mixed */
262
        $tableForeignKeys = $this->getTableMetadata($name, self::FOREIGN_KEYS, $refresh);
263
        return is_array($tableForeignKeys) ? $tableForeignKeys : [];
264
    }
265
266
    public function getTableIndexes(string|TableNameInterface $name, bool $refresh = false): array
267
    {
268
        /** @var mixed */
269
        $tableIndexes = $this->getTableMetadata($name, self::INDEXES, $refresh);
270
        return is_array($tableIndexes) ? $tableIndexes : [];
271
    }
272
273
    public function getTableNames(string $schema = '', bool $refresh = false): array
274 4
    {
275
        if (!isset($this->tableNames[$schema]) || $refresh) {
276 4
            /** @psalm-var string[] */
277 4
            $this->tableNames[$schema] = $this->findTableNames($schema);
278
        }
279
280
        return is_array($this->tableNames[$schema]) ? $this->tableNames[$schema] : [];
281
    }
282 8
283
    public function getTablePrimaryKey(string|TableNameInterface $name, bool $refresh = false): ?Constraint
284 8
    {
285 8
        /** @var mixed */
286
        $tablePrimaryKey = $this->getTableMetadata($name, self::PRIMARY_KEY, $refresh);
287
        return $tablePrimaryKey instanceof Constraint ? $tablePrimaryKey : null;
288
    }
289
290 28
    public function getTableSchema(string|TableNameInterface $name, bool $refresh = false): ?TableSchemaInterface
291
    {
292 28
        /** @var mixed */
293
        $tableSchema = $this->getTableMetadata($name, self::SCHEMA, $refresh);
294 28
        return $tableSchema instanceof TableSchemaInterface ? $tableSchema : null;
295
    }
296
297
    public function getTableSchemas(string $schema = '', bool $refresh = false): array
298 28
    {
299 28
        /** @var mixed */
300
        $tableSchemas = $this->getSchemaMetadata($schema, self::SCHEMA, $refresh);
301 28
        return is_array($tableSchemas) ? $tableSchemas : [];
0 ignored issues
show
introduced by
The condition is_array($tableSchemas) is always true.
Loading history...
302 26
    }
303 24
304 24
    public function getTableUniques(string|TableNameInterface $name, bool $refresh = false): array
305
    {
306
        /** @var mixed */
307 4
        $tableUniques = $this->getTableMetadata($name, self::UNIQUES, $refresh);
308
        return is_array($tableUniques) ? $tableUniques : [];
309
    }
310 28
311
    /**
312
     * Returns a value indicating whether a SQL statement is for read purpose.
313
     *
314
     * @param string $sql the SQL statement.
315
     *
316 1075
     * @return bool whether a SQL statement is for read purpose.
317
     */
318 1075
    public function isReadQuery(string $sql): bool
319 6
    {
320
        $pattern = '/^\s*(SELECT|SHOW|DESCRIBE)\b/i';
321
322 1075
        return preg_match($pattern, $sql) > 0;
323 1075
    }
324
325
    /**
326
     * Refreshes the schema.
327
     *
328
     * This method cleans up all cached table schemas so that they can be re-created later to reflect the database
329
     * schema change.
330
     */
331
    public function refresh(): void
332
    {
333 1574
        if ($this->schemaCache->isEnabled()) {
334
            $this->schemaCache->invalidate($this->getCacheTag());
335 1574
        }
336 4
337
        $this->tableNames = [];
338
        $this->tableMetadata = [];
339 1574
    }
340 146
341
    public function refreshTableSchema(string|TableNameInterface $name): void
342
    {
343 1550
        $name = $this->toTableNameInterface($name);
344 1467
        $rawName = (string) $name;
345
346
        unset($this->tableMetadata[$rawName]);
347 287
348
        $this->tableNames = [];
349 287
350 287
        if ($this->schemaCache->isEnabled()) {
351
            $this->schemaCache->remove($this->getCacheKey($rawName));
352
        }
353 287
    }
354
355
    /**
356
     * Returns all schema names in the database, including the default one but not system schemas.
357
     *
358
     * This method should be overridden by child classes in order to support this feature because the default
359 1698
     * implementation simply throws an exception.
360
     *
361 1698
     * @throws NotSupportedException if this method is not supported by the DBMS.
362 149
     *
363
     * @return array all schema names in the database, except system schemas.
364
     */
365 1683
    protected function findSchemaNames(): array
366 232
    {
367 232
        throw new NotSupportedException(static::class . ' does not support fetching all schema names.');
368
    }
369 1673
370
    /**
371
     * Returns all table names in the database.
372 1683
     *
373 4
     * This method should be overridden by child classes in order to support this feature because the default
374
     * implementation simply throws an exception.
375
     *
376 1683
     * @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema.
377
     *
378
     * @throws NotSupportedException if this method is not supported by the DBMS.
379
     *
380
     * @return array all table names in the database. The names have NO schema name prefix.
381
     */
382 1344
    protected function findTableNames(string $schema): array
0 ignored issues
show
Unused Code introduced by
The parameter $schema is not used and could be removed. ( Ignorable by Annotation )

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

382
    protected function findTableNames(/** @scrutinizer ignore-unused */ string $schema): array

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
383
    {
384 1344
        throw new NotSupportedException(static::class . ' does not support fetching all table names.');
385 987
    }
386
387 357
    /**
388
     * Extracts the PHP type from abstract DB type.
389
     *
390 1344
     * @param ColumnSchemaInterface $column the column schema information.
391
     *
392
     * @return string PHP type name.
393
     */
394
    protected function getColumnPhpType(ColumnSchemaInterface $column): string
395
    {
396 1683
        /** @psalm-var string[] */
397
        $typeMap = [
398 1683
            // abstract type => php type
399 1357
            self::TYPE_TINYINT => self::PHP_TYPE_INTEGER,
400
            self::TYPE_SMALLINT => self::PHP_TYPE_INTEGER,
401 326
            self::TYPE_INTEGER => self::PHP_TYPE_INTEGER,
402
            self::TYPE_BIGINT => self::PHP_TYPE_INTEGER,
403
            self::TYPE_BOOLEAN => self::PHP_TYPE_BOOLEAN,
404 1683
            self::TYPE_FLOAT => self::PHP_TYPE_DOUBLE,
405 1683
            self::TYPE_DOUBLE => self::PHP_TYPE_DOUBLE,
406
            self::TYPE_BINARY => self::PHP_TYPE_RESOURCE,
407
            self::TYPE_JSON => self::PHP_TYPE_ARRAY,
408
        ];
409
410
        if (isset($typeMap[$column->getType()])) {
411 5
            if ($column->getType() === self::TYPE_BIGINT) {
412
                return PHP_INT_SIZE === 8 && !$column->isUnsigned() ? self::PHP_TYPE_INTEGER : self::PHP_TYPE_STRING;
413 5
            }
414 5
415
            if ($column->getType() === self::TYPE_INTEGER) {
416
                return PHP_INT_SIZE === 4 && $column->isUnsigned() ? self::PHP_TYPE_STRING : self::PHP_TYPE_INTEGER;
417
            }
418
419 5
            return $typeMap[$column->getType()];
420
        }
421
422
        return self::PHP_TYPE_STRING;
423
    }
424
425
    /**
426
     * Returns the metadata of the given type for all tables in the given schema.
427
     *
428
     * @param string $schema the schema of the metadata. Defaults to empty string, meaning the current or default schema
429
     * name.
430
     * @param string $type metadata type.
431
     * @param bool $refresh whether to fetch the latest available table metadata. If this is `false`, cached data may be
432
     * returned if available.
433
     *
434
     * @throws NotSupportedException
435
     *
436
     * @return array of metadata.
437
     *
438
     * @psalm-return list<Constraint|TableSchemaInterface|array>
439 1622
     */
440
    protected function getSchemaMetadata(string $schema, string $type, bool $refresh): array
441 1622
    {
442 130
        $metadata = [];
443
        /** @psalm-var string[] */
444 130
        $tableNames = $this->getTableNames($schema, $refresh);
445
446
        foreach ($tableNames as $name) {
447 1622
            if ($schema !== '') {
448
                $name = $schema . '.' . $name;
449
            }
450
451
            $name = $this->toTableNameInterface($name);
452
            $tableMetadata = $this->getTableTypeMetadata($type, $name, $refresh);
453 50
454
            if ($tableMetadata !== null) {
455 50
                $metadata[] = $tableMetadata;
456
            }
457
        }
458
459 50
        return $metadata;
460
    }
461 50
462 50
    /**
463 13
     * Returns the metadata of the given type for the given table.
464
     *
465
     * @param string $name table name. The table name may contain schema name if any. Do not quote the table name.
466
     * @param string $type metadata type.
467 50
     * @param bool $refresh whether to reload the table metadata even if it is found in the cache.
468 50
     *
469
     * @return mixed metadata.
470 50
     *
471
     * @psalm-suppress MixedArrayAccess
472
     * @psalm-suppress MixedArrayAssignment
473
     */
474
    protected function getTableMetadata(string|TableNameInterface $name, string $type, bool $refresh = false): mixed
475
    {
476 12
        $name = $this->toTableNameInterface($name);
477
        $rawName = (string) $name;
478 12
479
        if (!isset($this->tableMetadata[$rawName])) {
480 12
            $this->loadTableMetadataFromCache($rawName);
481
        }
482
483
        if ($refresh || !isset($this->tableMetadata[$rawName][$type])) {
484
            $this->tableMetadata[$rawName][$type] = $this->loadTableTypeMetadata($type, $name);
485
            $this->saveTableMetadataToCache($rawName);
486 375
        }
487
488 375
        return $this->tableMetadata[$rawName][$type];
489 375
    }
490
491
    /**
492 375
     * This method returns the desired metadata type for the table name.
493
     *
494
     * @param string $type
495
     * @param string|TableNameInterface $name
496
     *
497
     * @return array|Constraint|TableSchemaInterface|null
498 153
     */
499
    protected function loadTableTypeMetadata(string $type, string|TableNameInterface $name): Constraint|array|TableSchemaInterface|null
500 153
    {
501
        $name = $this->toTableNameInterface($name);
502
503
        return match ($type) {
504
            self::SCHEMA => $this->loadTableSchema($name),
505
            self::PRIMARY_KEY => $this->loadTablePrimaryKey($name),
506
            self::UNIQUES => $this->loadTableUniques($name),
507
            self::FOREIGN_KEYS => $this->loadTableForeignKeys($name),
508
            self::INDEXES => $this->loadTableIndexes($name),
509
            self::DEFAULT_VALUES => $this->loadTableDefaultValues($name),
510
            self::CHECKS => $this->loadTableChecks($name),
511
            default => null,
512
        };
513
    }
514 20
515
    /**
516 20
     * This method returns the desired metadata type for table name (with refresh if needed)
517
     *
518
     * @param string $type
519
     * @param TableNameInterface $name
520
     * @param bool $refresh
521
     *
522
     * @return array|Constraint|TableSchemaInterface|null
523
     */
524
    protected function getTableTypeMetadata(
525
        string $type,
526
        TableNameInterface $name,
527
        bool $refresh = false
528
    ): Constraint|array|null|TableSchemaInterface {
529
        return match ($type) {
530 141
            self::SCHEMA => $this->getTableSchema($name, $refresh),
531
            self::PRIMARY_KEY => $this->getTablePrimaryKey($name, $refresh),
532 141
            self::UNIQUES => $this->getTableUniques($name, $refresh),
533
            self::FOREIGN_KEYS => $this->getTableForeignKeys($name, $refresh),
534
            self::INDEXES => $this->getTableIndexes($name, $refresh),
535
            self::DEFAULT_VALUES => $this->getTableDefaultValues($name, $refresh),
536
            self::CHECKS => $this->getTableChecks($name, $refresh),
537
            default => null,
538
        };
539
    }
540
541
    /**
542
     * Resolves the table name and schema name (if any).
543
     *
544
     * @param TableNameInterface $name the table name.
545
     *
546 156
     * @throws NotSupportedException if this method is not supported by the DBMS.
547
     *
548 156
     * {@see \Yiisoft\Db\Schema\TableSchemaInterface}
549
     *
550
     * @return TableSchemaInterface
551
     */
552
    protected function resolveTableName(TableNameInterface $name): TableSchemaInterface
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed. ( Ignorable by Annotation )

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

552
    protected function resolveTableName(/** @scrutinizer ignore-unused */ TableNameInterface $name): TableSchemaInterface

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
553
    {
554
        throw new NotSupportedException(static::class . ' does not support resolving table names.');
555
    }
556
557
    /**
558
     * Sets the metadata of the given type for the given table.
559
     *
560
     * @param TableNameInterface $name table name.
561
     * @param string $type metadata type.
562 63
     * @param mixed $data metadata.
563
     *
564 63
     * @psalm-suppress MixedArrayAssignment
565
     */
566
    protected function setTableMetadata(TableNameInterface $name, string $type, mixed $data): void
567
    {
568
        $this->tableMetadata[(string) $name][$type] = $data;
569
    }
570
571
    /**
572
     * Tries to load and populate table metadata from cache.
573
     *
574
     * @param string $rawName
575
     */
576
    private function loadTableMetadataFromCache(string $rawName): void
577
    {
578 61
        if (!$this->schemaCache->isEnabled() || $this->schemaCache->isExcluded($rawName)) {
579
            $this->tableMetadata[$rawName] = [];
580 61
            return;
581
        }
582
583
        $metadata = $this->schemaCache->getOrSet(
584
            $this->getCacheKey($rawName),
585
            null,
586
            $this->schemaCache->getDuration(),
587
            new TagDependency($this->getCacheTag()),
588
        );
589
590
        if (
591
            !is_array($metadata) ||
592
            !isset($metadata[self::CACHE_VERSION]) ||
593
            $metadata[self::CACHE_VERSION] !== static::SCHEMA_CACHE_VERSION
594
        ) {
595
            $this->tableMetadata[$rawName] = [];
596
            return;
597
        }
598
599
        unset($metadata[self::CACHE_VERSION]);
600
        $this->tableMetadata[$rawName] = $metadata;
601
    }
602
603
    /**
604
     * Saves table metadata to cache.
605
     *
606
     * @param string $rawName
607
     */
608
    private function saveTableMetadataToCache(string $rawName): void
609
    {
610
        if ($this->schemaCache->isEnabled() === false || $this->schemaCache->isExcluded($rawName) === true) {
611
            return;
612
        }
613
614
        /** @psalm-var array<string, array<TableSchemaInterface|int>> */
615
        $metadata = $this->tableMetadata[$rawName];
616
        /** @var int */
617
        $metadata[self::CACHE_VERSION] = static::SCHEMA_CACHE_VERSION;
618
619
        $this->schemaCache->set(
620
            $this->getCacheKey($rawName),
621
            $metadata,
622
            $this->schemaCache->getDuration(),
623
            new TagDependency($this->getCacheTag()),
624
        );
625
    }
626
627
    protected function toTableNameInterface(string|TableNameInterface $name): TableNameInterface
628
    {
629
        if (!$name instanceof TableNameInterface) {
630
            $parts = array_reverse($this->db->getQuoter()->getTableNameParts($name));
631
            /** @psalm-var non-empty-array<string> $parts */
632
            $name = $this->db->createTableName(...$parts);
0 ignored issues
show
Bug introduced by
$parts is expanded, but the parameter $name of Yiisoft\Db\Connection\Co...face::createTableName() does not expect variable arguments. ( Ignorable by Annotation )

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

632
            $name = $this->db->createTableName(/** @scrutinizer ignore-type */ ...$parts);
Loading history...
633
        }
634
635
        return $name;
636
    }
637
638
    protected function findViewNames(string $schema = ''): array
0 ignored issues
show
Unused Code introduced by
The parameter $schema is not used and could be removed. ( Ignorable by Annotation )

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

638
    protected function findViewNames(/** @scrutinizer ignore-unused */ string $schema = ''): array

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
639
    {
640
        return [];
641
    }
642
643
    /**
644
     * @throws Throwable
645
     */
646
    public function getViewNames(string $schema = '', bool $refresh = false): array
647
    {
648
        if (!isset($this->viewNames[$schema]) || $refresh) {
649
            $this->viewNames[$schema] = $this->findViewNames($schema);
650
        }
651
652
        return (array) $this->viewNames[$schema];
653
    }
654
}
655