Skip to content

Commit e2900b9

Browse files
[DI] add id of referencing service when a deprecated alias is found
1 parent b1e9e86 commit e2900b9

18 files changed

+58
-55
lines changed

Alias.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class Alias
2121
private $deprecated;
2222
private $deprecationTemplate;
2323

24-
private static $defaultDeprecationTemplate = 'The "%service_id%" service alias is deprecated. You should stop using it, as it will soon be removed.';
24+
private static $defaultDeprecationTemplate = 'The "%alias_id%" service alias is deprecated. You should stop using it, as it will be removed in the future.';
2525

2626
public function __construct(string $id, bool $public = true)
2727
{
@@ -103,8 +103,8 @@ public function setDeprecated($status = true, $template = null)
103103
throw new InvalidArgumentException('Invalid characters found in deprecation template.');
104104
}
105105

106-
if (false === strpos($template, '%service_id%')) {
107-
throw new InvalidArgumentException('The deprecation template must contain the "%service_id%" placeholder.');
106+
if (false === strpos($template, '%alias_id%')) {
107+
throw new InvalidArgumentException('The deprecation template must contain the "%alias_id%" placeholder.');
108108
}
109109

110110
$this->deprecationTemplate = $template;
@@ -122,7 +122,7 @@ public function isDeprecated(): bool
122122

123123
public function getDeprecationMessage(string $id): string
124124
{
125-
return str_replace('%service_id%', $id, $this->deprecationTemplate ?: self::$defaultDeprecationTemplate);
125+
return str_replace('%alias_id%', $id, $this->deprecationTemplate ?: self::$defaultDeprecationTemplate);
126126
}
127127

128128
/**

Compiler/RemoveUnusedDefinitionsPass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public function process(ContainerBuilder $container)
8686
protected function processValue($value, $isRoot = false)
8787
{
8888
if (!$value instanceof Reference) {
89-
return parent::processValue($value);
89+
return parent::processValue($value, $isRoot);
9090
}
9191

9292
if (ContainerBuilder::IGNORE_ON_UNINITIALIZED_REFERENCE !== $value->getInvalidBehavior()) {

Compiler/ResolveReferencesToAliasesPass.php

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public function process(ContainerBuilder $container)
3131

3232
foreach ($container->getAliases() as $id => $alias) {
3333
$aliasId = (string) $alias;
34+
$this->currentId = $id;
3435

3536
if ($aliasId !== $defId = $this->getDefinitionId($aliasId, $container)) {
3637
$container->setAlias($id, $defId)->setPublic($alias->isPublic())->setPrivate($alias->isPrivate());
@@ -43,34 +44,36 @@ public function process(ContainerBuilder $container)
4344
*/
4445
protected function processValue($value, $isRoot = false)
4546
{
46-
if ($value instanceof Reference) {
47-
$defId = $this->getDefinitionId($id = (string) $value, $this->container);
48-
49-
if ($defId !== $id) {
50-
return new Reference($defId, $value->getInvalidBehavior());
51-
}
47+
if (!$value instanceof Reference) {
48+
return parent::processValue($value, $isRoot);
5249
}
5350

54-
return parent::processValue($value);
51+
$defId = $this->getDefinitionId($id = (string) $value, $this->container);
52+
53+
return $defId !== $id ? new Reference($defId, $value->getInvalidBehavior()) : $value;
5554
}
5655

5756
private function getDefinitionId(string $id, ContainerBuilder $container): string
5857
{
58+
if (!$container->hasAlias($id)) {
59+
return $id;
60+
}
61+
62+
$alias = $container->getAlias($id);
63+
64+
if ($alias->isDeprecated()) {
65+
@trigger_error(sprintf('%s. It is being referenced by the "%s" %s.', rtrim($alias->getDeprecationMessage($id), '. '), $this->currentId, $container->hasDefinition($this->currentId) ? 'service' : 'alias'), E_USER_DEPRECATED);
66+
}
67+
5968
$seen = [];
60-
while ($container->hasAlias($id)) {
69+
do {
6170
if (isset($seen[$id])) {
6271
throw new ServiceCircularReferenceException($id, array_merge(array_keys($seen), [$id]));
6372
}
6473

6574
$seen[$id] = true;
66-
$alias = $container->getAlias($id);
67-
68-
if ($alias->isDeprecated()) {
69-
@trigger_error($alias->getDeprecationMessage($id), E_USER_DEPRECATED);
70-
}
71-
72-
$id = (string) $alias;
73-
}
75+
$id = (string) $container->getAlias($id);
76+
} while ($container->hasAlias($id));
7477

7578
return $id;
7679
}

Definition.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class Definition
4747

4848
protected $arguments = [];
4949

50-
private static $defaultDeprecationTemplate = 'The "%service_id%" service is deprecated. You should stop using it, as it will soon be removed.';
50+
private static $defaultDeprecationTemplate = 'The "%service_id%" service is deprecated. You should stop using it, as it will be removed in the future.';
5151

5252
/**
5353
* @internal

Tests/AliasTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function testCanSetPublic()
5252
public function testCanDeprecateAnAlias()
5353
{
5454
$alias = new Alias('foo', false);
55-
$alias->setDeprecated(true, 'The %service_id% service is deprecated.');
55+
$alias->setDeprecated(true, 'The %alias_id% service is deprecated.');
5656

5757
$this->assertTrue($alias->isDeprecated());
5858
}
@@ -62,14 +62,14 @@ public function testItHasADefaultDeprecationMessage()
6262
$alias = new Alias('foo', false);
6363
$alias->setDeprecated();
6464

65-
$expectedMessage = 'The "foo" service alias is deprecated. You should stop using it, as it will soon be removed.';
65+
$expectedMessage = 'The "foo" service alias is deprecated. You should stop using it, as it will be removed in the future.';
6666
$this->assertEquals($expectedMessage, $alias->getDeprecationMessage('foo'));
6767
}
6868

6969
public function testReturnsCorrectDeprecationMessage()
7070
{
7171
$alias = new Alias('foo', false);
72-
$alias->setDeprecated(true, 'The "%service_id%" is deprecated.');
72+
$alias->setDeprecated(true, 'The "%alias_id%" is deprecated.');
7373

7474
$expectedMessage = 'The "foo" is deprecated.';
7575
$this->assertEquals($expectedMessage, $alias->getDeprecationMessage('foo'));
@@ -101,10 +101,10 @@ public function testCannotDeprecateWithAnInvalidTemplate($message)
101101
public function invalidDeprecationMessageProvider()
102102
{
103103
return [
104-
"With \rs" => ["invalid \r message %service_id%"],
105-
"With \ns" => ["invalid \n message %service_id%"],
106-
'With */s' => ['invalid */ message %service_id%'],
107-
'message not containing required %service_id% variable' => ['this is deprecated'],
104+
"With \rs" => ["invalid \r message %alias_id%"],
105+
"With \ns" => ["invalid \n message %alias_id%"],
106+
'With */s' => ['invalid */ message %alias_id%'],
107+
'message not containing required %alias_id% variable' => ['this is deprecated'],
108108
];
109109
}
110110
}

Tests/Compiler/ResolveReferencesToAliasesPassTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public function testResolveFactory()
8585

8686
/**
8787
* @group legacy
88-
* @expectedDeprecation The "deprecated_foo_alias" service alias is deprecated. You should stop using it, as it will soon be removed.
88+
* @expectedDeprecation The "deprecated_foo_alias" service alias is deprecated. You should stop using it, as it will be removed in the future. It is being referenced by the "alias" alias.
8989
*/
9090
public function testDeprecationNoticeWhenReferencedByAlias()
9191
{
@@ -105,7 +105,7 @@ public function testDeprecationNoticeWhenReferencedByAlias()
105105

106106
/**
107107
* @group legacy
108-
* @expectedDeprecation The "foo_aliased" service alias is deprecated. You should stop using it, as it will soon be removed.
108+
* @expectedDeprecation The "foo_aliased" service alias is deprecated. You should stop using it, as it will be removed in the future. It is being referenced by the "definition" service.
109109
*/
110110
public function testDeprecationNoticeWhenReferencedByDefinition()
111111
{

Tests/ContainerBuilderTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public function testDefinitions()
8888

8989
/**
9090
* @group legacy
91-
* @expectedDeprecation The "deprecated_foo" service is deprecated. You should stop using it, as it will soon be removed.
91+
* @expectedDeprecation The "deprecated_foo" service is deprecated. You should stop using it, as it will be removed in the future.
9292
*/
9393
public function testCreateDeprecatedService()
9494
{
@@ -261,7 +261,7 @@ public function testAliases()
261261

262262
/**
263263
* @group legacy
264-
* @expectedDeprecation The "foobar" service alias is deprecated. You should stop using it, as it will soon be removed.
264+
* @expectedDeprecation The "foobar" service alias is deprecated. You should stop using it, as it will be removed in the future.
265265
*/
266266
public function testDeprecatedAlias()
267267
{

Tests/DefinitionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public function testSetIsDeprecated()
164164
$this->assertFalse($def->isDeprecated(), '->isDeprecated() returns false by default');
165165
$this->assertSame($def, $def->setDeprecated(true), '->setDeprecated() implements a fluent interface');
166166
$this->assertTrue($def->isDeprecated(), '->isDeprecated() returns true if the instance should not be used anymore.');
167-
$this->assertSame('The "deprecated_service" service is deprecated. You should stop using it, as it will soon be removed.', $def->getDeprecationMessage('deprecated_service'), '->getDeprecationMessage() should return a formatted message template');
167+
$this->assertSame('The "deprecated_service" service is deprecated. You should stop using it, as it will be removed in the future.', $def->getDeprecationMessage('deprecated_service'), '->getDeprecationMessage() should return a formatted message template');
168168
}
169169

170170
/**

Tests/Dumper/PhpDumperTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ public function testAliases()
327327

328328
/**
329329
* @group legacy
330-
* @expectedDeprecation The "alias_for_foo_deprecated" service alias is deprecated. You should stop using it, as it will soon be removed.
330+
* @expectedDeprecation The "alias_for_foo_deprecated" service alias is deprecated. You should stop using it, as it will be removed in the future.
331331
*/
332332
public function testAliasesDeprecation()
333333
{
@@ -1067,7 +1067,7 @@ public function testAdawsonContainer()
10671067
* This test checks the trigger of a deprecation note and should not be removed in major releases.
10681068
*
10691069
* @group legacy
1070-
* @expectedDeprecation The "foo" service is deprecated. You should stop using it, as it will soon be removed.
1070+
* @expectedDeprecation The "foo" service is deprecated. You should stop using it, as it will be removed in the future.
10711071
*/
10721072
public function testPrivateServiceTriggersDeprecation()
10731073
{

Tests/Fixtures/php/container_alias_deprecation.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ protected function getFooService()
6666
*/
6767
protected function getAliasForFooDeprecatedService()
6868
{
69-
@trigger_error('The "alias_for_foo_deprecated" service alias is deprecated. You should stop using it, as it will soon be removed.', E_USER_DEPRECATED);
69+
@trigger_error('The "alias_for_foo_deprecated" service alias is deprecated. You should stop using it, as it will be removed in the future.', E_USER_DEPRECATED);
7070

7171
return $this->get('foo');
7272
}

0 commit comments

Comments
 (0)