Skip to content

Commit ebb4d65

Browse files
GromNaNfabpot
authored andcommitted
[Config] Add Node::getDeprecationMessage() with full message
1 parent 91f8966 commit ebb4d65

File tree

9 files changed

+44
-7
lines changed

9 files changed

+44
-7
lines changed

src/Symfony/Component/Config/Builder/ConfigBuilderGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ private function getComment(BaseNode $node): string
446446
}
447447

448448
if ($node->isDeprecated()) {
449-
$comment .= '@deprecated '.$node->getDeprecation($node->getName(), $node->getParent()->getName())['message']."\n";
449+
$comment .= '@deprecated '.$node->getDeprecationMessage()."\n";
450450
}
451451

452452
return $comment ? ' * '.str_replace("\n", "\n * ", rtrim($comment, "\n"))."\n" : '';

src/Symfony/Component/Config/Definition/BaseNode.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
1717
use Symfony\Component\Config\Definition\Exception\InvalidTypeException;
1818
use Symfony\Component\Config\Definition\Exception\UnsetKeyException;
19+
use Symfony\Component\Config\Exception\LogicException;
1920

2021
/**
2122
* The base node class.
@@ -258,16 +259,39 @@ public function isDeprecated(): bool
258259
/**
259260
* @param string $node The configuration node name
260261
* @param string $path The path of the node
262+
*
263+
* @return array{package: string, version: string, message: string}
261264
*/
262265
public function getDeprecation(string $node, string $path): array
263266
{
267+
if (!$this->deprecation) {
268+
throw new LogicException(\sprintf('The node "%s" is not deprecated.', $this->getName()));
269+
}
270+
264271
return [
265272
'package' => $this->deprecation['package'],
266273
'version' => $this->deprecation['version'],
267274
'message' => strtr($this->deprecation['message'], ['%node%' => $node, '%path%' => $path]),
268275
];
269276
}
270277

278+
/**
279+
* @internal
280+
*/
281+
public function getDeprecationMessage(?NodeInterface $parent = null): string
282+
{
283+
if (!$this->deprecation) {
284+
throw new LogicException(\sprintf('The node "%s" is not deprecated.', $this->getName()));
285+
}
286+
287+
$message = strtr($this->deprecation['message'], ['%node%' => $this->getName(), '%path%' => ($parent ?? $this->parent ?? $this)->getPath()]);
288+
if ($this->deprecation['package'] || $this->deprecation['version']) {
289+
$message = \sprintf('Since %s %s: ', $this->deprecation['package'], $this->deprecation['version']).$message;
290+
}
291+
292+
return $message;
293+
}
294+
271295
public function getName(): string
272296
{
273297
return $this->name;

src/Symfony/Component/Config/Definition/Dumper/XmlReferenceDumper.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,7 @@ private function writeNode(NodeInterface $node, int $depth = 0, bool $root = fal
144144
}
145145

146146
if ($child instanceof BaseNode && $child->isDeprecated()) {
147-
$deprecation = $child->getDeprecation($child->getName(), $node->getPath());
148-
$comments[] = \sprintf('Deprecated (%s)', ($deprecation['package'] || $deprecation['version'] ? "Since {$deprecation['package']} {$deprecation['version']}: " : '').$deprecation['message']);
147+
$comments[] = \sprintf('Deprecated (%s)', $child->getDeprecationMessage($node));
149148
}
150149

151150
if ($child instanceof EnumNode) {

src/Symfony/Component/Config/Definition/Dumper/YamlReferenceDumper.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,7 @@ private function writeNode(NodeInterface $node, ?NodeInterface $parentNode = nul
120120

121121
// deprecated?
122122
if ($node instanceof BaseNode && $node->isDeprecated()) {
123-
$deprecation = $node->getDeprecation($node->getName(), $parentNode ? $parentNode->getPath() : $node->getPath());
124-
$comments[] = \sprintf('Deprecated (%s)', ($deprecation['package'] || $deprecation['version'] ? "Since {$deprecation['package']} {$deprecation['version']}: " : '').$deprecation['message']);
123+
$comments[] = \sprintf('Deprecated (%s)', $node->getDeprecationMessage($parentNode));
125124
}
126125

127126
// example

src/Symfony/Component/Config/Tests/Builder/Fixtures/AddToList/Symfony/Config/AddToList/TranslatorConfig.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function source(string $source_class, mixed $value): static
4343

4444
/**
4545
* looks for translation in old fashion way
46-
* @deprecated The child node "books" at path "translator" is deprecated.
46+
* @deprecated Since symfony/config 6.0: The child node "books" at path "add_to_list.translator" is deprecated.
4747
*/
4848
public function books(array $value = []): \Symfony\Config\AddToList\Translator\BooksConfig
4949
{

src/Symfony/Component/Config/Tests/Definition/ArrayNodeTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ public function testSetDeprecated()
239239
$this->assertSame('"foo" is deprecated', $deprecation['message']);
240240
$this->assertSame('vendor/package', $deprecation['package']);
241241
$this->assertSame('1.1', $deprecation['version']);
242+
$this->assertSame('Since vendor/package 1.1: "foo" is deprecated', $childNode->getDeprecationMessage());
242243

243244
$node = new ArrayNode('root');
244245
$node->addChild($childNode);

src/Symfony/Component/Config/Tests/Definition/Builder/BooleanNodeDefinitionTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,6 @@ public function testSetDeprecated()
6161
$this->assertSame('The "foo" node is deprecated.', $deprecation['message']);
6262
$this->assertSame('vendor/package', $deprecation['package']);
6363
$this->assertSame('1.1', $deprecation['version']);
64+
$this->assertSame('Since vendor/package 1.1: The "foo" node is deprecated.', $node->getDeprecationMessage());
6465
}
6566
}

src/Symfony/Component/Config/Tests/Definition/Builder/EnumNodeDefinitionTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,11 @@ public function testSetDeprecated()
8181
$node = $def->getNode();
8282

8383
$this->assertTrue($node->isDeprecated());
84-
$deprecation = $def->getNode()->getDeprecation($node->getName(), $node->getPath());
84+
$deprecation = $node->getDeprecation($node->getName(), $node->getPath());
8585
$this->assertSame('The "foo" node is deprecated.', $deprecation['message']);
8686
$this->assertSame('vendor/package', $deprecation['package']);
8787
$this->assertSame('1.1', $deprecation['version']);
88+
$this->assertSame('Since vendor/package 1.1: The "foo" node is deprecated.', $node->getDeprecationMessage());
8889
}
8990

9091
public function testSameStringCoercedValuesAreDifferent()

src/Symfony/Component/Config/Tests/Definition/ScalarNodeTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
1818
use Symfony\Component\Config\Definition\Exception\InvalidTypeException;
1919
use Symfony\Component\Config\Definition\ScalarNode;
20+
use Symfony\Component\Config\Exception\LogicException;
2021

2122
class ScalarNodeTest extends TestCase
2223
{
@@ -52,6 +53,7 @@ public function testSetDeprecated()
5253
$this->assertSame('"foo" is deprecated', $deprecation['message']);
5354
$this->assertSame('vendor/package', $deprecation['package']);
5455
$this->assertSame('1.1', $deprecation['version']);
56+
$this->assertSame('Since vendor/package 1.1: "foo" is deprecated', $childNode->getDeprecationMessage());
5557

5658
$node = new ArrayNode('root');
5759
$node->addChild($childNode);
@@ -82,6 +84,16 @@ public function testSetDeprecated()
8284
$this->assertSame(1, $deprecationTriggered, '->finalize() should trigger if the deprecated node is set');
8385
}
8486

87+
public function testNotDeprecatedException()
88+
{
89+
$childNode = new ScalarNode('foo');
90+
91+
$this->assertFalse($childNode->isDeprecated());
92+
$this->expectException(LogicException::class);
93+
$this->expectExceptionMessage('The node "foo" is not deprecated.');
94+
$childNode->getDeprecation('node', 'path');
95+
}
96+
8597
#[DataProvider('getInvalidValues')]
8698
public function testNormalizeThrowsExceptionOnInvalidValues($value)
8799
{

0 commit comments

Comments
 (0)