Skip to content

Commit af03149

Browse files
authored
Merge pull request #7094 from morozov/deprecate-get-name
Deprecate AbstractAsset::getName()
2 parents 87adc20 + 9af76d0 commit af03149

26 files changed

+325
-185
lines changed

UPGRADE.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ awareness about deprecated code.
88

99
# Upgrade to 4.4
1010

11+
## Deprecated `AbstractAsset::getName()`
12+
13+
The `AbstractAsset::getName()` method has been deprecated. Instead, use `NamedObject::getObjectName()` or
14+
`OptionallyQualifiedName::getObjectName()` to get the object representation of the name. SQL context, convert the
15+
resulting `Name` to SQL using `Name::toSQL()`. In other contexts, convert the resulting name to string using
16+
`Name::toString()`.
17+
1118
## Deprecated `AbstractAsset::isQuoted()`
1219

1320
The `AbstractAsset::isQuoted()` method has been deprecated. The recommended approach depends on the object class:
@@ -326,7 +333,7 @@ The `Table::__construct()` method has been marked as internal. Use `Table::edito
326333

327334
## Deprecated `AbstractAsset::getShortestName()`
328335

329-
The `AbstractAsset::getShortestName()` method has been deprecated. Use `AbstractAsset::getName()` instead.
336+
The `AbstractAsset::getShortestName()` method has been deprecated. Use `NamedObject::getObjectName()` instead.
330337

331338
## Deprecated `Sequence::isAutoIncrementsFor()`
332339

docs/en/reference/schema-manager.rst

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ Now you can loop over the array inspecting each sequence object:
5959
6060
<?php
6161
foreach ($sequences as $sequence) {
62-
echo $sequence->getName() . "\n";
62+
echo $sequence->getObjectName()->toString() . PHP_EOL;
6363
}
6464
6565
listTableColumns()
@@ -79,7 +79,7 @@ Now you can loop over the array inspecting each column object:
7979
8080
<?php
8181
foreach ($columns as $column) {
82-
echo $column->getName() . ': ' . $column->getType() . "\n";
82+
echo $column->getObjectName()->toString() . ': ' . $column->getType() . PHP_EOL;
8383
}
8484
8585
introspectTable()
@@ -119,7 +119,7 @@ object:
119119
120120
<?php
121121
foreach ($foreignKeys as $foreignKey) {
122-
echo $foreignKey->getName() ."\n";
122+
echo $foreignKey->getObjectName()->toString() . PHP_EOL;
123123
}
124124
125125
listTableIndexes()
@@ -139,7 +139,12 @@ Now you can loop over the array inspecting each index object:
139139
140140
<?php
141141
foreach ($indexes as $index) {
142-
echo $index->getName() . ': ' . ($index->isUnique() ? 'unique' : 'not unique') . "\n";
142+
echo $index->getObjectName()->toString() . ': ' . match($index->getType()) {
143+
IndexType::REGULAR => 'regular',
144+
IndexType::UNIQUE => 'unique',
145+
IndexType::FULLTEXT => 'fulltext',
146+
IndexType::SPATIAL => 'spatial',
147+
} . PHP_EOL;
143148
}
144149
145150
listTables()
@@ -162,9 +167,9 @@ retrieved with the ``getColumns()`` method:
162167
163168
<?php
164169
foreach ($tables as $table) {
165-
echo $table->getName() . " columns:\n\n";
170+
echo $table->getObjectName()->toString() . " columns:" . PHP_EOL;
166171
foreach ($table->getColumns() as $column) {
167-
echo ' - ' . $column->getName() . "\n";
172+
echo ' - ' . $column->getObjectName()->toString() . PHP_EOL;
168173
}
169174
}
170175
@@ -185,7 +190,7 @@ Now you can loop over the array inspecting each view object:
185190
186191
<?php
187192
foreach ($views as $view) {
188-
echo $view->getName() . ': ' . $view->getSql() . "\n";
193+
echo $view->getObjectName()->toString() . ': ' . $view->getSql() . PHP_EOL;
189194
}
190195
191196
introspectSchema()

src/Schema/AbstractAsset.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ public function getNamespaceName(): ?string
272272
* The shortest name is stripped of the default namespace. All other
273273
* namespaced elements are returned as full-qualified names.
274274
*
275-
* @deprecated Use {@link getName()} instead.
275+
* @deprecated Use {@see NamedObject::getObjectName()} instead.
276276
*/
277277
public function getShortestName(?string $defaultNamespaceName): string
278278
{
@@ -340,9 +340,20 @@ protected function trimQuotes(string $identifier): string
340340

341341
/**
342342
* Returns the name of this schema asset.
343+
*
344+
* @deprecated Use {@see NamedObject::getObjectName()} or {@see OptionallyQualifiedName::getObjectName()} instead.
345+
* In SQL context, convert the resulting {@see Name} to SQL using {@see Name::toSQL()}. In other
346+
* contexts, convert the resulting name to string using {@see Name::toString()}.
343347
*/
344348
public function getName(): string
345349
{
350+
Deprecation::triggerIfCalledFromOutside(
351+
'doctrine/dbal',
352+
'https://github.com/doctrine/dbal/pull/7094',
353+
'%s is deprecated and will be removed in 5.0.',
354+
__METHOD__,
355+
);
356+
346357
if ($this->_namespace !== null) {
347358
return $this->_namespace . '.' . $this->_name;
348359
}

src/Schema/AbstractSchemaManager.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public function listSchemaNames(): array
8888
/**
8989
* Lists the available sequences for this connection.
9090
*
91-
* @return array<int, Sequence>
91+
* @return list<Sequence>
9292
*
9393
* @throws Exception
9494
*/
@@ -177,7 +177,7 @@ public function tableExists(string $tableName): bool
177177
/**
178178
* Returns a list of all tables in the current database.
179179
*
180-
* @return array<int, non-empty-string>
180+
* @return list<non-empty-string>
181181
*
182182
* @throws Exception
183183
*/
@@ -196,9 +196,11 @@ public function listTableNames(): array
196196
* Filters asset names if they are configured to return only a subset of all
197197
* the found elements.
198198
*
199-
* @param array<int, mixed> $assetNames
199+
* @param list<N> $assetNames
200200
*
201-
* @return array<int, mixed>
201+
* @return list<N>
202+
*
203+
* @template N
202204
*/
203205
private function filterAssetNames(array $assetNames): array
204206
{

src/Schema/Table.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ final public function renameColumn(string $oldName, string $newName): Column
412412
if ($oldName === $newName) {
413413
throw new LogicException(sprintf(
414414
'Attempt to rename column "%s.%s" to the same name.',
415-
$this->getName(),
415+
$this->name->toString(),
416416
$oldName,
417417
));
418418
}
@@ -643,10 +643,11 @@ public function dropUniqueConstraint(string $name): void
643643
/**
644644
* Returns the list of table columns.
645645
*
646-
* @return list<Column>
646+
* @return non-empty-list<Column>
647647
*/
648648
public function getColumns(): array
649649
{
650+
/** @phpstan-ignore return.type */
650651
return array_values($this->_columns);
651652
}
652653

tests/Functional/Driver/DBAL6024Test.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function testDropPrimaryKey(): void
5454
}
5555

5656
$validationSchema = $schemaManager->introspectSchema();
57-
$validationTable = $validationSchema->getTable($table->getName());
57+
$validationTable = $validationSchema->getTable($table->getObjectName()->toString());
5858

5959
self::assertNull($validationTable->getPrimaryKey());
6060
}

tests/Functional/Driver/DBAL6044Test.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,19 @@ public function testUnloggedTables(): void
5353
$schemaManager = $this->connection->createSchemaManager();
5454

5555
$validationSchema = $schemaManager->introspectSchema();
56-
$validationUnloggedTable = $validationSchema->getTable($unloggedTable->getName());
56+
$validationUnloggedTable = $validationSchema->getTable($unloggedTable->getObjectName()->toString());
5757
self::assertTrue($validationUnloggedTable->getOption('unlogged'));
58-
$validationLoggedTable = $validationSchema->getTable($loggedTable->getName());
58+
$validationLoggedTable = $validationSchema->getTable($loggedTable->getObjectName()->toString());
5959
self::assertFalse($validationLoggedTable->getOption('unlogged'));
6060

6161
$sql = 'SELECT relpersistence FROM pg_class WHERE relname = ?';
6262
$stmt = $this->connection->prepare($sql);
6363

64-
$stmt->bindValue(1, $unloggedTable->getName());
64+
$stmt->bindValue(1, $unloggedTable->getObjectName()->toString());
6565
$unloggedTablePersistenceType = $stmt->executeQuery()->fetchOne();
6666
self::assertEquals('u', $unloggedTablePersistenceType);
6767

68-
$stmt->bindValue(1, $loggedTable->getName());
68+
$stmt->bindValue(1, $loggedTable->getObjectName()->toString());
6969
$loggedTablePersistenceType = $stmt->executeQuery()->fetchOne();
7070
self::assertEquals('p', $loggedTablePersistenceType);
7171
}

tests/Functional/Platform/AlterColumnTest.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@
77
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
88
use Doctrine\DBAL\Schema\Column;
99
use Doctrine\DBAL\Schema\ColumnEditor;
10+
use Doctrine\DBAL\Schema\Name\UnqualifiedName;
1011
use Doctrine\DBAL\Schema\Table;
1112
use Doctrine\DBAL\Tests\FunctionalTestCase;
1213
use Doctrine\DBAL\Types\Types;
1314

15+
use function array_map;
16+
1417
class AlterColumnTest extends FunctionalTestCase
1518
{
1619
public function testColumnPositionRetainedAfterAltering(): void
@@ -45,12 +48,15 @@ public function testColumnPositionRetainedAfterAltering(): void
4548

4649
$sm->alterTable($diff);
4750

48-
$table = $sm->introspectTable('test_alter');
49-
$columns = $table->getColumns();
51+
$table = $sm->introspectTable('test_alter');
5052

51-
self::assertCount(2, $columns);
52-
self::assertEqualsIgnoringCase('c1', $columns[0]->getName());
53-
self::assertEqualsIgnoringCase('c2', $columns[1]->getName());
53+
$this->assertUnqualifiedNameListEquals([
54+
UnqualifiedName::unquoted('c1'),
55+
UnqualifiedName::unquoted('c2'),
56+
], array_map(
57+
static fn (Column $column): UnqualifiedName => $column->getObjectName(),
58+
$table->getColumns(),
59+
));
5460
}
5561

5662
public function testSupportsCollations(): void

tests/Functional/Platform/RenameColumnTest.php

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@
66

77
use Doctrine\DBAL\Exception;
88
use Doctrine\DBAL\Schema\Column;
9+
use Doctrine\DBAL\Schema\Name\UnqualifiedName;
910
use Doctrine\DBAL\Schema\Table;
1011
use Doctrine\DBAL\Schema\TableDiff;
1112
use Doctrine\DBAL\Tests\FunctionalTestCase;
1213
use Doctrine\DBAL\Types\Type;
1314
use Doctrine\DBAL\Types\Types;
1415
use PHPUnit\Framework\Attributes\DataProvider;
1516

17+
use function array_map;
18+
1619
class RenameColumnTest extends FunctionalTestCase
1720
{
1821
/**
@@ -56,12 +59,16 @@ public function testColumnPositionRetainedAfterImplicitRenaming(string $oldColum
5659

5760
$sm->alterTable($diff);
5861

59-
$table = $sm->introspectTable('test_rename');
60-
$columns = $table->getColumns();
62+
$table = $sm->introspectTable('test_rename');
63+
64+
$this->assertUnqualifiedNameListEquals([
65+
UnqualifiedName::unquoted($newColumnName),
66+
UnqualifiedName::unquoted('c2'),
67+
], array_map(
68+
static fn (Column $column): UnqualifiedName => $column->getObjectName(),
69+
$table->getColumns(),
70+
));
6171

62-
self::assertCount(2, $columns);
63-
self::assertEqualsIgnoringCase($newColumnName, $columns[0]->getName());
64-
self::assertEqualsIgnoringCase('c2', $columns[1]->getName());
6572
self::assertCount(1, self::getRenamedColumns($diff));
6673
self::assertCount(1, $diff->getRenamedColumns());
6774
}
@@ -75,7 +82,10 @@ public static function getRenamedColumns(TableDiff $tableDiff): array
7582
continue;
7683
}
7784

78-
$oldColumnName = $diff->getOldColumn()->getName();
85+
$oldColumnName = $diff->getOldColumn()
86+
->getObjectName()
87+
->toString();
88+
7989
$renamed[$oldColumnName] = $diff->getNewColumn();
8090
}
8191

@@ -117,23 +127,27 @@ public function testColumnPositionRetainedAfterExplicitRenaming(string $oldColum
117127

118128
$sm->alterTable($diff);
119129

120-
$table = $sm->introspectTable('test_rename');
121-
$columns = $table->getColumns();
130+
$table = $sm->introspectTable('test_rename');
122131

123132
self::assertCount(1, $diff->getChangedColumns());
124133
self::assertCount(1, $diff->getRenamedColumns());
125134
self::assertCount(1, $diff->getModifiedColumns());
126-
self::assertCount(2, $columns);
127-
self::assertEqualsIgnoringCase($newColumnName, $columns[0]->getName());
128-
self::assertEqualsIgnoringCase('c2', $columns[1]->getName());
135+
136+
$this->assertUnqualifiedNameListEquals([
137+
UnqualifiedName::unquoted($newColumnName),
138+
UnqualifiedName::unquoted('c2'),
139+
], array_map(
140+
static fn (Column $column): UnqualifiedName => $column->getObjectName(),
141+
$table->getColumns(),
142+
));
129143
}
130144

131145
/** @return iterable<array{non-empty-string,non-empty-string}> */
132146
public static function columnNameProvider(): iterable
133147
{
134148
yield ['c1', 'c1_x'];
135149
yield ['C1', 'c1_x'];
136-
yield ['importantColumn', 'veryImportantColumn'];
150+
yield ['importantColumn', 'very_important_column'];
137151
}
138152

139153
/** @throws Exception */

tests/Functional/Schema/AlterTableTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,10 @@ private function testMigration(Table $oldTable, callable $migration, ?Comparator
413413

414414
$schemaManager->alterTable($diff);
415415

416-
$introspectedTable = $schemaManager->introspectTable($newTable->getName());
416+
$introspectedTable = $schemaManager->introspectTable(
417+
$newTable->getObjectName()
418+
->toString(),
419+
);
417420

418421
$diff = $schemaManager->createComparator()
419422
->compareTables($newTable, $introspectedTable);

0 commit comments

Comments
 (0)