Skip to content

Commit 7d3ad74

Browse files
[DependencyInjection] Deprecate registering a service without a class when its id is a non-existing FQCN
1 parent 585e9df commit 7d3ad74

File tree

5 files changed

+25
-13
lines changed

5 files changed

+25
-13
lines changed

UPGRADE-7.4.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ DependencyInjection
2222
-------------------
2323

2424
* Add argument `$target` to `ContainerBuilder::registerAliasForArgument()`
25+
* Deprecate registering a service without a class when its id is a non-existing FQCN
2526

2627
DoctrineBridge
2728
--------------

src/Symfony/Component/DependencyInjection/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* Allow `#[AsAlias]` to be extended
88
* Add argument `$target` to `ContainerBuilder::registerAliasForArgument()`
9+
* Deprecate registering a service without a class when its id is a non-existing FQCN
910

1011
7.3
1112
---

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,23 @@ class ResolveClassPass implements CompilerPassInterface
2323
public function process(ContainerBuilder $container): void
2424
{
2525
foreach ($container->getDefinitions() as $id => $definition) {
26-
if ($definition->isSynthetic() || null !== $definition->getClass()) {
26+
if ($definition->isSynthetic()
27+
|| null !== $definition->getClass()
28+
|| !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)
29+
) {
2730
continue;
2831
}
29-
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)) {
30-
if (!class_exists($id) && !interface_exists($id)) {
31-
$error = $definition instanceof ChildDefinition ?
32-
'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' :
33-
'name looks like a FQCN but the class does not exist';
34-
35-
throw new InvalidArgumentException("Service definition \"{$id}\" {$error}. To resolve this ambiguity, please rename this service to a non-FQCN (e.g. using dots), or create the missing class.");
36-
}
37-
32+
if (class_exists($id) || interface_exists($id, false)) {
3833
$definition->setClass($id);
34+
continue;
3935
}
36+
if ($definition instanceof ChildDefinition) {
37+
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));
38+
}
39+
40+
trigger_deprecation('symfony/dependency-injection', '7.4', 'Service id "%s" looks like a FQCN but no corresponding class or interface exists. To resolve this ambiguity, please rename this service to a non-FQCN (e.g. using dots), or create the missing class or interface.', $id);
41+
// throw new InvalidArgumentException(\sprintf('Service id "%s" looks like a FQCN but no corresponding class or interface exists. To resolve this ambiguity, please rename this service to a non-FQCN (e.g. using dots), or create the missing class or interface.'), $id);
42+
$definition->setClass($id);
4043
}
4144
}
4245
}

src/Symfony/Component/DependencyInjection/Loader/Configurator/Traits/BindTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ trait BindTrait
2424
* injected in the matching parameters (of the constructor, of methods
2525
* called and of controller actions).
2626
*
27-
* @param string $nameOrFqcn A parameter name with its "$" prefix, or an FQCN
27+
* @param string $nameOrFqcn A parameter name with its "$" prefix, or a FQCN
2828
* @param mixed $valueOrRef The value or reference to bind
2929
*
3030
* @return $this

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\DependencyInjection\Tests\Compiler;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
1516
use Symfony\Component\DependencyInjection\ChildDefinition;
1617
use Symfony\Component\DependencyInjection\Compiler\ResolveClassPass;
1718
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -20,6 +21,8 @@
2021

2122
class ResolveClassPassTest extends TestCase
2223
{
24+
use ExpectDeprecationTrait;
25+
2326
/**
2427
* @dataProvider provideValidClassId
2528
*/
@@ -93,10 +96,14 @@ public function testAmbiguousChildDefinition()
9396
(new ResolveClassPass())->process($container);
9497
}
9598

99+
/**
100+
* @group legacy
101+
*/
96102
public function testInvalidClassNameDefinition()
97103
{
98-
$this->expectException(InvalidArgumentException::class);
99-
$this->expectExceptionMessage('Service definition "Acme\UnknownClass" name looks like a FQCN but the class does not exist. To resolve this ambiguity, please rename this service to a non-FQCN (e.g. using dots), or create the missing class.');
104+
// $this->expectException(InvalidArgumentException::class);
105+
// $this->expectExceptionMessage('Service id "Acme\UnknownClass" looks like a FQCN but no corresponding class or interface exists. To resolve this ambiguity, please rename this service to a non-FQCN (e.g. using dots), or create the missing class or interface.');
106+
$this->expectDeprecation('Since symfony/dependency-injection 7.4: Service id "Acme\UnknownClass" looks like a FQCN but no corresponding class or interface exists. To resolve this ambiguity, please rename this service to a non-FQCN (e.g. using dots), or create the missing class or interface.');
100107
$container = new ContainerBuilder();
101108
$container->register('Acme\UnknownClass');
102109

0 commit comments

Comments
 (0)