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

Deprecate Type::getName() #5049

Merged
merged 1 commit into from
Jan 26, 2022
Merged

Conversation

greg0ire
Copy link
Member

@greg0ire greg0ire commented Nov 28, 2021

Q A
Type improvement
BC Break no
Fixed issues n/a

Summary

The consequence of this is that it no longer need to be implemented in
child classes. This means users no longer need to make sure the name
they use in getName() is the same they use when registering the type.

Type already has a dependency on TypeRegistry, which should make this
change OK and should not make it harder to get rid of getName() entirely
in the future.

Apart from rare scenarios where one would use a type without registering
it, that implementation should work reliably. That scenario happens
quite frequently in tests, which is why there were some changes in the
test suite.

This should unlock #5036 without requiring we get rid of getName() entirely, which would be a bigger endeavor.

Note: the ORM has docs on types: https://www.doctrine-project.org/projects/doctrine-orm/en/2.10/cookbook/custom-mapping-types.html#custom-mapping-types

They will probably need to be replaced with a link to the DBAL docs otherwise things are going to get confusing, especially if there are more consequential refactorings in the type system.

@greg0ire greg0ire requested a review from morozov November 28, 2021 21:52
@greg0ire greg0ire marked this pull request as draft November 28, 2021 21:56
@greg0ire greg0ire force-pushed the stop-requiring-getName branch 3 times, most recently from 114311a to 57ef469 Compare November 28, 2021 22:18
{
return Types::ARRAY;
}

Copy link
Member Author

Choose a reason for hiding this comment

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

The only possible BC-break in this PR is removing getName() from existing types. It will not work if the type is not registered, or is registered with a different name (which is not supported). If we think this is too risky, it can be reverted without loosing the benefits of having a generic getName() implementation.

Obviously, I think it should be fine.

Copy link
Member

Choose a reason for hiding this comment

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

I'd revert it. This code itself doesn't cause any problems to be removed in a minor release.

@greg0ire greg0ire marked this pull request as ready for review November 28, 2021 22:31
@greg0ire greg0ire requested a review from derrabus November 28, 2021 22:31
@greg0ire greg0ire force-pushed the stop-requiring-getName branch from 57ef469 to d5e4408 Compare November 28, 2021 22:49
abstract public function getName();
public function getName()
{
return self::getTypeRegistry()->lookupName($this);
Copy link
Member

Choose a reason for hiding this comment

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

The introduction of a dependency of a value on a registry doesn't look right, even temporarily. If we want to deprecate and eventually get rid of Type::getName(), we should do this.

In its current state, this pull request removes some code at a cost of introducing a risk of BC break.

Instead, what if we should identify the scenarios that rely on Type::getName() and deprecate them. For instance, deprecate registering a type with a name different than the value returned by Type::getName().

Copy link
Member Author

@greg0ire greg0ire Nov 29, 2021

Choose a reason for hiding this comment

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

The introduction of a dependency of a value on a registry doesn't look right, even temporarily. If we want to deprecate and eventually get rid of Type::getName(), we should do this.

I don't understand this paragraph. What value are you referring to? Did you mean to write "we should not do this."?

If we were to drop Type::getName(), we would probably replace calls to it by Type::getTypeRegistry()->lookupName($type), wouldn't we?

Copy link
Member

Choose a reason for hiding this comment

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

Sorry for the confusion. I was referring to the return value of Type::getName().

Did you mean to write "we should not do this."?

I meant that if we want to deprecate and eventually get rid of Type::getName(), we should do that (provide the upgrade path and deprecate it).

If we were to drop Type::getName(), we would probably replace calls to it by Type::getTypeRegistry()->lookupName($type), wouldn't we?

If we have a type registered under a name, we can use the type name in the API and look up the type by name. Why do we need to do the reverse lookup? What does it even mean given that the types will be no longer flyweights?

Copy link
Member Author

Choose a reason for hiding this comment

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

If we have a type registered under a name, we can use the type name in the API and look up the type by name. Why do we need to do the reverse lookup?

Well for instance here:

public function isCommentedDoctrineType(Type $doctrineType)
{
if ($this->doctrineTypeComments === null) {
$this->initializeCommentedDoctrineTypes();
}
assert(is_array($this->doctrineTypeComments));
return in_array($doctrineType->getName(), $this->doctrineTypeComments, true);
}

we have access to the type instance, but not to its name in the registry, so we would have to change the signature of this method to also provide the name, which will be a BC-break, and we will also need to change the calling methods if it does not have access to the name, etc. etc.

Or maybe we should use spl_object_id to identify the Type instance in that particular scenario, I don't know. I guess we would have to check for each scenario if we have an alternate why of fulfilling it.

Copy link
Member

Choose a reason for hiding this comment

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

Why can't we just call $type->requiresSQLCommentHint($this)?

@@ -21,7 +24,9 @@ class ArrayTest extends TestCase
protected function setUp(): void
{
$this->platform = $this->createMock(AbstractPlatform::class);
$this->type = new ArrayType();
$type = Type::getType(Types::ARRAY);
Copy link
Member

Choose a reason for hiding this comment

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

This is a step backward, IMO. Testing a type shouldn't require the usage of a type registry.

@derrabus derrabus changed the base branch from 3.3.x to 3.4.x January 18, 2022 00:43
@greg0ire greg0ire force-pushed the stop-requiring-getName branch 2 times, most recently from 6bea2fe to e0f4e26 Compare January 26, 2022 08:25
@greg0ire greg0ire changed the title Stop requiring users implement Type::getName() Deprecate Type::getName() Jan 26, 2022
@greg0ire greg0ire force-pushed the stop-requiring-getName branch 2 times, most recently from 44fa46d to 59d4ba5 Compare January 26, 2022 11:16
It serves little to no purpose now.
@greg0ire greg0ire force-pushed the stop-requiring-getName branch from 59d4ba5 to 8910d85 Compare January 26, 2022 12:23
@greg0ire greg0ire requested a review from morozov January 26, 2022 13:07
@greg0ire greg0ire added this to the 3.4.0 milestone Jan 26, 2022
@greg0ire greg0ire merged commit 41d6f44 into doctrine:3.4.x Jan 26, 2022
@greg0ire greg0ire deleted the stop-requiring-getName branch January 26, 2022 16:05
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 27, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants