diff --git a/UPGRADE.md b/UPGRADE.md index 4c163695feb..69fb612d654 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -55,6 +55,11 @@ all drivers and middleware. # Upgrade to 4.3 +## Deprecated `AbstractAsset::_setName()` + +Setting object name via `AbstractAsset::_setName()` has been deprecated. Pass the name to the `AbstractAsset` +constructor instead. + ## Marked `Identifier` class as internal In order to build SQL identifiers, use `AbstractPlatform::quoteSingleIdentifier()`. diff --git a/psalm.xml.dist b/psalm.xml.dist index b5358bb294e..1ffb1c1bcfa 100644 --- a/psalm.xml.dist +++ b/psalm.xml.dist @@ -55,6 +55,12 @@ + + + diff --git a/src/Schema/AbstractAsset.php b/src/Schema/AbstractAsset.php index 5f2d4755ff6..21e8c8a6b54 100644 --- a/src/Schema/AbstractAsset.php +++ b/src/Schema/AbstractAsset.php @@ -8,6 +8,7 @@ use Doctrine\DBAL\Schema\Exception\InvalidObjectName; use Doctrine\DBAL\Schema\Name\Parser; use Doctrine\DBAL\Schema\Name\Parser\Identifier; +use Doctrine\Deprecations\Deprecation; use function array_map; use function count; @@ -40,11 +41,36 @@ abstract class AbstractAsset /** @var list */ private array $identifiers = []; + public function __construct(?string $name = null) + { + if ($name === null) { + Deprecation::trigger( + 'doctrine/dbal', + 'https://github.com/doctrine/dbal/pull/6610', + 'Not passing $name to %s is deprecated.', + __METHOD__, + ); + + return; + } + + $this->_setName($name); + } + /** * Sets the name of this asset. + * + * @deprecated Use the constructor instead. */ protected function _setName(string $name): void { + Deprecation::triggerIfCalledFromOutside( + 'doctrine/dbal', + 'https://github.com/doctrine/dbal/pull/6610', + '%s is deprecated. Use the constructor instead.', + __METHOD__, + ); + if ($name !== '') { $parser = new Parser(); diff --git a/src/Schema/Column.php b/src/Schema/Column.php index 2d02403b262..aaad66a9e70 100644 --- a/src/Schema/Column.php +++ b/src/Schema/Column.php @@ -50,7 +50,8 @@ class Column extends AbstractAsset */ public function __construct(string $name, Type $type, array $options = []) { - $this->_setName($name); + parent::__construct($name); + $this->setType($type); $this->setOptions($options); } diff --git a/src/Schema/ForeignKeyConstraint.php b/src/Schema/ForeignKeyConstraint.php index bb5ef7f0433..2699139a20a 100644 --- a/src/Schema/ForeignKeyConstraint.php +++ b/src/Schema/ForeignKeyConstraint.php @@ -53,7 +53,7 @@ public function __construct( string $name = '', protected array $options = [], ) { - $this->_setName($name); + parent::__construct($name); $this->_localColumnNames = $this->createIdentifierMap($localColumnNames); $this->_foreignTableName = new Identifier($foreignTableName); diff --git a/src/Schema/Identifier.php b/src/Schema/Identifier.php index 6d04c4227a2..b4e54ba4cbb 100644 --- a/src/Schema/Identifier.php +++ b/src/Schema/Identifier.php @@ -20,7 +20,7 @@ class Identifier extends AbstractAsset */ public function __construct(string $identifier, bool $quote = false) { - $this->_setName($identifier); + parent::__construct($identifier); if (! $quote || $this->_quoted) { return; diff --git a/src/Schema/Index.php b/src/Schema/Index.php index 175565483d1..0ebd2bac717 100644 --- a/src/Schema/Index.php +++ b/src/Schema/Index.php @@ -47,13 +47,9 @@ public function __construct( array $flags = [], private readonly array $options = [], ) { - $isUnique = $isUnique || $isPrimary; + parent::__construct($name ?? ''); - if ($name !== null) { - $this->_setName($name); - } - - $this->_isUnique = $isUnique; + $this->_isUnique = $isUnique || $isPrimary; $this->_isPrimary = $isPrimary; foreach ($columns as $column) { diff --git a/src/Schema/Schema.php b/src/Schema/Schema.php index 25fe4a3c6d8..68b089ed364 100644 --- a/src/Schema/Schema.php +++ b/src/Schema/Schema.php @@ -76,9 +76,7 @@ public function __construct( $name = $schemaConfig->getName(); - if ($name !== null) { - $this->_setName($name); - } + parent::__construct($name ?? ''); foreach ($namespaces as $namespace) { $this->createNamespace($namespace); @@ -290,7 +288,12 @@ public function createTable(string $name): Table public function renameTable(string $oldName, string $newName): self { $table = $this->getTable($oldName); - $table->_setName($newName); + + $identifier = new Identifier($newName); + + $table->_name = $identifier->_name; + $table->_namespace = $identifier->_namespace; + $table->_quoted = $identifier->_quoted; $this->dropTable($oldName); $this->_addTable($table); diff --git a/src/Schema/Sequence.php b/src/Schema/Sequence.php index 32a5e67bd24..6b1c5ac6d05 100644 --- a/src/Schema/Sequence.php +++ b/src/Schema/Sequence.php @@ -22,7 +22,8 @@ public function __construct( int $initialValue = 1, protected ?int $cache = null, ) { - $this->_setName($name); + parent::__construct($name); + $this->setAllocationSize($allocationSize); $this->setInitialValue($initialValue); } diff --git a/src/Schema/Table.php b/src/Schema/Table.php index 851a43651b3..ba6f03406c3 100644 --- a/src/Schema/Table.php +++ b/src/Schema/Table.php @@ -82,7 +82,7 @@ public function __construct( throw InvalidTableName::new($name); } - $this->_setName($name); + parent::__construct($name); foreach ($columns as $column) { $this->_addColumn($column); @@ -298,10 +298,15 @@ final public function renameColumn(string $oldName, string $newName): Column )); } - $column = $this->getColumn($oldName); - $column->_setName($newName); + $oldColumn = $this->getColumn($oldName); + $options = $oldColumn->toArray(); + + unset($options['name'], $options['type']); + + $newColumn = new Column($newName, $oldColumn->getType(), $options); + unset($this->_columns[$oldName]); - $this->_addColumn($column); + $this->_addColumn($newColumn); $this->renameColumnInIndexes($oldName, $newName); $this->renameColumnInForeignKeyConstraints($oldName, $newName); @@ -318,7 +323,7 @@ final public function renameColumn(string $oldName, string $newName): Column $this->renamedColumns[$newName] = $oldName; } - return $column; + return $newColumn; } /** @param array $options */ diff --git a/src/Schema/UniqueConstraint.php b/src/Schema/UniqueConstraint.php index a33d446e071..5faef30149f 100644 --- a/src/Schema/UniqueConstraint.php +++ b/src/Schema/UniqueConstraint.php @@ -40,7 +40,7 @@ public function __construct( array $flags = [], private readonly array $options = [], ) { - $this->_setName($name); + parent::__construct($name); foreach ($columns as $column) { $this->addColumn($column); diff --git a/src/Schema/View.php b/src/Schema/View.php index 81f5f8a7d2b..37de4d77060 100644 --- a/src/Schema/View.php +++ b/src/Schema/View.php @@ -11,7 +11,7 @@ class View extends AbstractAsset { public function __construct(string $name, private readonly string $sql) { - $this->_setName($name); + parent::__construct($name); } public function getSql(): string