Skip to content

Handle anonymous classes more gracefully #286

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

Merged
merged 3 commits into from
Apr 20, 2022
Merged

Conversation

greg0ire
Copy link
Member

Fixes #24

Verified

This commit was signed with the committer’s verified signature.
greg0ire Grégoire Paris
It does not necessary hold a proxy class.
@greg0ire greg0ire changed the title Return null as a manager for anonymous classes Handle anonymous classes more gracefully Apr 20, 2022
@stof
Copy link
Member

stof commented Apr 20, 2022

I think you also need to update isTransient, to avoid making it trigger a deprecation and call getFqcnFromAlias if the anonymous class name contains a :.

@@ -462,7 +468,7 @@ public function isTransient($className)
}

// Check for namespace alias
if (strpos($className, ':') !== false) {
if (! class_exists($className) && strpos($className, ':') !== false) {
Copy link
Member

Choose a reason for hiding this comment

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

I would use ! class_exists($className, false), to avoid triggering autoloaders for namespace aliases. Excluding anonymous classes does not need to trigger the autoloader.

Copy link
Member

Choose a reason for hiding this comment

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

btw, other class_exists checks added in the PR in the anonymous class checks should probably do the same, as they are added before the resolution of the real class name which resolves aliases.

Copy link
Member Author

@greg0ire greg0ire Apr 20, 2022

Choose a reason for hiding this comment

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

I think that makes sense, since we want to test if the class is anonymous and anonymous classes surely don't require any autoloading.

@@ -220,6 +222,10 @@ abstract protected function isEntity(ClassMetadata $class);
*/
public function getMetadataFor($className)
{
if (class_exists($className, false) && (new ReflectionClass($className))->isAnonymous()) {
Copy link
Member

Choose a reason for hiding this comment

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

should we put that after the check of the cache, to avoid paying the cost of this reflection check when retrieving metadata that was already loaded (which is something that the ORM does a lot, including in its critical path) ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure 👍

In some situations, they were mistaken for short aliases.
@greg0ire greg0ire added this to the 2.5.2 milestone Apr 20, 2022
@greg0ire greg0ire added the Bug Something isn't working label Apr 20, 2022
@greg0ire greg0ire merged commit 07436d9 into doctrine:2.5.x Apr 20, 2022
@greg0ire greg0ire deleted the fix-24 branch April 20, 2022 16:52
@greg0ire greg0ire linked an issue Apr 22, 2022 that may be closed by this pull request
@derrabus derrabus mentioned this pull request Apr 22, 2022
weaverryan added a commit to weaverryan/ux that referenced this pull request Mar 22, 2023
symfony-splitter pushed a commit to symfony/ux-live-component that referenced this pull request Mar 22, 2023
jameswebapp added a commit to jameswebapp/ux that referenced this pull request Aug 1, 2023
jcrombez pushed a commit to jcrombez/ux that referenced this pull request Aug 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Registry::getManagerForClass doesn't work with anonymous classes
2 participants