Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ private function getComment(BaseNode $node): string
}

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

return $comment ? ' * '.str_replace("\n", "\n * ", rtrim($comment, "\n"))."\n" : '';
Expand Down
24 changes: 24 additions & 0 deletions src/Symfony/Component/Config/Definition/BaseNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\Config\Definition\Exception\InvalidTypeException;
use Symfony\Component\Config\Definition\Exception\UnsetKeyException;
use Symfony\Component\Config\Exception\LogicException;

/**
* The base node class.
Expand Down Expand Up @@ -258,16 +259,39 @@ public function isDeprecated(): bool
/**
* @param string $node The configuration node name
* @param string $path The path of the node
*
* @return array{package: string, version: string, message: string}
Copy link
Member Author

@GromNaN GromNaN Sep 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can also update the returned message to prepend the package and version. But this could duplicate the "since ..." prefix if its prepended somewhere outside. This method is not used in Symfony outside of the Config component.

*/
public function getDeprecation(string $node, string $path): array
{
if (!$this->deprecation) {
throw new LogicException(\sprintf('The node "%s" is not deprecated.', $this->getName()));
}

return [
'package' => $this->deprecation['package'],
'version' => $this->deprecation['version'],
'message' => strtr($this->deprecation['message'], ['%node%' => $node, '%path%' => $path]),
];
}

/**
* @internal
*/
public function getDeprecationMessage(?NodeInterface $parent = null): string
{
if (!$this->deprecation) {
throw new LogicException(\sprintf('The node "%s" is not deprecated.', $this->getName()));
}

$message = strtr($this->deprecation['message'], ['%node%' => $this->getName(), '%path%' => ($parent ?? $this->parent ?? $this)->getPath()]);
if ($this->deprecation['package'] || $this->deprecation['version']) {
$message = \sprintf('Since %s %s: ', $this->deprecation['package'], $this->deprecation['version']).$message;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would make more sense.

Suggested change
$message = \sprintf('Since %s %s: ', $this->deprecation['package'], $this->deprecation['version']).$message;
$message = \sprintf('Since %s %s: %s', $this->deprecation['package'], $this->deprecation['version'], $message);

}

return $message;
}

public function getName(): string
{
return $this->name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,7 @@ private function writeNode(NodeInterface $node, int $depth = 0, bool $root = fal
}

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

if ($child instanceof EnumNode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,7 @@ private function writeNode(NodeInterface $node, ?NodeInterface $parentNode = nul

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

// example
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function source(string $source_class, mixed $value): static

/**
* looks for translation in old fashion way
* @deprecated The child node "books" at path "translator" is deprecated.
* @deprecated Since symfony/config 6.0: The child node "books" at path "add_to_list.translator" is deprecated.
*/
public function books(array $value = []): \Symfony\Config\AddToList\Translator\BooksConfig
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ public function testSetDeprecated()
$this->assertSame('"foo" is deprecated', $deprecation['message']);
$this->assertSame('vendor/package', $deprecation['package']);
$this->assertSame('1.1', $deprecation['version']);
$this->assertSame('Since vendor/package 1.1: "foo" is deprecated', $childNode->getDeprecationMessage());

$node = new ArrayNode('root');
$node->addChild($childNode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,6 @@ public function testSetDeprecated()
$this->assertSame('The "foo" node is deprecated.', $deprecation['message']);
$this->assertSame('vendor/package', $deprecation['package']);
$this->assertSame('1.1', $deprecation['version']);
$this->assertSame('Since vendor/package 1.1: The "foo" node is deprecated.', $node->getDeprecationMessage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,11 @@ public function testSetDeprecated()
$node = $def->getNode();

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

public function testSameStringCoercedValuesAreDifferent()
Expand Down
12 changes: 12 additions & 0 deletions src/Symfony/Component/Config/Tests/Definition/ScalarNodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\Config\Definition\Exception\InvalidTypeException;
use Symfony\Component\Config\Definition\ScalarNode;
use Symfony\Component\Config\Exception\LogicException;

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

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

public function testNotDeprecatedException()
{
$childNode = new ScalarNode('foo');

$this->assertFalse($childNode->isDeprecated());
$this->expectException(LogicException::class);
$this->expectExceptionMessage('The node "foo" is not deprecated.');
$childNode->getDeprecation('node', 'path');
}

#[DataProvider('getInvalidValues')]
public function testNormalizeThrowsExceptionOnInvalidValues($value)
{
Expand Down
Loading