Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove final constructor for Type #6705

Open
wants to merge 3 commits into
base: 4.3.x
Choose a base branch
from

Conversation

ruudk
Copy link

@ruudk ruudk commented Jan 13, 2025

Q A
Type improvement
Fixed issues #6704

Summary

This allows people to define a constructor when creating custom Doctrine\DBAL\Types\Type implementations.

They can be registered by passing an instance to Type::addType.

When a class string is passed, and the Type cannot be instantiated, an helpful error message will be thrown.

@greg0ire
Copy link
Member

Note: this is another take on #5528

@morozov
Copy link
Member

morozov commented Jan 13, 2025

I've never used custom types as besides schema management, they are primarily designed for the ORM. I'll defer to Alexander.

@derrabus
Copy link
Member

If we do that, we turn Type::addType() into a potential footgun. Maybe it's time to deprecate the static methods on the Type class in favor of the TypeRegistry?

@ruudk
Copy link
Author

ruudk commented Jan 14, 2025

If we do that, we turn Type::addType() into a potential footgun.

How will it be a potential footgun? The current desired behavior is guarded, so I don't see how.

Maybe it's time to deprecate the static methods on the Type class in favor of the TypeRegistry?

I agree, but that can be done independently from this PR, no?

@derrabus
Copy link
Member

If we do that, we turn Type::addType() into a potential footgun.

How will it be a potential footgun? The current desired behavior is guarded, so I don't see how.

Yes, you've done all you could to give the developer proper feedback. Yet, we're about to create a class of types that cannot be registered with Type::addType() anymore, an issue which is only discoverable at runtime.

Maybe it's time to deprecate the static methods on the Type class in favor of the TypeRegistry?

I agree, but that can be done independently from this PR, no?

Sure, but still we should talk about it. We somehow ended up with an only partially implemented feature and I want us to work towards a proper solution this time instead of adding a small hack on top to make it somewhat work.

@ruudk ruudk force-pushed the ruudk/2025/1/remove-final-type-constructor branch from e6b5c83 to 355887c Compare January 16, 2025 08:29
@ruudk
Copy link
Author

ruudk commented Jan 16, 2025

@derrabus @greg0ire You're right. Let's deprecate Type::addType immediately. Updated the PR.

Copy link
Member

@greg0ire greg0ire left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems the tests I wrote in my initial PR were not sufficient to prove it's possible to do what's in the documentation. Maybe they should be improved?

UPGRADE.md Outdated
@@ -8,6 +8,10 @@ awareness about deprecated code.

# Upgrade to 4.3

## Deprecated Type::addType

Use Type::getTypeRegistry()->register() instead.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Use Type::getTypeRegistry()->register() instead.
Use `Type::getTypeRegistry()->register()` instead.

@greg0ire
Copy link
Member

The docs should be updated, they mention addType.

@ruudk ruudk force-pushed the ruudk/2025/1/remove-final-type-constructor branch 2 times, most recently from 2c1e7df to b3b1dac Compare February 7, 2025 14:22
@ruudk
Copy link
Author

ruudk commented Feb 7, 2025

@greg0ire I made some changes. Could you approve the workflow so we can see the test results?

Copy link
Member

@GromNaN GromNaN left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some suggestion to keep the API consistent.

Injecting the TypeRegistry everywhere might be a long-term project that involves a lot of changes in other Doctrine packages.

@@ -141,10 +138,16 @@ public static function lookupName(self $type): string
* @param class-string<Type> $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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could keep this method if the arg type were widened.

Suggested change
public static function addType(string $name, string $className): void
public static function addType(string $name, string|Type $type): void

Either we keep this API, or we deprecate all the static methods of this class (getType, hasType, lookupName, overrideType, getTypesMap), which would have a large impact on the Doctrine related packages.

If you don't want to change the signature of this method, you can add setType(string $name, Type $type, bool $override = false).

Note that the DoctrineBundle would use the optional "override" in ConnectionFactory.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it, I pushed the changes. Much simpler now!

@@ -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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The API could be as simple as this:

Suggested change
Type::getTypeRegistry()->register(MoneyType::NAME, new MoneyType);
Type::addType(MoneyType::NAME, new MoneyType());

@ruudk ruudk force-pushed the ruudk/2025/1/remove-final-type-constructor branch from b3b1dac to e466fd8 Compare February 11, 2025 07:45
@ruudk ruudk force-pushed the ruudk/2025/1/remove-final-type-constructor branch from e466fd8 to f9917a1 Compare February 11, 2025 07:47
@ruudk
Copy link
Author

ruudk commented Feb 11, 2025

@GromNaN @greg0ire Updated the PR. I think this is a simpler approach. WDYT?

@ruudk ruudk requested review from GromNaN and greg0ire February 11, 2025 07:48
Copy link
Member

@GromNaN GromNaN left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The approach seems correct at first.

Ideally, someone should take care of injecting the TypeRegistry everywhere the Type::getType() is used.

src/Types/Exception/TypeArgumentCountError.php Outdated Show resolved Hide resolved
src/Types/Type.php Outdated Show resolved Hide resolved
src/Types/Type.php Outdated Show resolved Hide resolved
@ruudk
Copy link
Author

ruudk commented Feb 11, 2025

@GromNaN thanks, applied your feedback!

@ruudk ruudk requested a review from GromNaN February 11, 2025 08:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants