8000 [DX] [DI] Throw more helpful error when shortcutting global classes by curry684 · Pull Request #22153 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[DX] [DI] Throw more helpful error when shortcutting global classes #22153

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

Merged
merged 1 commit into from
Mar 27, 2017
Merged
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
[DependencyInjection] Throw helpful error when shortcutting global cl…
…asses

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.
  • Loading branch information
curry684 committed Mar 26, 2017
commit b9e7b4fd61edd3ee3752c549f72395ef6b3d554f
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -1106,7 +1106,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