Skip to content

Remove final constructor for Type#6705

Merged
greg0ire merged 4 commits intodoctrine:4.3.xfrom
ruudk:ruudk/2025/1/remove-final-type-constructor
Feb 20, 2025
Merged

Remove final constructor for Type#6705
greg0ire merged 4 commits intodoctrine:4.3.xfrom
ruudk:ruudk/2025/1/remove-final-type-constructor

Conversation

@ruudk
Copy link
Contributor

@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
Contributor 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
Contributor 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

## 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
Contributor 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.

*
* @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
Contributor 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!

}

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 2 times, most recently from e466fd8 to f9917a1 Compare February 11, 2025 07:47
@ruudk
Copy link
Contributor 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.

@ruudk
Copy link
Contributor Author

ruudk commented Feb 11, 2025

@GromNaN thanks, applied your feedback!

@ruudk ruudk requested a review from GromNaN February 11, 2025 08:28
@greg0ire
Copy link
Member

@ruudk static analysis is failing

@ruudk ruudk force-pushed the ruudk/2025/1/remove-final-type-constructor branch from 4d5934e to a3db335 Compare February 19, 2025 06:47
@ruudk
Copy link
Contributor Author

ruudk commented Feb 19, 2025

@greg0ire Fixed them, please approve the workflow.

Copy link
Member

@SenseException SenseException left a comment

Choose a reason for hiding this comment

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

Okay, but what happened to the deprecation?

@ruudk
Copy link
Contributor Author

ruudk commented Feb 20, 2025

@SenseException See #6705 (comment). There a solution was proposed that would allow me to not deal with the depreciation.

Deprecating it yes or no is now a task for the future. Not required for this feature.

@greg0ire greg0ire added this to the 4.3.0 milestone Feb 20, 2025
@greg0ire
Copy link
Member

Thanks @ruudk !

@greg0ire greg0ire merged commit a5ac9e2 into doctrine:4.3.x Feb 20, 2025
67 checks passed
@ruudk ruudk deleted the ruudk/2025/1/remove-final-type-constructor branch February 20, 2025 12:24
@ruudk
Copy link
Contributor Author

ruudk commented Jul 4, 2025

@greg0ire Is there an ETA for 4.3.0? I see the milestone is 100%. I would really like to use this constructor 😁

@derrabus
Copy link
Member

derrabus commented Jul 4, 2025

Not yet, but I wouldn't want to wait too much longer either. 😃

@SerafimArts
Copy link

SerafimArts commented Dec 23, 2025

Thank you a lot guys! This finally made it possible to register custom Doctrine types in the Symfony Container and use them from there. I can't express my gratitude for the work done!

@ruudk
Copy link
Contributor Author

ruudk commented Dec 23, 2025

Thanks!

I wish DoctrineBundle would make it a lot easier to register types as services now that this is in place.

@GromNaN
Copy link
Member

GromNaN commented Dec 23, 2025

For the MongoDB ODM Bundle, I'm adding a new attribute #[AsFieldType]: https://github.com/docine/DoctrineMongoDBBundle/pull/963

This is something that could be done in the same way in Doctrine bundle; with a new tag and an attribute #[AsColumnType].

@ruudk
Copy link
Contributor Author

ruudk commented Dec 23, 2025

That would be amazing!

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.

7 participants