10000 [Config] Do not array_unique EnumNode values by fancyweb · Pull Request #48957 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Config] Do not array_unique EnumNode values #48957

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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 @@ -426,7 +426,7 @@ private function getComment(BaseNode $node): string
}

if ($node instanceof EnumNode) {
$comment .= sprintf(' * @param ParamConfigurator|%s $value', implode('|', array_map(fn ($a) => var_export($a, true), $node->getValues())))."\n";
$comment .= sprintf(' * @param ParamConfigurator|%s $value', implode('|', array_unique(array_map(fn ($a) => var_export($a, true), $node->getValues()))))."\n";
} else {
$parameterTypes = $this->getParameterTypes($node);
$comment .= ' * @param ParamConfigurator|'.implode('|', $parameterTypes).' $value'."\n";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ class EnumNodeDefinition extends ScalarNodeDefinition
*/
public function values(array $values): static
{
$values = array_unique($values);

if (!$values) {
throw new \InvalidArgumentException('->values() must be called with at least one value.');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ private function writeNode(NodeInterface $node, int $depth = 0, bool $root = fal
FloatNode::class,
IntegerNode::class => 'numeric value',
BooleanNode::class => 'true|false',
EnumNode::class => implode('|', array_map('json_encode', $prototype->getValues())),
EnumNode::class => implode('|', array_unique(array_map('json_encode', $prototype->getValues()))),
default => 'value',
};
}
Expand Down Expand Up @@ -149,7 +149,7 @@ private function writeNode(NodeInterface $node, int $depth = 0, bool $root = fal
}

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

if (\count($comments)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ private function writeNode(NodeInterface $node, NodeInterface $parentNode = null
}
}
} elseif ($node instanceof EnumNode) {
$comments[] = 'One of '.implode('; ', array_map('json_encode', $node->getValues()));
$comments[] = 'One of '.implode('; ', array_unique(array_map('json_encode', $node->getValues())));
$default = $node->hasDefaultValue() ? Inline::dump($node->getDefaultValue()) : '~';
} elseif (VariableNode::class === $node::class && \is_array($example)) {
// If there is an array example, we are sure we dont need to print a default value
Expand Down
7 changes: 6 additions & 1 deletion src/Symfony/Component/Config/Definition/EnumNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,16 @@ class EnumNode extends ScalarNode

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

foreach ($values as $value) {
if (null !== $value && !\is_scalar($value)) {
throw new \InvalidArgumentException(sprintf('"%s" only supports scalar or null values, "%s" given.', __CLASS__, get_debug_type($value)));
}
}

parent::__construct($name, $parent, $pathSeparator);
$this->values = $values;
}
Expand Down
D967
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,6 @@ public function testWithOneValue()
$this->assertEquals(['foo'], $node->getValues());
}

public function testWithOneDistinctValue()
{
$def = new EnumNodeDefinition('foo');
$def->values(['foo', 'foo']);

$node = $def->getNode();
$this->assertEquals(['foo'], $node->getValues());
}

public function testNoValuesPassed()
{
$this->expectException(\RuntimeException::class);
Expand Down Expand Up @@ -73,4 +64,12 @@ public function testSetDeprecated()
$this->assertSame('vendor/package', $deprecation['package']);
$this->assertSame('1.1', $deprecation['version']);
}

public function testSameStringCoercedValuesAreDifferent()
{
$def = new EnumNodeDefinition('ccc');
$def->values(['', false, null]);

$this->assertSame(['', false, null], $def->getNode()->getValues());
}
}
16 changes: 16 additions & 0 deletions src/Symfony/Component/Config/Tests/Definition/EnumNodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,20 @@ public function testWithPlaceHolderWithInvalidValue()
$this->expectExceptionMessage('The value "foo" is not allowed for path "cookie_samesite". Permissible values: "lax", "strict", "none"');
$node->finalize('custom');
}

public function testSameStringCoercedValuesAreDifferent()
{
$node = new EnumNode('ccc', null, ['', false, null]);
$this->assertSame('', $node->finalize(''));
$this->assertFalse($node->finalize(false));
$this->assertNull($node->finalize(null));
}

public function testNonScalarOrNullValueThrows()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('"Symfony\Component\Config\Definition\EnumNode" only supports scalar or null values, "stdClass" given.');

new EnumNode('ccc', null, [new \stdClass()]);
}
}
0