1818use Symfony \Component \Config \Definition \NodeInterface ;
1919use Symfony \Component \Config \Definition \PrototypedArrayNode ;
2020use Symfony \Component \Config \Definition \ScalarNode ;
21- use Symfony \Component \Config \Definition \VariableNode ;
2221use 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 }
0 commit comments