Skip to content

Commit 2978a4e

Browse files
committed
feature #62109 [Config] Ensure configuration nodes do not have both isRequired() and defaultValue() (GromNaN)
This PR was merged into the 8.0 branch. Discussion ---------- [Config] Ensure configuration nodes do not have both `isRequired()` and `defaultValue()` | Q | A | ------------- | --- | Branch? | 8.0 | Bug fix? | no | New feature? | yes | Deprecations? | no | Issues | Fix symfony/symfony#62076 | License | MIT Convert deprecation introduced in symfony/symfony#62090 into an exception. Commits ------- 59f69e0127b [Config] Ensure configuration nodes does not have both isRequired() and defaultValue()
2 parents 810cee1 + 05d7911 commit 2978a4e

File tree

3 files changed

+7
-16
lines changed

3 files changed

+7
-16
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ CHANGELOG
77
* Remove support for accessing the internal scope of the loader in PHP config files, use only its public API instead
88
* Add argument `$singular` to `NodeBuilder::arrayNode()`
99
* Add argument `$info` to `ArrayNodeDefinition::canBeDisabled()` and `canBeEnabled()`
10+
* Ensure configuration nodes do not have both `isRequired()` and `defaultValue()`
1011

1112
7.4
1213
---

Definition/Builder/NodeDefinition.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,7 @@ public function getNode(bool $forceRootNode = false): NodeInterface
179179
public function defaultValue(mixed $value): static
180180
{
181181
if ($this->required) {
182-
// throw new InvalidDefinitionException(sprintf('The node "%s" cannot be required and have a default value.', $this->name));
183-
trigger_deprecation('symfony/config', '7.4', 'Setting a default value to a required node is deprecated. Remove the default value from the node "%s" or make it optional.', $this->name);
182+
throw new InvalidDefinitionException(\sprintf('The node "%s" cannot be required and have a default value.', $this->name));
184183
}
185184

186185
$this->default = true;
@@ -197,8 +196,7 @@ public function defaultValue(mixed $value): static
197196
public function isRequired(): static
198197
{
199198
if ($this->default) {
200-
// throw new InvalidDefinitionException(sprintf('The node "%s" cannot be required and have a default value.', $this->name));
201-
trigger_deprecation('symfony/config', '7.4', 'Flagging a node with a default value as required is deprecated. Remove the default from node "%s" or make it optional.', $this->name);
199+
throw new InvalidDefinitionException(\sprintf('The node "%s" cannot be required and have a default value.', $this->name));
202200
}
203201

204202
$this->required = true;

Tests/Definition/Builder/NodeDefinitionTest.php

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
namespace Symfony\Component\Config\Tests\Definition\Builder;
1313

1414
use PHPUnit\Framework\Attributes\DataProvider;
15-
use PHPUnit\Framework\Attributes\Group;
16-
use PHPUnit\Framework\Attributes\IgnoreDeprecations;
1715
use PHPUnit\Framework\TestCase;
1816
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
1917
use Symfony\Component\Config\Definition\Builder\BooleanNodeDefinition;
@@ -76,32 +74,26 @@ public function testUnknownPackageThrowsException()
7674
$node->docUrl('https://example.com/doc/{package}/{version:major}.{version:minor}', 'phpunit/invalid');
7775
}
7876

79-
#[IgnoreDeprecations]
80-
#[Group('legacy')]
8177
#[DataProvider('provideDefinitionClassesAndDefaultValues')]
8278
public function testIncoherentRequiredAndDefaultValue(string $class, mixed $defaultValue)
8379
{
8480
$node = new $class('foo');
8581
self::assertInstanceOf(NodeDefinition::class, $node);
8682

87-
// $this->expectException(InvalidDefinitionException::class);
88-
// $this->expectExceptionMessage('The node "foo" cannot be required and have a default value.');
89-
$this->expectUserDeprecationMessage('Since symfony/config 7.4: Flagging a node with a default value as required is deprecated. Remove the default from node "foo" or make it optional.');
83+
$this->expectException(InvalidDefinitionException::class);
84+
$this->expectExceptionMessage('The node "foo" cannot be required and have a default value.');
9085

9186
$node->defaultValue($defaultValue)->isRequired();
9287
}
9388

94-
#[IgnoreDeprecations]
95-
#[Group('legacy')]
9689
#[DataProvider('provideDefinitionClassesAndDefaultValues')]
9790
public function testIncoherentDefaultValueAndRequired(string $class, mixed $defaultValue)
9891
{
9992
$node = new $class('foo');
10093
self::assertInstanceOf(NodeDefinition::class, $node);
10194

102-
// $this->expectException(InvalidDefinitionException::class);
103-
// $this->expectExceptionMessage('The node "foo" cannot be required and have a default value.');
104-
$this->expectUserDeprecationMessage('Since symfony/config 7.4: Setting a default value to a required node is deprecated. Remove the default value from the node "foo" or make it optional.');
95+
$this->expectException(InvalidDefinitionException::class);
96+
$this->expectExceptionMessage('The node "foo" cannot be required and have a default value.');
10597

10698
$node->isRequired()->defaultValue($defaultValue);
10799
}

0 commit comments

Comments
 (0)