Skip to content

Commit f7b60e3

Browse files
authored
Merge pull request #6666 from morozov/remove-namespace-name
Remove AbstractAsset namespace-related methods
2 parents 98a18fd + f049057 commit f7b60e3

File tree

5 files changed

+57
-125
lines changed

5 files changed

+57
-125
lines changed

UPGRADE.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ awareness about deprecated code.
88

99
# Upgrade to 5.0
1010

11+
## BC BREAK: Removed `AbstractAsset` namespace-related methods and property
12+
13+
The following namespace-related methods and property have been removed:
14+
15+
- `AbstractAsset::getNamespaceName()`
16+
- `AbstractAsset::isInDefaultNamespace()`
17+
- `AbstractAsset::$_namespace`
18+
1119
## BC BREAK: Removed `AbstractAsset::getShortestName()`
1220

1321
The `AbstractAsset::getShortestName()` method has been removed.

psalm.xml.dist

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -62,25 +62,8 @@
6262
-->
6363
<referencedMethod name="Doctrine\DBAL\Schema\AbstractAsset::getNameParser" />
6464
<referencedMethod name="Doctrine\DBAL\Schema\AbstractAsset::setName" />
65-
66-
<!--
67-
https://github.com/doctrine/dbal/pull/6664
68-
TODO: remove in 5.0.0
69-
-->
70-
<referencedMethod name="Doctrine\DBAL\Schema\AbstractAsset::getNamespaceName" />
71-
<referencedMethod name="Doctrine\DBAL\Schema\AbstractAsset::isInDefaultNamespace" />
7265
</errorLevel>
7366
</DeprecatedMethod>
74-
<DeprecatedProperty>
75-
<errorLevel type="suppress">
76-
<!--
77-
https://github.com/doctrine/dbal/pull/6664
78-
TODO: remove in 5.0.0
79-
-->
80-
<referencedProperty name="Doctrine\DBAL\Schema\AbstractAsset::$_namespace" />
81-
<referencedProperty name="Doctrine\DBAL\Schema\Table::$_namespace" />
82-
</errorLevel>
83-
</DeprecatedProperty>
8467
<DocblockTypeContradiction>
8568
<errorLevel type="suppress">
8669
<!--

src/Schema/AbstractAsset.php

Lines changed: 5 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use Doctrine\DBAL\Schema\Name\OptionallyQualifiedName;
1313
use Doctrine\DBAL\Schema\Name\Parser;
1414
use Doctrine\DBAL\Schema\Name\UnqualifiedName;
15-
use Doctrine\Deprecations\Deprecation;
1615

1716
use function array_map;
1817
use function assert;
@@ -39,13 +38,6 @@ abstract class AbstractAsset
3938
{
4039
protected string $_name = '';
4140

42-
/**
43-
* Namespace of the asset. If none isset the default namespace is assumed.
44-
*
45-
* @deprecated Use {@see NamedObject::getObjectName()} and {@see OptionallyQualifiedName::getQualifier()} instead.
46-
*/
47-
protected ?string $_namespace = null;
48-
4941
protected bool $_quoted = false;
5042

5143
/** @var list<Identifier> */
@@ -87,26 +79,10 @@ public function __construct(string $name)
8779
$count = count($identifiers);
8880
assert($count > 0);
8981

90-
switch ($count) {
91-
case 1:
92-
$namespace = null;
93-
$name = $identifiers[0];
94-
break;
95-
96-
case 2:
97-
/** @psalm-suppress PossiblyUndefinedArrayOffset */
98-
[$namespace, $name] = $identifiers;
99-
break;
100-
101-
default:
102-
$namespace = null;
103-
$name = $identifiers[$count - 1];
104-
break;
105-
}
82+
$name = $identifiers[$count - 1];
10683

10784
$this->_name = $name->getValue();
10885
$this->_quoted = $name->isQuoted();
109-
$this->_namespace = $namespace?->getValue();
11086
$this->identifiers = $identifiers;
11187
}
11288

@@ -134,43 +110,6 @@ protected function setName(?Name $name): void
134110
throw NotImplemented::fromMethod(static::class, __FUNCTION__);
135111
}
136112

137-
/**
138-
* Is this asset in the default namespace?
139-
*
140-
* @deprecated
141-
*/
142-
public function isInDefaultNamespace(string $defaultNamespaceName): bool
143-
{
144-
Deprecation::triggerIfCalledFromOutside(
145-
'doctrine/dbal',
146-
'https://github.com/doctrine/dbal/pull/6664',
147-
'%s is deprecated and will be removed in 5.0.',
148-
__METHOD__,
149-
);
150-
151-
return $this->_namespace === $defaultNamespaceName || $this->_namespace === null;
152-
}
153-
154-
/**
155-
* Gets the namespace name of this asset.
156-
*
157-
* If NULL is returned this means the default namespace is used.
158-
*
159-
* @deprecated Use {@see NamedObject::getObjectName()} and {@see OptionallyQualifiedName::getQualifier()} instead.
160-
*/
161-
public function getNamespaceName(): ?string
162-
{
163-
Deprecation::triggerIfCalledFromOutside(
164-
'doctrine/dbal',
165-
'https://github.com/doctrine/dbal/pull/6664',
166-
'%s is deprecated and will be removed in 5.0. Use NamedObject::getObjectName()'
167-
. ' and OptionallyQualifiedName::getQualifier() instead.',
168-
__METHOD__,
169-
);
170-
171-
return $this->_namespace;
172-
}
173-
174113
/**
175114
* Checks if this asset's name is quoted.
176115
*/
@@ -200,11 +139,10 @@ protected function trimQuotes(string $identifier): string
200139
*/
201140
public function getName(): string
202141
{
203-
if ($this->_namespace !== null) {
204-
return $this->_namespace . '.' . $this->_name;
205-
}
206-
207-
return $this->_name;
142+
return implode('.', array_map(
143+
static fn (Identifier $identifier): string => $identifier->getValue(),
144+
$this->identifiers,
145+
));
208146
}
209147

210148
/**

src/Schema/Schema.php

Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -104,42 +104,53 @@ protected function getNameParser(): UnqualifiedNameParser
104104

105105
protected function _addTable(Table $table): void
106106
{
107-
$namespaceName = $table->getNamespaceName();
108-
$tableName = $this->normalizeName($table);
107+
$name = $table->getObjectName();
109108

110-
if (isset($this->_tables[$tableName])) {
111-
throw TableAlreadyExists::new($tableName);
112-
}
109+
$normalizedName = $this->normalizeName($name);
113110

114-
if (
115-
$namespaceName !== null
116-
&& ! $table->isInDefaultNamespace($this->getName())
117-
&& ! $this->hasNamespace($namespaceName)
118-
) {
119-
$this->createNamespace($namespaceName);
111+
if (isset($this->_tables[$normalizedName])) {
112+
throw TableAlreadyExists::new($normalizedName);
120113
}
121114

122-
$this->_tables[$tableName] = $table;
115+
$this->ensureNamespaceExists($name);
116+
117+
$this->_tables[$normalizedName] = $table;
123118
}
124119

125120
protected function _addSequence(Sequence $sequence): void
126121
{
127-
$namespaceName = $sequence->getNamespaceName();
128-
$seqName = $this->normalizeName($sequence);
122+
$name = $sequence->getObjectName();
123+
124+
$normalizedName = $this->normalizeName($name);
129125

130-
if (isset($this->_sequences[$seqName])) {
131-
throw SequenceAlreadyExists::new($seqName);
126+
if (isset($this->_sequences[$normalizedName])) {
127+
throw SequenceAlreadyExists::new($normalizedName);
132128
}
133129

134-
if (
135-
$namespaceName !== null
136-
&& ! $sequence->isInDefaultNamespace($this->getName())
137-
&& ! $this->hasNamespace($namespaceName)
138-
) {
139-
$this->createNamespace($namespaceName);
130+
$this->ensureNamespaceExists($name);
131+
132+
$this->_sequences[$normalizedName] = $sequence;
133+
}
134+
135+
private function ensureNamespaceExists(OptionallyQualifiedName $name): void
136+
{
137+
$qualifier = $name->getQualifier();
138+
139+
if ($qualifier === null) {
140+
return;
140141
}
141142

142-
$this->_sequences[$seqName] = $sequence;
143+
$namespaceName = $qualifier->getValue();
144+
145+
if ($namespaceName === $this->getName()) {
146+
return;
147+
}
148+
149+
if ($this->hasNamespace($namespaceName)) {
150+
return;
151+
}
152+
153+
$this->createNamespace($namespaceName);
143154
}
144155

145156
/**
@@ -190,16 +201,13 @@ private function getFullQualifiedAssetName(string $name): string
190201
* Foo) then you will NOT be able to use Doctrine Schema abstraction.
191202
*
192203
* Every non-namespaced element is prefixed with this schema name.
193-
*
194-
* @param AbstractAsset<OptionallyQualifiedName> $asset
195204
*/
196-
private function normalizeName(AbstractAsset $asset): string
205+
private function normalizeName(OptionallyQualifiedName $name): string
197206
{
198-
$name = $asset->getName();
207+
$namespaceName = $name->getQualifier()?->getValue()
208+
?? $this->getName();
199209

200-
if ($asset->getNamespaceName() === null) {
201-
$name = $this->getName() . '.' . $name;
202-
}
210+
$name = $namespaceName . '.' . $name->getUnqualifiedName()->getValue();
203211

204212
return strtolower($name);
205213
}
@@ -300,13 +308,10 @@ public function createTable(string $name): Table
300308
*/
301309
public function renameTable(string $oldName, string $newName): self
302310
{
303-
$table = $this->getTable($oldName);
304-
305-
$identifier = new Identifier($newName);
306-
307-
$table->_name = $identifier->_name;
308-
$table->_namespace = $identifier->_namespace;
309-
$table->_quoted = $identifier->_quoted;
311+
$table = $this->getTable($oldName)
312+
->edit()
313+
->setName($newName)
314+
->create();
310315

311316
$this->dropTable($oldName);
312317
$this->_addTable($table);

tests/Schema/SchemaTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,13 @@ public function testCreateTableTwiceThrowsException(): void
7171

7272
public function testRenameTable(): void
7373
{
74-
$tableName = 'foo';
75-
$table = new Table($tableName);
76-
$schema = new Schema([$table]);
74+
$table = new Table('foo');
75+
$schema = new Schema([$table]);
7776

7877
self::assertTrue($schema->hasTable('foo'));
7978
$schema->renameTable('foo', 'bar');
8079
self::assertFalse($schema->hasTable('foo'));
8180
self::assertTrue($schema->hasTable('bar'));
82-
self::assertSame($table, $schema->getTable('bar'));
8381
}
8482

8583
public function testDropTable(): void

0 commit comments

Comments
 (0)