Skip to content

Commit a42f5bc

Browse files
Remove duplicate code move to yiisoft/db/Schema::class. (#409)
* Remove duplicate code move to yiisoft/db/Schema::class.
1 parent df027fd commit a42f5bc

1 file changed

Lines changed: 33 additions & 13 deletions

File tree

src/Schema/Schema.php

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
use Yiisoft\Db\Constraint\Constraint;
1313
use Yiisoft\Db\Exception\NotSupportedException;
1414

15+
use function array_change_key_case;
16+
use function array_map;
1517
use function gettype;
1618
use function is_array;
1719
use function preg_match;
@@ -165,7 +167,7 @@ public function getDefaultSchema(): string|null
165167

166168
public function getPdoType(mixed $data): int
167169
{
168-
/** @psalm-var array<string, int> */
170+
/** @psalm-var array<string, int> $typeMap */
169171
$typeMap = [
170172
// php type => PDO type
171173
self::PHP_TYPE_BOOLEAN => PDO::PARAM_BOOL,
@@ -250,28 +252,28 @@ public function getSchemaUniques(string $schema = '', bool $refresh = false): ar
250252

251253
public function getTableChecks(string $name, bool $refresh = false): array
252254
{
253-
/** @var mixed */
255+
/** @psalm-var mixed $tableChecks */
254256
$tableChecks = $this->getTableMetadata($name, self::CHECKS, $refresh);
255257
return is_array($tableChecks) ? $tableChecks : [];
256258
}
257259

258260
public function getTableDefaultValues(string $name, bool $refresh = false): array
259261
{
260-
/** @var mixed */
262+
/** @psalm-var mixed $tableDefaultValues */
261263
$tableDefaultValues = $this->getTableMetadata($name, self::DEFAULT_VALUES, $refresh);
262264
return is_array($tableDefaultValues) ? $tableDefaultValues : [];
263265
}
264266

265267
public function getTableForeignKeys(string $name, bool $refresh = false): array
266268
{
267-
/** @var mixed */
269+
/** @psalm-var mixed $tableForeignKeys */
268270
$tableForeignKeys = $this->getTableMetadata($name, self::FOREIGN_KEYS, $refresh);
269271
return is_array($tableForeignKeys) ? $tableForeignKeys : [];
270272
}
271273

272274
public function getTableIndexes(string $name, bool $refresh = false): array
273275
{
274-
/** @var mixed */
276+
/** @psalm-var mixed $tableIndexes */
275277
$tableIndexes = $this->getTableMetadata($name, self::INDEXES, $refresh);
276278
return is_array($tableIndexes) ? $tableIndexes : [];
277279
}
@@ -288,28 +290,29 @@ public function getTableNames(string $schema = '', bool $refresh = false): array
288290

289291
public function getTablePrimaryKey(string $name, bool $refresh = false): Constraint|null
290292
{
291-
/** @var mixed */
293+
/** @psalm-var mixed $tablePrimaryKey */
292294
$tablePrimaryKey = $this->getTableMetadata($name, self::PRIMARY_KEY, $refresh);
293295
return $tablePrimaryKey instanceof Constraint ? $tablePrimaryKey : null;
294296
}
295297

296298
public function getTableSchema(string $name, bool $refresh = false): TableSchemaInterface|null
297299
{
298-
/** @var mixed */
300+
/** @psalm-var mixed $tableSchema */
299301
$tableSchema = $this->getTableMetadata($name, self::SCHEMA, $refresh);
300302
return $tableSchema instanceof TableSchemaInterface ? $tableSchema : null;
301303
}
302304

303305
public function getTableSchemas(string $schema = '', bool $refresh = false): array
304306
{
305-
/** @var mixed */
307+
/** @psalm-var mixed $tableSchemas */
306308
$tableSchemas = $this->getSchemaMetadata($schema, self::SCHEMA, $refresh);
309+
307310
return is_array($tableSchemas) ? $tableSchemas : [];
308311
}
309312

310313
public function getTableUniques(string $name, bool $refresh = false): array
311314
{
312-
/** @var mixed */
315+
/** @psalm-var mixed $tableUniques */
313316
$tableUniques = $this->getTableMetadata($name, self::UNIQUES, $refresh);
314317
return is_array($tableUniques) ? $tableUniques : [];
315318
}
@@ -403,7 +406,7 @@ protected function findTableNames(string $schema): array
403406
*/
404407
protected function getColumnPhpType(ColumnSchemaInterface $column): string
405408
{
406-
/** @psalm-var string[] */
409+
/** @psalm-var string[] $typeMap */
407410
$typeMap = [
408411
// abstract type => php type
409412
self::TYPE_TINYINT => self::PHP_TYPE_INTEGER,
@@ -450,7 +453,7 @@ protected function getColumnPhpType(ColumnSchemaInterface $column): string
450453
protected function getSchemaMetadata(string $schema, string $type, bool $refresh): array
451454
{
452455
$metadata = [];
453-
/** @psalm-var string[] */
456+
/** @psalm-var string[] $tableNames */
454457
$tableNames = $this->getTableNames($schema, $refresh);
455458

456459
foreach ($tableNames as $name) {
@@ -533,6 +536,23 @@ protected function getTableTypeMetadata(
533536
};
534537
}
535538

539+
/**
540+
* Changes row's array key case to lower.
541+
*
542+
* @param array $row row's array or an array of row's arrays.
543+
* @param bool $multiple whether multiple rows or a single row passed.
544+
*
545+
* @return array normalized row or rows.
546+
*/
547+
protected function normalizeRowKeyCase(array $row, bool $multiple): array
548+
{
549+
if ($multiple) {
550+
return array_map(static fn (array $row) => array_change_key_case($row), $row);
551+
}
552+
553+
return array_change_key_case($row);
554+
}
555+
536556
/**
537557
* Resolves the table name and schema name (if any).
538558
*
@@ -542,7 +562,7 @@ protected function getTableTypeMetadata(
542562
*
543563
* @return TableSchemaInterface with resolved table, schema, etc. names.
544564
*
545-
* {@see \Yiisoft\Db\Schema\TableSchemaInterface}
565+
* {@see TableSchemaInterface}
546566
*/
547567
protected function resolveTableName(string $name): TableSchemaInterface
548568
{
@@ -602,7 +622,7 @@ private function saveTableMetadataToCache(string $rawName): void
602622
return;
603623
}
604624

605-
/** @psalm-var array<string, array<TableSchemaInterface|int>> */
625+
/** @psalm-var array<string, array<TableSchemaInterface|int>> $metadata */
606626
$metadata = $this->tableMetadata[$rawName];
607627
/** @var int */
608628
$metadata[self::CACHE_VERSION] = static::SCHEMA_CACHE_VERSION;

0 commit comments

Comments
 (0)