Skip to content

Commit 36626a8

Browse files
committed
schema: rewrite to readonly classes
1 parent 0c62a78 commit 36626a8

6 files changed

+106
-94
lines changed

src/Platforms/Data/Column.php

+17-13
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,21 @@ class Column
1111
use StrictObjectTrait;
1212

1313

14-
public string $name;
15-
public string $type;
16-
public ?int $size;
17-
public ?string $default;
18-
public bool $isPrimary;
19-
public bool $isAutoincrement;
20-
public bool $isUnsigned;
21-
public bool $isNullable;
22-
/**
23-
* @var mixed[]
24-
* @phpstan-var array<string, mixed>
25-
*/
26-
public array $meta = [];
14+
public function __construct(
15+
public readonly string $name,
16+
public readonly string $type,
17+
public readonly ?int $size,
18+
public readonly ?string $default,
19+
public readonly bool $isPrimary,
20+
public readonly bool $isAutoincrement,
21+
public readonly bool $isUnsigned,
22+
public readonly bool $isNullable,
23+
/**
24+
* @var mixed[]
25+
* @phpstan-var array<string, mixed>
26+
*/
27+
public array $meta = [],
28+
)
29+
{
30+
}
2731
}

src/Platforms/Data/ForeignKey.php

+10-6
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,16 @@ class ForeignKey
1111
use StrictObjectTrait;
1212

1313

14-
public string $name;
15-
public string $schema;
16-
public string $column;
17-
public string $refTable;
18-
public string $refTableSchema;
19-
public string $refColumn;
14+
public function __construct(
15+
public readonly string $name,
16+
public readonly string $schema,
17+
public readonly string $column,
18+
public readonly string $refTable,
19+
public readonly string $refTableSchema,
20+
public readonly string $refColumn,
21+
)
22+
{
23+
}
2024

2125

2226
public function getNameFqn(): string

src/Platforms/Data/Table.php

+7-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@ class Table
1111
use StrictObjectTrait;
1212

1313

14-
public string $name;
15-
public string $schema;
16-
public bool $isView;
14+
public function __construct(
15+
public readonly string $name,
16+
public readonly string $schema,
17+
public readonly bool $isView = false,
18+
)
19+
{
20+
}
1721

1822

1923
/**

src/Platforms/MySqlPlatform.php

+24-24
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ public function getTables(?string $schema = null): array
5858

5959
$tables = [];
6060
foreach ($result as $row) {
61-
$table = new Table();
62-
$table->name = (string) $row->TABLE_NAME;
63-
$table->schema = (string) $row->TABLE_SCHEMA;
64-
$table->isView = $row->TABLE_TYPE === 'VIEW';
65-
61+
$table = new Table(
62+
name: (string) $row->TABLE_NAME,
63+
schema: (string) $row->TABLE_SCHEMA,
64+
isView: $row->TABLE_TYPE === 'VIEW',
65+
);
6666
$tables[$table->getUnescapedFqn()] = $table;
6767
}
6868
return $tables;
@@ -81,17 +81,17 @@ public function getColumns(string $table, ?string $schema = null): array
8181
foreach ($query as $row) {
8282
$type = explode('(', (string) $row->Type);
8383

84-
$column = new Column();
85-
$column->name = (string) $row->Field;
86-
$column->type = strtoupper($type[0]);
87-
$column->size = isset($type[1]) ? (int) $type[1] : null;
88-
$column->default = $row->Default !== null ? (string) $row->Default : null;
89-
$column->isPrimary = $row->Key === 'PRI';
90-
$column->isAutoincrement = $row->Extra === 'auto_increment';
91-
$column->isUnsigned = (bool) strstr((string) $row->Type, 'unsigned');
92-
$column->isNullable = $row->Null === 'YES';
93-
$column->meta = [];
94-
84+
$column = new Column(
85+
name: (string) $row->Field,
86+
type: strtoupper($type[0]),
87+
size: isset($type[1]) ? (int) $type[1] : null,
88+
default: $row->Default !== null ? (string) $row->Default : null,
89+
isPrimary: $row->Key === 'PRI',
90+
isAutoincrement: $row->Extra === 'auto_increment',
91+
isUnsigned: (bool) strstr((string) $row->Type, 'unsigned'),
92+
isNullable: $row->Null === 'YES',
93+
meta: [],
94+
);
9595
$columns[$column->name] = $column;
9696
}
9797
return $columns;
@@ -122,14 +122,14 @@ public function getForeignKeys(string $table, ?string $schema = null): array
122122
/** @var array<string, ForeignKey> $keys */
123123
$keys = [];
124124
foreach ($result as $row) {
125-
$foreignKey = new ForeignKey();
126-
$foreignKey->name = (string) $row->CONSTRAINT_NAME;
127-
$foreignKey->schema = (string) $row->CONSTRAINT_SCHEMA;
128-
$foreignKey->column = (string) $row->COLUMN_NAME;
129-
$foreignKey->refTable = (string) $row->REFERENCED_TABLE_NAME;
130-
$foreignKey->refTableSchema = (string) $row->REFERENCED_TABLE_SCHEMA;
131-
$foreignKey->refColumn = (string) $row->REFERENCED_COLUMN_NAME;
132-
125+
$foreignKey = new ForeignKey(
126+
name: (string) $row->CONSTRAINT_NAME,
127+
schema: (string) $row->CONSTRAINT_SCHEMA,
128+
column: (string) $row->COLUMN_NAME,
129+
refTable: (string) $row->REFERENCED_TABLE_NAME,
130+
refTableSchema: (string) $row->REFERENCED_TABLE_SCHEMA,
131+
refColumn: (string) $row->REFERENCED_COLUMN_NAME,
132+
);
133133
$keys[$foreignKey->column] = $foreignKey;
134134
}
135135
return $keys;

src/Platforms/PostgreSqlPlatform.php

+24-24
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@ public function getTables(?string $schema = null): array
6464

6565
$tables = [];
6666
foreach ($result as $row) {
67-
$table = new Table();
68-
$table->name = (string) $row->name;
69-
$table->schema = (string) $row->schema;
70-
$table->isView = (bool) $row->is_view;
71-
67+
$table = new Table(
68+
name: (string) $row->name,
69+
schema: (string) $row->schema,
70+
isView: (bool) $row->is_view,
71+
);
7272
$tables[$table->getUnescapedFqn()] = $table;
7373
}
7474
return $tables;
@@ -110,17 +110,17 @@ public function getColumns(string $table, ?string $schema = null): array
110110

111111
$columns = [];
112112
foreach ($result as $row) {
113-
$column = new Column();
114-
$column->name = (string) $row->name;
115-
$column->type = (string) $row->type;
116-
$column->size = $row->size !== null ? (int) $row->size : null;
117-
$column->default = $row->default !== null ? (string) $row->default : null;
118-
$column->isPrimary = (bool) $row->is_primary;
119-
$column->isAutoincrement = (bool) $row->is_autoincrement;
120-
$column->isUnsigned = false;
121-
$column->isNullable = (bool) $row->is_nullable;
122-
$column->meta = isset($row->sequence) ? ['sequence' => $row->sequence] : [];
123-
113+
$column = new Column(
114+
name: (string) $row->name,
115+
type: (string) $row->type,
116+
size: $row->size !== null ? (int) $row->size : null,
117+
default: $row->default !== null ? (string) $row->default : null,
118+
isPrimary: (bool) $row->is_primary,
119+
isAutoincrement: (bool) $row->is_autoincrement,
120+
isUnsigned: false,
121+
isNullable: (bool) $row->is_nullable,
122+
meta: isset($row->sequence) ? ['sequence' => $row->sequence] : [],
123+
);
124124
$columns[$column->name] = $column;
125125
}
126126
return $columns;
@@ -157,14 +157,14 @@ public function getForeignKeys(string $table, ?string $schema = null): array
157157

158158
$keys = [];
159159
foreach ($result as $row) {
160-
$foreignKey = new ForeignKey();
161-
$foreignKey->name = (string) $row->name;
162-
$foreignKey->schema = (string) $row->schema;
163-
$foreignKey->column = (string) $row->column;
164-
$foreignKey->refTable = (string) $row->ref_table;
165-
$foreignKey->refTableSchema = (string) $row->ref_table_schema;
166-
$foreignKey->refColumn = (string) $row->ref_column;
167-
160+
$foreignKey = new ForeignKey(
161+
name: (string) $row->name,
162+
schema: (string) $row->schema,
163+
column: (string) $row->column,
164+
refTable: (string) $row->ref_table,
165+
refTableSchema: (string) $row->ref_table_schema,
166+
refColumn: (string) $row->ref_column,
167+
);
168168
$keys[$foreignKey->column] = $foreignKey;
169169
}
170170
return $keys;

src/Platforms/SqlServerPlatform.php

+24-24
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ public function getTables(?string $schema = null): array
4949

5050
$tables = [];
5151
foreach ($result as $row) {
52-
$table = new Table();
53-
$table->name = (string) $row->TABLE_NAME;
54-
$table->schema = (string) $row->TABLE_SCHEMA;
55-
$table->isView = $row->TABLE_TYPE === 'VIEW';
56-
52+
$table = new Table(
53+
name: (string) $row->TABLE_NAME,
54+
schema: (string) $row->TABLE_SCHEMA,
55+
isView: $row->TABLE_TYPE === 'VIEW',
56+
);
5757
$tables[$table->getUnescapedFqn()] = $table;
5858
}
5959
return $tables;
@@ -105,17 +105,17 @@ public function getColumns(string $table, ?string $schema = null): array
105105

106106
$columns = [];
107107
foreach ($result as $row) {
108-
$column = new Column();
109-
$column->name = (string) $row->name;
110-
$column->type = (string) $row->type;
111-
$column->size = $row->size !== null ? (int) $row->size : null;
112-
$column->default = $row->default !== null ? (string) $row->default : null;
113-
$column->isPrimary = (bool) $row->is_primary;
114-
$column->isAutoincrement = (bool) $row->is_autoincrement;
115-
$column->isUnsigned = false; // not available in SqlServer
116-
$column->isNullable = (bool) $row->is_nullable;
117-
$column->meta = [];
118-
108+
$column = new Column(
109+
name: (string) $row->name,
110+
type: (string) $row->type,
111+
size: $row->size !== null ? (int) $row->size : null,
112+
default: $row->default !== null ? (string) $row->default : null,
113+
isPrimary: (bool) $row->is_primary,
114+
isAutoincrement: (bool) $row->is_autoincrement,
115+
isUnsigned: false, // not available in SqlServer
116+
isNullable: (bool) $row->is_nullable,
117+
meta: [],
118+
);
119119
$columns[$column->name] = $column;
120120
}
121121

@@ -154,14 +154,14 @@ public function getForeignKeys(string $table, ?string $schema = null): array
154154

155155
$keys = [];
156156
foreach ($result as $row) {
157-
$foreignKey = new ForeignKey();
158-
$foreignKey->name = (string) $row->name;
159-
$foreignKey->schema = (string) $row->schema;
160-
$foreignKey->column = (string) $row->column;
161-
$foreignKey->refTable = (string) $row->ref_table;
162-
$foreignKey->refTableSchema = (string) $row->ref_table_schema;
163-
$foreignKey->refColumn = (string) $row->ref_column;
164-
157+
$foreignKey = new ForeignKey(
158+
name: (string) $row->name,
159+
schema: (string) $row->schema,
160+
column: (string) $row->column,
161+
refTable: (string) $row->ref_table,
162+
refTableSchema: (string) $row->ref_table_schema,
163+
refColumn: (string) $row->ref_column,
164+
);
165165
$keys[$foreignKey->column] = $foreignKey;
166166
}
167167
return $keys;

0 commit comments

Comments
 (0)