Skip to content
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

[Config] Fix YamlReferenceDumper handling of array examples #54372

Merged
merged 1 commit into from Mar 23, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -18,7 +18,6 @@
use Symfony\Component\Config\Definition\NodeInterface;
use Symfony\Component\Config\Definition\PrototypedArrayNode;
use Symfony\Component\Config\Definition\ScalarNode;
use Symfony\Component\Config\Definition\VariableNode;
use Symfony\Component\Yaml\Inline;

/**
Expand Down Expand Up @@ -90,19 +89,12 @@ private function writeNode(NodeInterface $node, ?NodeInterface $parentNode = nul
$children = $this->getPrototypeChildren($node);
}

if (!$children) {
if ($node->hasDefaultValue() && \count($defaultArray = $node->getDefaultValue())) {
$default = '';
} elseif (!\is_array($example)) {
$default = '[]';
}
if (!$children && !($node->hasDefaultValue() && \count($defaultArray = $node->getDefaultValue()))) {
$default = '[]';
}
} elseif ($node instanceof EnumNode) {
$comments[] = 'One of '.implode('; ', array_map('json_encode', $node->getValues()));
$default = $node->hasDefaultValue() ? Inline::dump($node->getDefaultValue()) : '~';
} elseif (VariableNode::class === \get_class($node) && \is_array($example)) {
// If there is an array example, we are sure we dont need to print a default value
$default = '';
} else {
$default = '~';

Expand Down Expand Up @@ -170,7 +162,7 @@ private function writeNode(NodeInterface $node, ?NodeInterface $parentNode = nul

$this->writeLine('# '.$message.':', $depth * 4 + 4);

$this->writeArray(array_map([Inline::class, 'dump'], $example), $depth + 1);
$this->writeArray(array_map([Inline::class, 'dump'], $example), $depth + 1, true);
}

if ($children) {
Expand All @@ -191,7 +183,7 @@ private function writeLine(string $text, int $indent = 0)
$this->reference .= sprintf($format, $text)."\n";
}

private function writeArray(array $array, int $depth)
private function writeArray(array $array, int $depth, bool $asComment = false)
{
$isIndexed = array_values($array) === $array;

Expand All @@ -202,14 +194,16 @@ private function writeArray(array $array, int $depth)
$val = $value;
}

$prefix = $asComment ? '# ' : '';

if ($isIndexed) {
$this->writeLine('- '.$val, $depth * 4);
$this->writeLine($prefix.'- '.$val, $depth * 4);
} else {
$this->writeLine(sprintf('%-20s %s', $key.':', $val), $depth * 4);
$this->writeLine(sprintf('%s%-20s %s', $prefix, $key.':', $val), $depth * 4);
}

if (\is_array($value)) {
$this->writeArray($value, $depth + 1);
$this->writeArray($value, $depth + 1, $asComment);
}
}
}
Expand Down
Expand Up @@ -109,6 +109,8 @@ enum=""

</pipou>

<array-with-array-example-and-no-default-value />

</config>

EOL
Expand Down
Expand Up @@ -114,11 +114,11 @@ enum: ~ # One of "this"; "that"
# which should be indented
child3: ~ # Example: 'example setting'
scalar_prototyped: []
variable:
variable: ~

# Examples:
- foo
- bar
# - foo
# - bar
parameters:

# Prototype: Parameter name
Expand All @@ -142,6 +142,11 @@ enum: ~ # One of "this"; "that"

# Prototype
name: []
array_with_array_example_and_no_default_value: []

# Examples:
# - foo
# - bar
custom_node: true

EOL;
Expand Down
Expand Up @@ -96,6 +96,9 @@ public function getConfigTreeBuilder(): TreeBuilder
->end()
->end()
->end()
->arrayNode('array_with_array_example_and_no_default_value')
->example(['foo', 'bar'])
->end()
->append(new CustomNodeDefinition('acme'))
->end()
;
Expand Down