Skip to content

Commit

Permalink
minor #22153 [DX] [DI] Throw more helpful error when shortcutting glo…
Browse files Browse the repository at this point in the history
…bal 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
  • Loading branch information
fabpot committed Mar 27, 2017
2 parents 21827a5 + b9e7b4f commit 3cd563a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
Expand Up @@ -48,6 +48,15 @@ public function process(ContainerBuilder $container)
if ($definition->getFactory()) {
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));
}
if (class_exists($id) || interface_exists($id, false)) {
throw new RuntimeException(sprintf(
'The definition for "%s" has no class attribute, and appears to reference a '
.'class or interface in the global namespace. Leaving out the "class" attribute '
.'is only allowed for namespaced classes. Please specify the class attribute '
.'explicitly to get rid of this error.',
$id
));
}

throw new RuntimeException(sprintf(
'The definition for "%s" has no class. If you intend to inject '
Expand Down
Expand Up @@ -1047,7 +1047,7 @@ public function testClassFromId()

/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
* @expectedExceptionMessage The definition for "DateTime" has no class.
* @expectedExceptionMessage The definition for "DateTime" has no class attribute, and appears to reference a class or interface in the global namespace.
*/
public function testNoClassFromGlobalNamespaceClassId()
{
Expand Down

0 comments on commit 3cd563a

Please sign in to comment.