Skip to content

Commit f495168

Browse files
minor symfony#28057 [DI] Improve class named servics error message (ro0NL)
This PR was squashed before being merged into the 3.4 branch (closes symfony#28057). Discussion ---------- [DI] Improve class named servics error message | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | no | New feature? | no | BC breaks? | no <!-- see https://symfony.com/bc --> | Deprecations? | no | Tests pass? | yes <!-- please add some, will be required by reviewers --> | Fixed tickets | symfony#28006 | License | MIT | Doc PR | symfony/symfony-docs#... <!-- required for new features --> Commits ------- 61de060 [DI] Improve class named servics error message
2 parents d0eef33 + 61de060 commit f495168

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)