8000 [Config] Do not array_unique EnumNode values · symfony/symfony@c9d8b15 · GitHub
[go: up one dir, main page]

Skip to content

Commit c9d8b15

Browse files
committed
[Config] Do not array_unique EnumNode values
1 parent 1f7bc10 commit c9d8b15

File tree

7 files changed

+34
-16
lines changed

7 files changed

+34
-16
lines changed

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

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

428428
if ($node instanceof EnumNode) {
429-
$comment .= sprintf(' * @param ParamConfigurator|%s $value', implode('|', array_map(fn ($a) => var_export($a, true), $node->getValues())))."\n";
429+
$comment .= sprintf(' * @param ParamConfigurator|%s $value', implode('|', array_unique(array_map(fn ($a) => var_export($a, true), $node->getValues()))))."\n";
430430
} else {
431431
$parameterTypes = $this->getParameterTypes($node);
432432
$comment .= ' * @param ParamConfigurator|'.implode('|', $parameterTypes).' $value'."\n";

src/Symfony/Component/Config/Definition/Builder/EnumNodeDefinition.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ class EnumNodeDefinition extends ScalarNodeDefinition
2727
*/
2828
public function values(array $values): static
2929
{
30-
$values = array_unique($values);
31-
3230
if (!$values) {
3331
throw new \InvalidArgumentException('->values() must be called with at least one value.');
3432
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ private function writeNode(NodeInterface $node, int $depth = 0, bool $root = fal
107107
FloatNode::class,
108108
IntegerNode::class => 'numeric value',
109109
BooleanNode::class => 'true|false',
110-
EnumNode::class => implode('|', array_map('json_encode', $prototype->getValues())),
110+
EnumNode::class => implode('|', array_unique(array_map('json_encode', $prototype->getValues()))),
111111
default => 'value',
112112
};
113113
}
@@ -149,7 +149,7 @@ private function writeNode(NodeInterface $node, int $depth = 0, bool $root = fal
149149
}
150150

151151
if ($child instanceof EnumNode) {
152-
$comments[] = 'One of '.implode('; ', array_map('json_encode', $child->getValues()));
152+
$comments[] = 'One of '.implode('; ', array_unique(array_map('json_encode', $child->getValues())));
153153
}
154154

155155
if (\count($comments)) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ private function writeNode(NodeInterface $node, NodeInterface $parentNode = null
9898
}
9999
}
100100
} elseif ($node instanceof EnumNode) {
101-
$comments[] = 'One of '.implode('; ', array_map('json_encode', $node->getValues()));
101+
$comments[] = 'One of '.implode('; ', array_unique(array_map('json_encode', $node->getValues())));
102102
$default = $node->hasDefaultValue() ? Inline::dump($node->getDefaultValue()) : '~';
103103
} elseif (VariableNode::class === $node::class && \is_array($example)) {
104104
// If there is an array example, we are sure we dont need to print a default value

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,16 @@ class EnumNode extends ScalarNode
2424

2525
public function __construct(?string $name, NodeInterface $parent = null, array $values = [], string $pathSeparator = BaseNode::DEFAULT_PATH_SEPARATOR)
2626
{
27-
$values = array_unique($values);
2827
if (!$values) {
2928
throw new \InvalidArgumentException('$values must contain at least one element.');
3029
}
3130

31+
foreach ($values as $value) {
32+
if (null !== $value && !\is_scalar($value)) {
33+
throw new \InvalidArgumentException(sprintf('"%s" only supports scalar or null values, "%s" given.', __CLASS__, get_debug_type($value)));
34+
}
35+
}
36+
3237
parent::__construct($name, $parent, $pathSeparator);
3338
$this->values = $values;
3439
}

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,6 @@ public function testWithOneValue()
2525
$this->assertEquals(['foo'], $node->getValues());
2626
}
2727

28-
public function testWithOneDistinctValue()
29-
{
30-
$def = new EnumNodeDefinition('foo');
31-
$def->values(['foo', 'foo']);
32-
33-
$node = $def->getNode();
34-
$this->assertEquals(['foo'], $node->getValues());
35-
}
36-
3728
public function testNoValuesPassed()
3829
{
3930
$this->expectException(\RuntimeException::class);
@@ -73,4 +64,12 @@ public function testSetDeprecated()
7364
$this->assertSame('vendor/package', $deprecation['package']);
7465
$this->assertSame('1.1', $deprecation['version']);
7566
}
67+
68+
public function testSameStringCoercedValuesAreDifferent()
69+
{
70+
$def = new EnumNodeDefinition('ccc');
71+
$def->values(['', false, null]);
72+
73+
$this->assertSame(['', false, null], $def->getNode()->getValues());
74+
}
7675
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,20 @@ public function testWithPlaceHolderWithInvalidValue()
7171
$this->expectExceptionMessage('The value "foo" is not allowed for path "cookie_samesite". Permissible values: "lax", "strict", "none"');
7272
$node->finalize('custom');
7373
}
74+
75+
public function testSameStringCoercedValuesAreDifferent()
76+
{
77+
$node = new EnumNode('ccc', null, ['', false, null]);
78+
$this->assertSame('', $node->finalize(''));
79+
$this->assertFalse($node->finalize(false));
80+
$this->assertNull($node->finalize(null));
81+
}
82+
83+
public function testNonScalarOrNullValueThrows()
84+
{
85+
$this->expectException(\InvalidArgumentException::class);
86+
$this->expectExceptionMessage('"Symfony\Component\Config\Definition\EnumNode" only supports scalar or null values, "stdClass" given.');
87+
88+
new EnumNode('ccc', null, [new \stdClass()]);
89+
}
7490
}

0 commit comments

Comments
 (0)
0