Skip to content

[EventDispatcher] Validate existence of event listener classes #60277

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

Conversation

ashutoshagrawal1010
Copy link

@ashutoshagrawal1010 ashutoshagrawal1010 commented Apr 26, 2025

Q A
Branch? 7.3
Bug fix? no
New feature? no
Deprecations? no
Issues Fix #54095
License MIT

This PR improves the developer experience for event listener services.

  • During container compilation, it validates that the service class defined for a kernel.event_listener tag actually exists.
  • If the class does not exist, it throws an early InvalidArgumentException instead of failing later at runtime.

@carsonbot
Copy link

Hey!

I see that this is your first PR. That is great! Welcome!

Symfony has a contribution guide which I suggest you to read.

In short:

  • Always add tests
  • Keep backward compatibility (see https://symfony.com/bc).
  • Bug fixes must be submitted against the lowest maintained branch where they apply (see https://symfony.com/releases)
  • Features and deprecations must be submitted against the 7.3 branch.

Review the GitHub status checks of your pull request and try to solve the reported issues. If some tests are failing, try to see if they are failing because of this change.

When two Symfony core team members approve this change, it will be merged and you will become an official Symfony contributor!
If this PR is merged in a lower version branch, it will be merged up to all maintained branches within a few days.

I am going to sit back now and wait for the reviews.

Cheers!

Carsonbot

@ashutoshagrawal1010
Copy link
Author

As per my understanding, failed tests are unrelated to my change.

@ashutoshagrawal1010 ashutoshagrawal1010 changed the title [EventDispatcher] Validate existence of event listener classes [DependencyInjection] Validate existence of event listener classes Apr 26, 2025
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.

This is something that could potentially be useful for all the classes in the container, not just the event listener. As a last step before dumping the container, we can check the classes used for all services.

This was proposed 10 years ago, but declined: #11315 (comment). This decision may be reviewed.

@carsonbot carsonbot changed the title [DependencyInjection] Validate existence of event listener classes [EventDispatcher] Validate existence of event listener classes Apr 28, 2025
@ashutoshagrawal1010 ashutoshagrawal1010 marked this pull request as draft April 30, 2025 00:54
@ashutoshagrawal1010 ashutoshagrawal1010 force-pushed the fix-missing-listener-class-validation branch 3 times, most recently from 2b5b852 to 7a8e232 Compare April 30, 2025 01:30
…ggest close matches during container compilation
@ashutoshagrawal1010 ashutoshagrawal1010 force-pushed the fix-missing-listener-class-validation branch from 7a8e232 to 282dc16 Compare April 30, 2025 01:41
@ashutoshagrawal1010 ashutoshagrawal1010 marked this pull request as ready for review April 30, 2025 01:52
@carsonbot

This comment has been minimized.

@GromNaN GromNaN changed the base branch from 6.4 to 7.3 May 9, 2025 08:26
@@ -172,10 +172,13 @@ public function process(ContainerBuilder $container)

private function getEventFromTypeDeclaration(ContainerBuilder $container, string $id, string $method): string
{
$class = $container->getDefinition($id)->getClass();
if (!$r = $container->getReflectionClass($class)) {
throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id));
Copy link
Member

Choose a reason for hiding this comment

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

An exception is already thrown by ContainerBuilder::getReflectionClass:

/**
* Retrieves the requested reflection class and registers it for resource tracking.
*
* @throws \ReflectionException when a parent class/interface/trait is not found and $throw is true
*
* @final
*/
public function getReflectionClass(?string $class, bool $throw = true): ?\ReflectionClass

Copy link
Author

@ashutoshagrawal1010 ashutoshagrawal1010 May 13, 2025

Choose a reason for hiding this comment

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

I could not reproduce this exception with the test in the PR. The ContainerBuilder::getReflectionClass returns null instead of throwing.

@GromNaN
Copy link
Member

GromNaN commented May 9, 2025

I changed the target branch as this is a behavior change and not a bug.

@nicolas-grekas
Copy link
Member

nicolas-grekas commented May 9, 2025

👎 for the current implementation but looking at the linked issue, I'm wondering if we shouldn't add the check when the class is not defined for a service (and thus when the id becomes the class). Up for digging this idea @ashutoshagrawal1010?

@nicolas-grekas
Copy link
Member

Closing meanwhile. Thanks for submitting.

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.

Confusing error message: 'must define the "event" attribute on "kernel.event_listener" tags' when the problem was nonexistent class
4 participants