Content-Length: 516956 | pFad | http://github.com/symfony/symfony/pull/61215

82 [DependencyInjection] Update `ResolveClassPass` to check class existence by GaryPEGEOT · Pull Request #61215 · symfony/symfony · GitHub
Skip to content

[DependencyInjection] Update ResolveClassPass to check class existence #61215

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

Open
wants to merge 14 commits into
base: 7.4
Choose a base branch
from

Conversation

GaryPEGEOT
Copy link
Contributor

@GaryPEGEOT GaryPEGEOT commented Jul 23, 2025

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

Currently doesn't check for incorrect factories, but I'm not sure what would be the best approach

@carsonbot carsonbot added Status: Needs Review DependencyInjection DX DX = Developer eXperience (anything that improves the experience of using Symfony) Feature labels Jul 23, 2025
@carsonbot carsonbot added this to the 7.4 milestone Jul 23, 2025
@carsonbot carsonbot changed the title [DX][DI] Add a compiler pass to check class existence. [DependencyInjection] Add a compiler pass to check class existence. Jul 23, 2025
@OskarStark OskarStark changed the title [DependencyInjection] Add a compiler pass to check class existence. [DependencyInjection] Add a compiler pass to check class existence Jul 23, 2025
@GaryPEGEOT GaryPEGEOT force-pushed the feat/check-class-pass branch from 740f29a to 65259c9 Compare July 23, 2025 13:19
@GaryPEGEOT GaryPEGEOT requested a review from chalasr as a code owner July 23, 2025 13:19
@nicolas-grekas
Copy link
Member

This approach is too broad IMHO. There are cases where the class is eg string for some internal services.

In #54095 (comment), I wrote:

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).

Can't we patch the existing compiler pass that copies the id to the class when no class is set, and make it throw when id is not a class?

@GaryPEGEOT
Copy link
Contributor Author

Can't we patch the existing compiler pass that copies the id to the class when no class is set, and make it throw when id is not a class?

But then it would not work if you define a manual id and make a typo in the class (although more of a niche case)

@GaryPEGEOT
Copy link
Contributor Author

There are cases where the class is eg string for some internal services.

So we could have something like ['mailer.transport', 'createFromDsn'] at this point? (Not a reference but the service ID as is?

@GaryPEGEOT GaryPEGEOT force-pushed the feat/check-class-pass branch from 65259c9 to 321ccff Compare July 24, 2025 08:48
@nicolas-grekas
Copy link
Member

There are many ways one can mess up with their DI config. Validating everything adds to the compilation time. That's something we always considered outside of the scope of the compilation step.

@GaryPEGEOT GaryPEGEOT changed the title [DependencyInjection] Add a compiler pass to check class existence [DependencyInjection] Update ResolveClassPass to check class existence Jul 24, 2025
@OskarStark OskarStark changed the title [DependencyInjection] Update ResolveClassPass to check class existence [DependencyInjection] Update ResolveClassPass to check class existence Jul 24, 2025
Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

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

LGTM, test cases changes look consistent with the updated behavior.

if (!class_exists($id) && !interface_exists($id)) {
$error = $definition instanceof ChildDefinition ?
'has a parent but no class, and its name looks like an FQCN. Either the class is missing or you want to inherit it from the parent service' :
'name looks like an FQCN but the class does not exists';
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
'name looks like an FQCN but the class does not exists';
'name looks like a FQCN but the class does not exist';

throw new InvalidArgumentException(\sprintf('Service definition "%s" has a parent but no class, and its name looks like an FQCN. Either the class is missing or you want to inherit it from the parent service. To resolve this ambiguity, please rename this service to a non-FQCN (e.g. using dots), or create the missing class.', $id));
if (!class_exists($id) && !interface_exists($id)) {
$error = $definition instanceof ChildDefinition ?
'has a parent but no class, and its name looks like an FQCN. Either the class is missing or you want to inherit it from the parent service' :
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
'has a parent but no class, and its name looks like an FQCN. Either the class is missing or you want to inherit it from the parent service' :
'has a parent but no class, and its name looks like a FQCN. Either the class is missing or you want to inherit it from the parent service' :

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed!

@GaryPEGEOT GaryPEGEOT force-pushed the feat/check-class-pass branch from 9690e1e to 349c87c Compare July 25, 2025 06:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
DependencyInjection DX DX = Developer eXperience (anything that improves the experience of using Symfony) Feature Status: Needs Work
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
6 participants








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/symfony/symfony/pull/61215

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy