8000 minor #22153 [DX] [DI] Throw more helpful error when shortcutting glo… · symfony/symfony@3cd563a · GitHub
[go: up one dir, main page]

Skip to content

Commit 3cd563a

Browse files
committed
minor #22153 [DX] [DI] Throw more helpful error when shortcutting global classes (curry684)
This PR was merged into the 3.3-dev branch. Discussion ---------- [DX] [DI] Throw more helpful error when shortcutting global classes | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | no | BC breaks? |no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #22146 | License | MIT As discussed in #22146 the error message received when trying to use a class in the global namespace as a service without defined class is confusing. Helpful information was added pointing out this current limitation. Commits ------- b9e7b4f [DependencyInjection] Throw helpful error when shortcutting global classes
2 parents 21827a5 + b9e7b4f commit 3cd563a

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
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
if ($definition->getFactory()) {
4949
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));
5050
}
51+
if (class_exists($id) || interface_exists($id, false)) {
52+
throw new RuntimeException(sprintf(
53+
'The definition for "%s" has no class attribute, and appears to reference a '
54+
.'class or interface in the global namespace. Leaving out the "class" attribute '
55+
.'is only allowed for namespaced classes. Please specify the class attribute '
56+
.'explicitly to get rid of this error.',
57+
$id
58+
));
59+
}
5160

5261
throw new RuntimeException(sprintf(
5362
'The definition for "%s" has no class. If you intend to inject '

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1047,7 +1047,7 @@ public function testClassFromId()
10471047

10481048
/**
10491049
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
1050-
* @expectedExceptionMessage The definition for "DateTime" has no class.
1050+
* @expectedExceptionMessage The definition for "DateTime" has no class attribute, and appears to reference a class or interface in the global namespace.
10511051
*/
10521052
public function testNoClassFromGlobalNamespaceClassId()
10531053
{

0 commit comments

Comments
 (0)
0