From 2331fd2e12d84b4d02b1398d772c37691e2ed5df Mon Sep 17 00:00:00 2001 From: Ruud Kamphuis Date: Mon, 13 Jan 2025 14:09:14 +0100 Subject: [PATCH 1/3] Remove final constructor for Type See #6704 --- .../Exception/TypeArgumentCountError.php | 19 ++++++++++++++++++ src/Types/Type.php | 13 ++++++------ tests/Types/TypeTest.php | 10 ++++++++++ tests/Types/TypeWithConstructor.php | 20 +++++++++++++++++++ 4 files changed, 56 insertions(+), 6 deletions(-) create mode 100644 src/Types/Exception/TypeArgumentCountError.php create mode 100644 tests/Types/TypeWithConstructor.php diff --git a/src/Types/Exception/TypeArgumentCountError.php b/src/Types/Exception/TypeArgumentCountError.php new file mode 100644 index 00000000000..2fda22f26f2 --- /dev/null +++ b/src/Types/Exception/TypeArgumentCountError.php @@ -0,0 +1,19 @@ +register instead.', $name), previous: $previous); + } +} diff --git a/src/Types/Type.php b/src/Types/Type.php index f8d218fb753..d6317603712 100644 --- a/src/Types/Type.php +++ b/src/Types/Type.php @@ -4,9 +4,11 @@ namespace Doctrine\DBAL\Types; +use ArgumentCountError; use Doctrine\DBAL\Exception; use Doctrine\DBAL\ParameterType; use Doctrine\DBAL\Platforms\AbstractPlatform; +use Doctrine\DBAL\Types\Exception\TypeArgumentCountError; use function array_map; @@ -51,11 +53,6 @@ abstract class Type private static ?TypeRegistry $typeRegistry = null; - /** @internal Do not instantiate directly - use {@see Type::addType()} method instead. */ - final public function __construct() - { - } - /** * Converts a value from its PHP representation to its database representation * of this type. @@ -144,7 +141,11 @@ public static function lookupName(self $type): string */ public static function addType(string $name, string $className): void { - self::getTypeRegistry()->register($name, new $className()); + try { + self::getTypeRegistry()->register($name, new $className()); + } catch (ArgumentCountError $e) { + throw TypeArgumentCountError::new($name, $e); + } } /** diff --git a/tests/Types/TypeTest.php b/tests/Types/TypeTest.php index 3142576f668..aaa8f697a02 100644 --- a/tests/Types/TypeTest.php +++ b/tests/Types/TypeTest.php @@ -4,6 +4,8 @@ namespace Doctrine\DBAL\Tests\Types; +use Doctrine\DBAL\Tests\Functional\Schema\Types\MoneyType; +use Doctrine\DBAL\Types\Exception\TypeArgumentCountError; use Doctrine\DBAL\Types\Type; use Doctrine\DBAL\Types\Types; use PHPUnit\Framework\Attributes\DataProvider; @@ -39,4 +41,12 @@ public static function defaultTypesProvider(): iterable yield [$constantValue]; } } + + public function testAddTypeWhenTypeRequiresArguments(): void + { + self::expectException(TypeArgumentCountError::class); + self::expectExceptionMessage('Type "some_type" cannot be registered through Type::addType because it requires arguments. Use Type::getTypeRegistry()->register instead.'); + + Type::addType('some_type', TypeWithConstructor::class); + } } diff --git a/tests/Types/TypeWithConstructor.php b/tests/Types/TypeWithConstructor.php new file mode 100644 index 00000000000..a8fe0bbdab0 --- /dev/null +++ b/tests/Types/TypeWithConstructor.php @@ -0,0 +1,20 @@ + Date: Thu, 16 Jan 2025 09:28:11 +0100 Subject: [PATCH 2/3] Deprecate Type::addType --- UPGRADE.md | 4 ++++ src/Types/Type.php | 2 ++ tests/Functional/Schema/CustomIntrospectionTest.php | 2 +- tests/Functional/Schema/MySQLSchemaManagerTest.php | 4 ++-- tests/Functional/Schema/PostgreSQLSchemaManagerTest.php | 2 +- tests/Platforms/AbstractPlatformTestCase.php | 2 +- 6 files changed, 11 insertions(+), 5 deletions(-) diff --git a/UPGRADE.md b/UPGRADE.md index 15f8256f55c..bb2217da13f 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -8,6 +8,10 @@ awareness about deprecated code. # Upgrade to 4.3 +## Deprecated Type::addType + +Use Type::getTypeRegistry()->register() instead. + ## Deprecated `Table::columnsAreIndexed()` The `Table::columnsAreIndexed()` method has been deprecated. diff --git a/src/Types/Type.php b/src/Types/Type.php index d6317603712..be052a0c3ad 100644 --- a/src/Types/Type.php +++ b/src/Types/Type.php @@ -138,6 +138,8 @@ public static function lookupName(self $type): string * @param class-string $className The class name of the custom type. * * @throws Exception + * + * @deprecated Use Type::getTypeRegistry()->register() instead. */ public static function addType(string $name, string $className): void { diff --git a/tests/Functional/Schema/CustomIntrospectionTest.php b/tests/Functional/Schema/CustomIntrospectionTest.php index a65cbb4cecd..9061d041d1e 100644 --- a/tests/Functional/Schema/CustomIntrospectionTest.php +++ b/tests/Functional/Schema/CustomIntrospectionTest.php @@ -29,7 +29,7 @@ public static function setUpBeforeClass(): void self::markTestSkipped('Skip on Oracle'); } - Type::addType(MoneyType::NAME, MoneyType::class); + Type::getTypeRegistry()->register(MoneyType::NAME, new MoneyType); } public function testCustomColumnIntrospection(): void diff --git a/tests/Functional/Schema/MySQLSchemaManagerTest.php b/tests/Functional/Schema/MySQLSchemaManagerTest.php index 893f673ca3a..ab3d2e7c6df 100644 --- a/tests/Functional/Schema/MySQLSchemaManagerTest.php +++ b/tests/Functional/Schema/MySQLSchemaManagerTest.php @@ -27,7 +27,7 @@ class MySQLSchemaManagerTest extends SchemaManagerFunctionalTestCase { public static function setUpBeforeClass(): void { - Type::addType('point', PointType::class); + Type::getTypeRegistry()->register('point', new PointType); } protected function supportsPlatform(AbstractPlatform $platform): bool @@ -591,7 +591,7 @@ public function testListTableColumnsThrowsDatabaseRequired(): void public function testSchemaDiffWithCustomColumnTypeWhileDatabaseTypeDiffers(): void { - Type::addType('custom_type', CustomType::class); + Type::getTypeRegistry()->register('custom_type', new CustomType); $metadataTable = new Table('table_with_custom_type'); $metadataTable->addColumn('col1', 'custom_type'); diff --git a/tests/Functional/Schema/PostgreSQLSchemaManagerTest.php b/tests/Functional/Schema/PostgreSQLSchemaManagerTest.php index 591340c8864..5e352948a2e 100644 --- a/tests/Functional/Schema/PostgreSQLSchemaManagerTest.php +++ b/tests/Functional/Schema/PostgreSQLSchemaManagerTest.php @@ -53,7 +53,7 @@ public function testSupportDomainTypeFallback(): void $table = $this->connection->createSchemaManager()->introspectTable('domain_type_test'); self::assertInstanceOf(DecimalType::class, $table->getColumn('value')->getType()); - Type::addType('MyMoney', MoneyType::class); + Type::getTypeRegistry()->register('MyMoney', new MoneyType); $this->connection->getDatabasePlatform()->registerDoctrineTypeMapping('MyMoney', 'MyMoney'); $table = $this->connection->createSchemaManager()->introspectTable('domain_type_test'); diff --git a/tests/Platforms/AbstractPlatformTestCase.php b/tests/Platforms/AbstractPlatformTestCase.php index d53da6f5cc1..39cfe339b28 100644 --- a/tests/Platforms/AbstractPlatformTestCase.php +++ b/tests/Platforms/AbstractPlatformTestCase.php @@ -117,7 +117,7 @@ public function getSQLDeclaration(array $column, AbstractPlatform $platform): st if (Type::hasType($type->getName())) { Type::overrideType($type->getName(), $type::class); } else { - Type::addType($type->getName(), $type::class); + Type::getTypeRegistry()->register($type->getName(), new $type); } self::assertSame($type->getName(), $this->platform->getDoctrineTypeMapping('TeStTyPe')); From ae1c6be14c55f86dc4c611cd34729fbdecad472c Mon Sep 17 00:00:00 2001 From: Ruud Kamphuis Date: Thu, 16 Jan 2025 09:51:16 +0100 Subject: [PATCH 3/3] Update UPGRADE.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Grégoire Paris --- UPGRADE.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/UPGRADE.md b/UPGRADE.md index bb2217da13f..385b69880c4 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -10,7 +10,7 @@ awareness about deprecated code. ## Deprecated Type::addType -Use Type::getTypeRegistry()->register() instead. +Use `Type::getTypeRegistry()->register()` instead. ## Deprecated `Table::columnsAreIndexed()`