-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
base: 4.3.x
Are you sure you want to change the base?
Remove final constructor for Type #6705
Conversation
Note: this is another take on #5528 |
I've never used custom types as besides schema management, they are primarily designed for the ORM. I'll defer to Alexander. |
If we do that, we turn |
How will it be a potential footgun? The current desired behavior is guarded, so I don't see how.
I agree, but that can be done independently from this PR, no? |
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
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. |
e6b5c83
to
355887c
Compare
There was a problem hiding this 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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use Type::getTypeRegistry()->register() instead. | |
Use `Type::getTypeRegistry()->register()` instead. |
The docs should be updated, they mention |
2c1e7df
to
b3b1dac
Compare
@greg0ire I made some changes. Could you approve the workflow so we can see the test results? |
There was a problem hiding this 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.
src/Types/Type.php
Outdated
@@ -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 |
There was a problem hiding this comment.
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.
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
.
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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:
Type::getTypeRegistry()->register(MoneyType::NAME, new MoneyType); | |
Type::addType(MoneyType::NAME, new MoneyType()); |
b3b1dac
to
e466fd8
Compare
e466fd8
to
f9917a1
Compare
There was a problem hiding this 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.
@GromNaN thanks, applied your feedback! |
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.