Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update psalm types #267

Merged
merged 5 commits into from
Aug 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 37 additions & 43 deletions src/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,7 @@
/**
* Implements the SQLite Server specific schema, supporting SQLite 3.3.0 or higher.
*
* @psalm-type Column = array<array-key, array{seqno:string, cid:string, name:string}>
* @psalm-type NormalizePragmaForeignKeyList = array<
* string,
* array<
* array-key,
* array{
* id:string,
* cid:string,
* seq:string,
* table:string,
* from:string,
* to:string,
* on_update:string,
* on_delete:string
* }
* >
* >
* @psalm-type PragmaForeignKeyList = array<
* string,
* array{
* @psalm-type ForeignKeyInfo = array{
* id:string,
* cid:string,
* seq:string,
Expand All @@ -61,17 +42,31 @@
* to:string,
* on_update:string,
* on_delete:string
* }
* >
* @psalm-type PragmaIndexInfo = array<array-key, array{seqno:string, cid:string, name:string}>
* @psalm-type PragmaIndexList = array<
* array-key,
* array{seq:string, name:string, unique:string, origin:string, partial:string}
* >
* @psalm-type PragmaTableInfo = array<
* array-key,
* array{cid:string, name:string, type:string, notnull:string, dflt_value:string|null, pk:string}
* }
* @psalm-type GroupedForeignKeyInfo = array<
* string,
* ForeignKeyInfo[]
* >
* @psalm-type IndexInfo = array{
* seqno:string,
* cid:string,
* name:string
* }
* @psalm-type IndexListInfo = array{
* seq:string,
* name:string,
* unique:string,
* origin:string,
* partial:string
* }
* @psalm-type ColumnInfo = array{
* cid:string,
* name:string,
* type:string,
* notnull:string,
* dflt_value:string|null,
* pk:string
* }
*/
final class Schema extends AbstractPdoSchema
{
Expand Down Expand Up @@ -201,15 +196,14 @@ protected function loadTablePrimaryKey(string $tableName): Constraint|null
protected function loadTableForeignKeys(string $tableName): array
{
$result = [];
/** @psalm-var PragmaForeignKeyList $foreignKeysList */

$foreignKeysList = $this->getPragmaForeignKeyList($tableName);
/** @psalm-var NormalizePragmaForeignKeyList $foreignKeysList */
/** @psalm-var ForeignKeyInfo[] $foreignKeysList */
$foreignKeysList = $this->normalizeRowKeyCase($foreignKeysList, true);
/** @psalm-var NormalizePragmaForeignKeyList $foreignKeysList */
$foreignKeysList = DbArrayHelper::index($foreignKeysList, null, ['table']);
DbArrayHelper::multisort($foreignKeysList, 'seq');

/** @psalm-var NormalizePragmaForeignKeyList $foreignKeysList */
/** @psalm-var GroupedForeignKeyInfo $foreignKeysList */
foreach ($foreignKeysList as $table => $foreignKey) {
$fk = (new ForeignKeyConstraint())
->columnNames(DbArrayHelper::getColumn($foreignKey, 'from'))
Expand Down Expand Up @@ -347,7 +341,7 @@ protected function loadTableDefaultValues(string $tableName): array
*/
protected function findColumns(TableSchemaInterface $table): bool
{
/** @psalm-var PragmaTableInfo $columns */
/** @psalm-var ColumnInfo[] $columns */
$columns = $this->getPragmaTableInfo($table->getName());

foreach ($columns as $info) {
Expand Down Expand Up @@ -380,7 +374,7 @@ protected function findColumns(TableSchemaInterface $table): bool
*/
protected function findConstraints(TableSchemaInterface $table): void
{
/** @psalm-var PragmaForeignKeyList $foreignKeysList */
/** @psalm-var ForeignKeyInfo[] $foreignKeysList */
$foreignKeysList = $this->getPragmaForeignKeyList($table->getName());

foreach ($foreignKeysList as $foreignKey) {
Expand Down Expand Up @@ -418,13 +412,13 @@ protected function findConstraints(TableSchemaInterface $table): void
*/
public function findUniqueIndexes(TableSchemaInterface $table): array
{
/** @psalm-var PragmaIndexList $indexList */
/** @psalm-var IndexListInfo[] $indexList */
$indexList = $this->getPragmaIndexList($table->getName());
$uniqueIndexes = [];

foreach ($indexList as $index) {
$indexName = $index['name'];
/** @psalm-var PragmaIndexInfo $indexInfo */
/** @psalm-var IndexInfo[] $indexInfo */
$indexInfo = $this->getPragmaIndexInfo($index['name']);

if ($index['unique']) {
Expand Down Expand Up @@ -535,7 +529,7 @@ private function normalizeDefaultValue(string|null $defaultValue, ColumnSchemaIn
private function loadTableColumnsInfo(string $tableName): array
{
$tableColumns = $this->getPragmaTableInfo($tableName);
/** @psalm-var PragmaTableInfo $tableColumns */
/** @psalm-var ColumnInfo[] $tableColumns */
$tableColumns = $this->normalizeRowKeyCase($tableColumns, true);

return DbArrayHelper::index($tableColumns, 'cid');
Expand All @@ -556,7 +550,7 @@ private function loadTableColumnsInfo(string $tableName): array
private function loadTableConstraints(string $tableName, string $returnType): Constraint|array|null
{
$indexList = $this->getPragmaIndexList($tableName);
/** @psalm-var PragmaIndexList $indexes */
/** @psalm-var IndexListInfo[] $indexes */
$indexes = $this->normalizeRowKeyCase($indexList, true);
$result = [
self::PRIMARY_KEY => null,
Expand All @@ -565,7 +559,7 @@ private function loadTableConstraints(string $tableName, string $returnType): Co
];

foreach ($indexes as $index) {
/** @psalm-var Column $columns */
/** @psalm-var IndexInfo[] $columns */
$columns = $this->getPragmaIndexInfo($index['name']);

if ($index['origin'] === 'pk') {
Expand All @@ -592,7 +586,7 @@ private function loadTableConstraints(string $tableName, string $returnType): Co
*
* @link https://www.sqlite.org/lang_createtable.html#primkeyconst
*
* @psalm-var PragmaTableInfo $tableColumns
* @psalm-var ColumnInfo[] $tableColumns
*/
$tableColumns = $this->loadTableColumnsInfo($tableName);

Expand Down Expand Up @@ -645,7 +639,7 @@ private function getPragmaIndexInfo(string $name): array
$column = $this->db
->createCommand('PRAGMA INDEX_INFO(' . (string) $this->db->getQuoter()->quoteValue($name) . ')')
->queryAll();
/** @psalm-var Column $column */
/** @psalm-var IndexInfo[] $column */
$column = $this->normalizeRowKeyCase($column, true);
DbArrayHelper::multisort($column, 'seqno');

Expand Down
Loading