-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Yaml] Yaml::dump() inserts unneeded newlines for arrays #15782
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
Comments
Both are valid, so there is no bug. |
Euhr.. Nobody said it was a bug, I said "more readable and preferred". Call it an "improvement". |
Revisiting this after a few years… A quick and dirty workaround is to |
Adding to the dirty workaround, using an indent of two spaces aligns the properties of the array item nicely: |
Another version, which may be more robust in edge cases, e.g. if you have text that would cause false positives in the regex match. function ymlDump(mixed $data, $inline = 99, $indent = 2, $flags = Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK): string {
$marker = \uniqid('_', true);
$dumper = new class($marker) extends Dumper {
public function __construct(
private string $marker,
) {
parent::__construct(2);
}
public function dump($input, int $inline = 0, int $indent = 0, int $flags = 0): string {
return $this->marker . parent::dump($input, $inline, $indent, $flags);
}
};
$yml = $dumper->dump($data, $inline, $indent, $flags);
$yml = \preg_replace(
sprintf("@-\n%s +@", \preg_quote($marker, '@')),
'- ',
$yml,
);
$yml = \str_replace($marker, '', $yml);
return $yml;
} |
Note that this only works well if indentation = 2. map:
submap:
a: A
b: B
list: # which of these is better?
- a: A
b: B
- a: A
b: B |
I'm sure I'm not the first to notice, and maybe I didn't search well enough, but couldn't find an existing issue regarding this.
Yaml::dump()
will produce arrays like this:Instead of the more readable and preferred:
I first noticed this using "incenteev/composer-parameter-handler" (no mention of the issue there either), which will produce this:
From a parameters.yml.dist which looks like this:
Obviously the inlining is configurable in
Yaml:dump
, the extra newline, as far as I can tell, is not.The text was updated successfully, but these errors were encountered: