Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 4d4b552

Browse filesBrowse files
committed
[Config] Fix YamlReferenceDumper handling of array examples
1 parent afc658d commit 4d4b552
Copy full SHA for 4d4b552

File tree

Expand file treeCollapse file tree

4 files changed

+22
-18
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+22
-18
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Config/Definition/Dumper/YamlReferenceDumper.php
+9-15Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
use Symfony\Component\Config\Definition\NodeInterface;
1919
use Symfony\Component\Config\Definition\PrototypedArrayNode;
2020
use Symfony\Component\Config\Definition\ScalarNode;
21-
use Symfony\Component\Config\Definition\VariableNode;
2221
use Symfony\Component\Yaml\Inline;
2322

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

93-
if (!$children) {
94-
if ($node->hasDefaultValue() && \count($defaultArray = $node->getDefaultValue())) {
95-
$default = '';
96-
} elseif (!\is_array($example)) {
97-
$default = '[]';
98-
}
92+
if (!$children && !($node->hasDefaultValue() && \count($defaultArray = $node->getDefaultValue()))) {
93+
$default = '[]';
9994
}
10095
} elseif ($node instanceof EnumNode) {
10196
$comments[] = 'One of '.implode('; ', array_map('json_encode', $node->getValues()));
10297
$default = $node->hasDefaultValue() ? Inline::dump($node->getDefaultValue()) : '~';
103-
} elseif (VariableNode::class === \get_class($node) && \is_array($example)) {
104-
// If there is an array example, we are sure we dont need to print a default value
105-
$default = '';
10698
} else {
10799
$default = '~';
108100

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

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

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

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

194-
private function writeArray(array $array, int $depth)
186+
private function writeArray(array $array, int $depth, bool $asComment = false)
195187
{
196188
$isIndexed = array_values($array) === $array;
197189

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

197+
$prefix = $asComment ? '# ' : '';
198+
205199
if ($isIndexed) {
206-
$this->writeLine('- '.$val, $depth * 4);
200+
$this->writeLine($prefix.'- '.$val, $depth * 4);
207201
} else {
208-
$this->writeLine(sprintf('%-20s %s', $key.':', $val), $depth * 4);
202+
$this->writeLine(sprintf('%s%-20s %s', $prefix, $key.':', $val), $depth * 4);
209203
}
210204

211205
if (\is_array($value)) {
212-
$this->writeArray($value, $depth + 1);
206+
$this->writeArray($value, $depth + 1, $asComment);
213207
}
214208
}
215209
}

‎src/Symfony/Component/Config/Tests/Definition/Dumper/XmlReferenceDumperTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Config/Tests/Definition/Dumper/XmlReferenceDumperTest.php
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ enum=""
109109
110110
</pipou>
111111
112+
<array-with-array-example-and-no-default-value />
113+
112114
</config>
113115

114116
EOL

‎src/Symfony/Component/Config/Tests/Definition/Dumper/YamlReferenceDumperTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Config/Tests/Definition/Dumper/YamlReferenceDumperTest.php
+8-3Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,11 @@ enum: ~ # One of "this"; "that"
114114
# which should be indented
115115
child3: ~ # Example: 'example setting'
116116
scalar_prototyped: []
117-
variable:
117+
variable: ~
118118
119119
# Examples:
120-
- foo
121-
- bar
120+
# - foo
121+
# - bar
122122
parameters:
123123
124124
# Prototype: Parameter name
@@ -142,6 +142,11 @@ enum: ~ # One of "this"; "that"
142142
143143
# Prototype
144144
name: []
145+
array_with_array_example_and_no_default_value: []
146+
147+
# Examples:
148+
# - foo
149+
# - bar
145150
custom_node: true
146151

147152
EOL;

‎src/Symfony/Component/Config/Tests/Fixtures/Configuration/ExampleConfiguration.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Config/Tests/Fixtures/Configuration/ExampleConfiguration.php
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ public function getConfigTreeBuilder(): TreeBuilder
9696
->end()
9797
->end()
9898
->end()
99+
->arrayNode('array_with_array_example_and_no_default_value')
100+
->example(['foo', 'bar'])
101+
->end()
99102
->append(new CustomNodeDefinition('acme'))
100103
->end()
101104
;

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.