Skip to content

[DI] Validate class-like service IDs #33111

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ public function process(ContainerBuilder $container)
$id
));
}
if (preg_match('/^\\\\[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+(?:\\\\[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+)++$/', $id)) {
throw new RuntimeException(sprintf(
'The definition for "%s" has no class attribute, and appears to reference a class or interface. '
Copy link
Member

Choose a reason for hiding this comment

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

and appears to reference a class or interface

actually not, since the class doesn't exist
that + the fact that the message reported in #33092 is not the one just below, I'm not sure this PR is the correct answer.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

you're right. Let's continue in #33108

AFAIK the if block at L51 is now dead code :/

.'Please specify the class attribute explicitly or remove the leading backslash by renaming '
.'the service to "%s" to get rid of this error.',
$id, substr($id, 1)
));
}

throw new RuntimeException(sprintf('The definition for "%s" has no class. If you intend to inject this service dynamically at runtime, please mark it as synthetic=true. If this is an abstract definition solely used by child definitions, please add abstract=true, otherwise specify a class to get rid of this error.', $id));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1289,6 +1289,16 @@ public function testNoClassFromNamespaceClassIdWithLeadingSlash()
$container->compile();
}

public function testNoClassFromNamespaceDummyClassIdWithLeadingSlash()
{
$this->expectException('Symfony\Component\DependencyInjection\Exception\RuntimeException');
$this->expectExceptionMessage('The definition for "\\App\\Foo" has no class attribute, and appears to reference a class or interface. Please specify the class attribute explicitly or remove the leading backslash by renaming the service to "App\\Foo" to get rid of this error.');
$container = new ContainerBuilder();

$container->register('\\App\\Foo');
$container->compile();
}

public function testNoClassFromNonClassId()
{
$this->expectException('Symfony\Component\DependencyInjection\Exception\RuntimeException');
Expand Down