Skip to content

Commit 61de060

Browse files
ro0NLnicolas-grekas
authored andcommitted
[DI] Improve class named servics error message
1 parent 0ff03c9 commit 61de060

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/Symfony/Component/DependencyInjection/Compiler/CheckDefinitionValidityPass.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,15 @@ public function process(ContainerBuilder $container)
4848
throw new RuntimeException(sprintf('Please add the class to service "%s" even if it is constructed by a factory since we might need to add method calls based on compile-time checks.', $id));
4949
}
5050
if (class_exists($id) || interface_exists($id, false)) {
51+
if (0 === strpos($id, '\\') && 1 < substr_count($id, '\\')) {
52+
throw new RuntimeException(sprintf(
53+
'The definition for "%s" has no class attribute, and appears to reference a class or interface. '
54+
.'Please specify the class attribute explicitly or remove the leading backslash by renaming '
55+
.'the service to "%s" to get rid of this error.',
56+
$id, substr($id, 1)
57+
));
58+
}
59+
5160
throw new RuntimeException(sprintf(
5261
'The definition for "%s" has no class attribute, and appears to reference a '
5362
.'class or interface in the global namespace. Leaving out the "class" attribute '

src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,6 +1264,30 @@ public function testNoClassFromGlobalNamespaceClassId()
12641264
$container->compile();
12651265
}
12661266

1267+
/**
1268+
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
1269+
* @expectedExceptionMessage The definition for "\DateTime" has no class attribute, and appears to reference a class or interface in the global namespace.
1270+
*/
1271+
public function testNoClassFromGlobalNamespaceClassIdWithLeadingSlash()
1272+
{
1273+
$container = new ContainerBuilder();
1274+
1275+
$container->register('\\'.\DateTime::class);
1276+
$container->compile();
1277+
}
1278+
1279+
/**
1280+
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
1281+
* @expectedExceptionMessage The definition for "\Symfony\Component\DependencyInjection\Tests\FooClass" 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 "Symfony\Component\DependencyInjection\Tests\FooClass" to get rid of this error.
1282+
*/
1283+
public function testNoClassFromNamespaceClassIdWithLeadingSlash()
1284+
{
1285+
$container = new ContainerBuilder();
1286+
1287+
$container->register('\\'.FooClass::class);
1288+
$container->compile();
1289+
}
1290+
12671291
/**
12681292
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
12691293
* @expectedExceptionMessage The definition for "123_abc" has no class.

0 commit comments

Comments
 (0)