Skip to content

Commit bea4814

Browse files
committed
Merge branch '3.5.x' into 3.6.x
* 3.5.x: Prefer non-deprecated AbstractAsset API (#12142)
2 parents 238c159 + 85c13ed commit bea4814

File tree

4 files changed

+64
-28
lines changed

4 files changed

+64
-28
lines changed

phpstan-baseline.neon

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1374,12 +1374,6 @@ parameters:
13741374
count: 2
13751375
path: src/Mapping/Driver/DatabaseDriver.php
13761376

1377-
-
1378-
message: '#^Cannot call method getName\(\) on Doctrine\\DBAL\\Schema\\Column\|false\.$#'
1379-
identifier: method.nonObject
1380-
count: 1
1381-
path: src/Mapping/Driver/DatabaseDriver.php
1382-
13831377
-
13841378
message: '#^Instanceof between Doctrine\\ORM\\Mapping\\ClassMetadata\<T of object\> and Doctrine\\ORM\\Mapping\\ClassMetadata will always evaluate to true\.$#'
13851379
identifier: instanceof.alwaysTrue
@@ -1410,12 +1404,24 @@ parameters:
14101404
count: 1
14111405
path: src/Mapping/Driver/DatabaseDriver.php
14121406

1407+
-
1408+
message: '#^Method Doctrine\\ORM\\Mapping\\Driver\\DatabaseDriver\:\:getAssetName\(\) has parameter \$asset with generic class Doctrine\\DBAL\\Schema\\AbstractAsset but does not specify its types\: N$#'
1409+
identifier: missingType.generics
1410+
count: 1
1411+
path: src/Mapping/Driver/DatabaseDriver.php
1412+
14131413
-
14141414
message: '#^Method Doctrine\\ORM\\Mapping\\Driver\\DatabaseDriver\:\:getClassNameForTable\(\) should return class\-string but returns string\.$#'
14151415
identifier: return.type
14161416
count: 2
14171417
path: src/Mapping/Driver/DatabaseDriver.php
14181418

1419+
-
1420+
message: '#^Parameter \#1 \$asset of static method Doctrine\\ORM\\Mapping\\Driver\\DatabaseDriver\:\:getAssetName\(\) expects Doctrine\\DBAL\\Schema\\AbstractAsset, Doctrine\\DBAL\\Schema\\Column\|false given\.$#'
1421+
identifier: argument.type
1422+
count: 1
1423+
path: src/Mapping/Driver/DatabaseDriver.php
1424+
14191425
-
14201426
message: '#^Parameter \#2 \$columnName of method Doctrine\\ORM\\Mapping\\Driver\\DatabaseDriver\:\:getFieldNameForColumn\(\) expects string, string\|false given\.$#'
14211427
identifier: argument.type
@@ -3231,6 +3237,12 @@ parameters:
32313237
count: 1
32323238
path: src/Tools/SchemaTool.php
32333239

3240+
-
3241+
message: '#^Method Doctrine\\ORM\\Tools\\SchemaTool\:\:getAssetName\(\) has parameter \$asset with generic class Doctrine\\DBAL\\Schema\\AbstractAsset but does not specify its types\: N$#'
3242+
identifier: missingType.generics
3243+
count: 1
3244+
path: src/Tools/SchemaTool.php
3245+
32343246
-
32353247
message: '#^Method Doctrine\\ORM\\Tools\\SchemaTool\:\:getCreateSchemaSql\(\) has parameter \$classes with generic class Doctrine\\ORM\\Mapping\\ClassMetadata but does not specify its types\: T$#'
32363248
identifier: missingType.generics

phpstan-dbal3.neon

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,12 @@ parameters:
7979
message: '~^Call to method toString.*UnqualifiedName\.$~'
8080
path: src/Tools/SchemaTool.php
8181

82+
- '~^Call to method getObjectName\(\) on an unknown class Doctrine\\DBAL\\Schema\\NamedObject\.$~'
83+
8284
- '~^Class Doctrine\\DBAL\\Platforms\\SQLitePlatform not found\.$~'
8385

86+
- '~^Class Doctrine\\DBAL\\Schema\\NamedObject not found\.$~'
87+
8488
-
8589
message: '~sort~'
8690
identifier: argument.unresolvableType

src/Mapping/Driver/DatabaseDriver.php

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44

55
namespace Doctrine\ORM\Mapping\Driver;
66

7+
use Doctrine\DBAL\Schema\AbstractAsset;
78
use Doctrine\DBAL\Schema\AbstractSchemaManager;
89
use Doctrine\DBAL\Schema\Column;
910
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
1011
use Doctrine\DBAL\Schema\Index;
1112
use Doctrine\DBAL\Schema\Index\IndexedColumn;
1213
use Doctrine\DBAL\Schema\Index\IndexType;
1314
use Doctrine\DBAL\Schema\Name\UnqualifiedName;
15+
use Doctrine\DBAL\Schema\NamedObject;
1416
use Doctrine\DBAL\Schema\PrimaryKeyConstraint;
1517
use Doctrine\DBAL\Schema\SchemaException;
1618
use Doctrine\DBAL\Schema\Table;
@@ -143,14 +145,14 @@ public function setTables(array $entityTables, array $manyToManyTables): void
143145
$this->tables = $this->manyToManyTables = $this->classToTableNames = [];
144146

145147
foreach ($entityTables as $table) {
146-
$className = $this->getClassNameForTable($table->getName());
148+
$className = $this->getClassNameForTable(self::getAssetName($table));
147149

148-
$this->classToTableNames[$className] = $table->getName();
149-
$this->tables[$table->getName()] = $table;
150+
$this->classToTableNames[$className] = self::getAssetName($table);
151+
$this->tables[self::getAssetName($table)] = $table;
150152
}
151153

152154
foreach ($manyToManyTables as $table) {
153-
$this->manyToManyTables[$table->getName()] = $table;
155+
$this->manyToManyTables[self::getAssetName($table)] = $table;
154156
}
155157
}
156158

@@ -219,13 +221,13 @@ public function loadMetadataForClass(string $className, PersistenceClassMetadata
219221
$localColumn = current(self::getReferencingColumnNames($myFk));
220222

221223
$associationMapping = [];
222-
$associationMapping['fieldName'] = $this->getFieldNameForColumn($manyTable->getName(), current(self::getReferencingColumnNames($otherFk)), true);
224+
$associationMapping['fieldName'] = $this->getFieldNameForColumn(self::getAssetName($manyTable), current(self::getReferencingColumnNames($otherFk)), true);
223225
$associationMapping['targetEntity'] = $this->getClassNameForTable(self::getReferencedTableName($otherFk));
224226

225-
if (current($manyTable->getColumns())->getName() === $localColumn) {
226-
$associationMapping['inversedBy'] = $this->getFieldNameForColumn($manyTable->getName(), current(self::getReferencingColumnNames($myFk)), true);
227+
if (self::getAssetName(current($manyTable->getColumns())) === $localColumn) {
228+
$associationMapping['inversedBy'] = $this->getFieldNameForColumn(self::getAssetName($manyTable), current(self::getReferencingColumnNames($myFk)), true);
227229
$associationMapping['joinTable'] = [
228-
'name' => strtolower($manyTable->getName()),
230+
'name' => strtolower(self::getAssetName($manyTable)),
229231
'joinColumns' => [],
230232
'inverseJoinColumns' => [],
231233
];
@@ -270,7 +272,7 @@ private function reverseEngineerMappingFromDatabase(): void
270272
$this->tables = $this->manyToManyTables = $this->classToTableNames = [];
271273

272274
foreach ($this->sm->listTables() as $table) {
273-
$tableName = $table->getName();
275+
$tableName = self::getAssetName($table);
274276
$foreignKeys = $table->getForeignKeys();
275277

276278
$allForeignKeyColumns = [];
@@ -335,7 +337,7 @@ private function buildIndexes(ClassMetadata $metadata): void
335337
$isUnique = $index->isUnique();
336338
}
337339

338-
$indexName = $index->getName();
340+
$indexName = self::getAssetName($index);
339341
$indexColumns = self::getIndexedColumns($index);
340342
$constraintType = $isUnique
341343
? 'uniqueConstraints'
@@ -364,13 +366,13 @@ private function buildFieldMappings(ClassMetadata $metadata): void
364366
$fieldMappings = [];
365367

366368
foreach ($columns as $column) {
367-
if (in_array($column->getName(), $allForeignKeys, true)) {
369+
if (in_array(self::getAssetName($column), $allForeignKeys, true)) {
368370
continue;
369371
}
370372

371373
$fieldMapping = $this->buildFieldMapping($tableName, $column);
372374

373-
if ($primaryKeys && in_array($column->getName(), $primaryKeys, true)) {
375+
if ($primaryKeys && in_array(self::getAssetName($column), $primaryKeys, true)) {
374376
$fieldMapping['id'] = true;
375377
$ids[] = $fieldMapping;
376378
}
@@ -411,8 +413,8 @@ private function buildFieldMappings(ClassMetadata $metadata): void
411413
private function buildFieldMapping(string $tableName, Column $column): array
412414
{
413415
$fieldMapping = [
414-
'fieldName' => $this->getFieldNameForColumn($tableName, $column->getName(), false),
415-
'columnName' => $column->getName(),
416+
'fieldName' => $this->getFieldNameForColumn($tableName, self::getAssetName($column), false),
417+
'columnName' => self::getAssetName($column),
416418
'type' => Type::getTypeRegistry()->lookupName($column->getType()),
417419
'nullable' => ! $column->getNotnull(),
418420
'options' => [
@@ -619,4 +621,12 @@ private static function getPrimaryKey(Table $table): Index|null
619621

620622
return null;
621623
}
624+
625+
private static function getAssetName(AbstractAsset $asset): string
626+
{
627+
return $asset instanceof NamedObject
628+
? $asset->getObjectName()->toString()
629+
// DBAL < 4.4
630+
: $asset->getName();
631+
}
622632
}

src/Tools/SchemaTool.php

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Doctrine\DBAL\Schema\Index\IndexedColumn;
1515
use Doctrine\DBAL\Schema\Name\Identifier;
1616
use Doctrine\DBAL\Schema\Name\UnqualifiedName;
17+
use Doctrine\DBAL\Schema\NamedObject;
1718
use Doctrine\DBAL\Schema\PrimaryKeyConstraint;
1819
use Doctrine\DBAL\Schema\Schema;
1920
use Doctrine\DBAL\Schema\Table;
@@ -735,7 +736,7 @@ private function gatherRelationJoinColumns(
735736
$theJoinTable->addUniqueIndex($unique['columns'], is_numeric($indexName) ? null : $indexName);
736737
}
737738

738-
$compositeName = $theJoinTable->getName() . '.' . implode('', $localColumns);
739+
$compositeName = $this->getAssetName($theJoinTable) . '.' . implode('', $localColumns);
739740
if (
740741
isset($addedFks[$compositeName])
741742
&& ($foreignTableName !== $addedFks[$compositeName]['foreignTableName']
@@ -859,15 +860,15 @@ public function getDropSchemaSQL(array $classes): array
859860
$deployedSchema = $this->schemaManager->introspectSchema();
860861

861862
foreach ($schema->getTables() as $table) {
862-
if (! $deployedSchema->hasTable($table->getName())) {
863-
$schema->dropTable($table->getName());
863+
if (! $deployedSchema->hasTable($this->getAssetName($table))) {
864+
$schema->dropTable($this->getAssetName($table));
864865
}
865866
}
866867

867868
if ($this->platform->supportsSequences()) {
868869
foreach ($schema->getSequences() as $sequence) {
869-
if (! $deployedSchema->hasSequence($sequence->getName())) {
870-
$schema->dropSequence($sequence->getName());
870+
if (! $deployedSchema->hasSequence($this->getAssetName($sequence))) {
871+
$schema->dropSequence($this->getAssetName($sequence));
871872
}
872873
}
873874

@@ -889,7 +890,7 @@ public function getDropSchemaSQL(array $classes): array
889890
}
890891

891892
if (count($columns) === 1) {
892-
$checkSequence = $table->getName() . '_' . $columns[0] . '_seq';
893+
$checkSequence = $this->getAssetName($table) . '_' . $columns[0] . '_seq';
893894
if ($deployedSchema->hasSequence($checkSequence) && ! $schema->hasSequence($checkSequence)) {
894895
$schema->createSequence($checkSequence);
895896
}
@@ -955,8 +956,9 @@ private function createSchemaForComparison(Schema $toSchema): Schema
955956
}
956957

957958
// whitelist assets we already know about in $toSchema, use the existing filter otherwise
958-
$config->setSchemaAssetsFilter(static function ($asset) use ($previousFilter, $toSchema): bool {
959-
$assetName = $asset instanceof AbstractAsset ? $asset->getName() : $asset;
959+
$getAssetName = $this->getAssetName(...);
960+
$config->setSchemaAssetsFilter(static function ($asset) use ($previousFilter, $toSchema, $getAssetName): bool {
961+
$assetName = $asset instanceof AbstractAsset ? $getAssetName($asset) : $asset;
960962

961963
return $toSchema->hasTable($assetName) || $toSchema->hasSequence($assetName) || $previousFilter($asset);
962964
});
@@ -994,4 +996,12 @@ private static function getIndexedColumns(Index $index): array
994996

995997
return $index->getColumns();
996998
}
999+
1000+
private function getAssetName(AbstractAsset $asset): string
1001+
{
1002+
return $asset instanceof NamedObject
1003+
? $asset->getObjectName()->toString()
1004+
// DBAL < 4.4
1005+
: $asset->getName();
1006+
}
9971007
}

0 commit comments

Comments
 (0)