10000 [DependencyInjection] improved exception message · symfony/symfony@3647cca · GitHub
[go: up one dir, main page]

Skip to content

Commit 3647cca

Browse files
committed
[DependencyInjection] improved exception message
1 parent 1ff2ab3 commit 3647cca

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,13 @@ public function process(ContainerBuilder $container)
2929
if ($definition->isSynthetic() || null !== $definition->getClass()) {
3030
continue;
3131
}
32-
if (preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+(?:\\\\[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+)++$/', $id)) {
32+
if (preg_match('/^\\\\?[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+(?:\\\\[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+)++$/', $id)) {
3333
if ($definition instanceof ChildDefinition && !class_exists($id)) {
3434
throw new InvalidArgumentException(sprintf('Service definition "%s" has a parent but no class, and its name looks like a FQCN. Either the class is missing or you want to inherit it from the parent service. To resolve this ambiguity, please rename this service to a non-FQCN (e.g. using dots), or create the missing class.', $id));
3535
}
36+
if ('\\' === $id[0]) {
37+
throw new InvalidArgumentException(sprintf('Service definition "%s" has no class, and its name looks like a FQCN but it starts with a backslash; remove the leading backslash.', $id));
38+
}
3639
$definition->setClass($id);
3740
}
3841
}

src/Symfony/Component/DependencyInjection/Tests/Compiler/ResolveClassPassTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\DependencyInjection\ChildDefinition;
1616
use Symfony\Component\DependencyInjection\Compiler\ResolveClassPass;
1717
use Symfony\Component\DependencyInjection\ContainerBuilder;
18+
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
1819
use Symfony\Component\DependencyInjection\Tests\Fixtures\CaseSensitiveClass;
1920

2021
class ResolveClassPassTest extends TestCase
@@ -58,6 +59,17 @@ public function provideInvalidClassId()
5859
yield ['\DateTime'];
5960
}
6061

62+
public function testWontResolveClassFromClassIdWithLeadingBackslash()
63+
{
64+
$this->expectException(InvalidArgumentException::class);
65+
$this->expectExceptionMessage('Service definition "\App\Some\Service" has no class, and its name looks like a FQCN but it starts with a backslash; remove the leading backslash.');
66+
67+
$container = new ContainerBuilder();
68+
$container->register('\App\Some\Service');
69+
70+
(new ResolveClassPass())->process($container);
71+
}
72+
6173
public function testNonFqcnChildDefinition()
6274
{
6375
$container = new ContainerBuilder();

0 commit comments

Comments
 (0)
0