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

Add ClassMetadataFactoryInterface to allow switching implementation #11497

Open
wants to merge 1 commit into
base: 3.2.x
Choose a base branch
from

Conversation

alekitto
Copy link

New Feature

Q A
New Feature yes
RFC no
BC Break no

Summary

Fixes #11496

This PR introduces a new ClassMetadataFactoryInterface interface and changes the type-hints to allow switching implementation of the factory in configuration.

@@ -154,6 +154,7 @@

<rule ref="SlevomatCodingStandard.Classes.SuperfluousInterfaceNaming">
<exclude-pattern>src/EntityManagerInterface.php</exclude-pattern>
<exclude-pattern>src/Mapping/ClassMetadataFactoryInterface.php</exclude-pattern>
Copy link
Member

Choose a reason for hiding this comment

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

Why was this excluded from phpcs checks? It's going to be a new interface that you can name without the superflous naming.

Copy link
Author

Choose a reason for hiding this comment

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

Doctrine\ORM\Mapping\ClassMetadataFactory is already used (is the name of the current implementation).
Do you have a better proposal for the name of this interface?

Comment on lines +381 to +384
$reflectionClass = new ReflectionClass($cmfName);
if (! $reflectionClass->implementsInterface(ClassMetadataFactoryInterface::class)) {
throw InvalidClassMetadataFactory::create($cmfName);
}
Copy link
Member

Choose a reason for hiding this comment

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

This would introduce an exception when there was not one before.

Copy link
Author

Choose a reason for hiding this comment

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

Prior to this PR a call with a class different from Doctrine\ORM\Mapping\ClassMetadataFactory would cause a TypeError. So yes, it would not throw an exception here, but in a different (and completely unrelated) point of the code, which can be harder to debug.

@alekitto
Copy link
Author

alekitto commented Nov 5, 2024

@SenseException any news on this?

@beberlei
Copy link
Member

beberlei commented Nov 5, 2024

@alekitto can you explain what your use case is in overwriting the CMF this way? Just trying to asses what is the overall idea. Historically we didn't like overwriting any of the internal classes, which I would count the CMF.

@alekitto
Copy link
Author

alekitto commented Nov 5, 2024

@alekitto can you explain what your use case is in overwriting the CMF this way? Just trying to asses what is the overall idea. Historically we didn't like overwriting any of the internal classes, which I would count the CMF.

I already did that on the issue: #11496 (comment)

I'm overriding the metadata factory with a mocked/fake version of it in unit tests, to precisely control what has been returned and test libraries and classes in isolation. The configuration object still allow to overwrite the class name of the class metadata factory, and I always used that in testing and, for example, to override the ClassMetadata object in order to support associations on embeddables.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cannot override the class metadata factory via configuration since 3.0
3 participants