Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: symfony/yaml
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v4.4.18
Choose a base ref
...
head repository: symfony/yaml
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v4.4.19
Choose a head ref
  • 14 commits
  • 12 files changed
  • 5 contributors

Commits on Dec 26, 2020

  1. Copy the full SHA
    3181afc View commit details

Commits on Dec 31, 2020

  1. Copy the full SHA
    2fcf81b View commit details

Commits on Jan 1, 2021

  1. Bump license year

    fabpot committed Jan 1, 2021
    Copy the full SHA
    dcd7108 View commit details
  2. bug #39668 [Yaml] do not dump extra trailing newlines for multiline b…

    …locks (xabbuh)
    
    This PR was merged into the 4.4 branch.
    
    Discussion
    ----------
    
    [Yaml] do not dump extra trailing newlines for multiline blocks
    
    | Q             | A
    | ------------- | ---
    | Branch?       | 4.4
    | Bug fix?      | yes
    | New feature?  | no
    | Deprecations? | no
    | Tickets       | Fix #38310
    | License       | MIT
    | Doc PR        |
    
    Commits
    -------
    
    5fa9592d5e do not dump extra trailing newlines for multiline blocks
    fabpot committed Jan 1, 2021
    Copy the full SHA
    22beebc View commit details

Commits on Jan 2, 2021

  1. Copy the full SHA
    3fda4df View commit details

Commits on Jan 9, 2021

  1. Copy the full SHA
    8e9fcae View commit details

Commits on Jan 10, 2021

  1. Copy the full SHA
    498ad4e View commit details
  2. minor #39773 Improve composer.json descriptions (fabpot)

    This PR was merged into the 4.4 branch.
    
    Discussion
    ----------
    
    Improve composer.json descriptions
    
    | Q             | A
    | ------------- | ---
    | Branch?       | 4.4 <!-- see below -->
    | Bug fix?      | no
    | New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
    | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
    | Tickets       | Refs #39768 <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead -->
    | License       | MIT
    | Doc PR        | n/a
    
    Commits
    -------
    
    611a2dfaf3 Improve composer.json descriptions
    fabpot committed Jan 10, 2021
    Copy the full SHA
    9bbbc93 View commit details
  3. Copy the full SHA
    471d834 View commit details

Commits on Jan 11, 2021

  1. minor #39775 [WIP] Use ::class keyword when possible (fabpot)

    This PR was merged into the 4.4 branch.
    
    Discussion
    ----------
    
    [WIP] Use ::class keyword when possible
    
    | Q             | A
    | ------------- | ---
    | Branch?       | 4.4 <!-- see below -->
    | Bug fix?      | no
    | New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
    | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
    | Tickets       | n/a  <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead -->
    | License       | MIT
    | Doc PR        | n/a
    
    Commits
    -------
    
    036a36cb14 Use ::class keyword when possible
    fabpot committed Jan 11, 2021
    Copy the full SHA
    f1c4a3c View commit details
  2. Copy the full SHA
    22640fb View commit details
  3. Copy the full SHA
    dd3f776 View commit details

Commits on Jan 24, 2021

  1. Copy the full SHA
    a170990 View commit details

Commits on Jan 27, 2021

  1. Copy the full SHA
    17ed9f1 View commit details
25 changes: 21 additions & 4 deletions Dumper.php
Original file line number Diff line number Diff line change
@@ -64,14 +64,31 @@ public function dump($input, int $inline = 0, int $indent = 0, int $flags = 0):
$dumpAsMap = Inline::isHash($input);

foreach ($input as $key => $value) {
if ('' !== $output && "\n" !== $output[-1]) {
$output .= "\n";
}

if ($inline >= 1 && Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK & $flags && \is_string($value) && false !== strpos($value, "\n") && false === strpos($value, "\r")) {
// If the first line starts with a space character, the spec requires a blockIndicationIndicator
// http://www.yaml.org/spec/1.2/spec.html#id2793979
$blockIndentationIndicator = (' ' === substr($value, 0, 1)) ? (string) $this->indentation : '';
$output .= sprintf("%s%s%s |%s\n", $prefix, $dumpAsMap ? Inline::dump($key, $flags).':' : '-', '', $blockIndentationIndicator);

if (isset($value[-2]) && "\n" === $value[-2] && "\n" === $value[-1]) {
$blockChompingIndicator = '+';
} elseif ("\n" === $value[-1]) {
$blockChompingIndicator = '';
} else {
$blockChompingIndicator = '-';
}

$output .= sprintf('%s%s%s |%s%s', $prefix, $dumpAsMap ? Inline::dump($key, $flags).':' : '-', '', $blockIndentationIndicator, $blockChompingIndicator);

foreach (explode("\n", $value) as $row) {
$output .= sprintf("%s%s%s\n", $prefix, str_repeat(' ', $this->indentation), $row);
if ('' === $row) {
$output .= "\n";
} else {
$output .= sprintf("\n%s%s%s", $prefix, str_repeat(' ', $this->indentation), $row);
}
}

continue;
@@ -84,10 +101,10 @@ public function dump($input, int $inline = 0, int $indent = 0, int $flags = 0):
// If the first line starts with a space character, the spec requires a blockIndicationIndicator
// http://www.yaml.org/spec/1.2/spec.html#id2793979
$blockIndentationIndicator = (' ' === substr($value->getValue(), 0, 1)) ? (string) $this->indentation : '';
$output .= sprintf(" |%s\n", $blockIndentationIndicator);
$output .= sprintf(' |%s', $blockIndentationIndicator);

foreach (explode("\n", $value->getValue()) as $row) {
$output .= sprintf("%s%s%s\n", $prefix, str_repeat(' ', $this->indentation), $row);
$output .= sprintf("\n%s%s%s", $prefix, str_repeat(' ', $this->indentation), $row);
}

continue;
8 changes: 4 additions & 4 deletions Escaper.php
Original file line number Diff line number Diff line change
@@ -28,15 +28,15 @@ class Escaper
// first to ensure proper escaping because str_replace operates iteratively
// on the input arrays. This ordering of the characters avoids the use of strtr,
// which performs more slowly.
private static $escapees = ['\\', '\\\\', '\\"', '"',
private const ESCAPEES = ['\\', '\\\\', '\\"', '"',
"\x00", "\x01", "\x02", "\x03", "\x04", "\x05", "\x06", "\x07",
"\x08", "\x09", "\x0a", "\x0b", "\x0c", "\x0d", "\x0e", "\x0f",
"\x10", "\x11", "\x12", "\x13", "\x14", "\x15", "\x16", "\x17",
"\x18", "\x19", "\x1a", "\x1b", "\x1c", "\x1d", "\x1e", "\x1f",
"\x7f",
"\xc2\x85", "\xc2\xa0", "\xe2\x80\xa8", "\xe2\x80\xa9",
];
private static $escaped = ['\\\\', '\\"', '\\\\', '\\"',
private const ESCAPED = ['\\\\', '\\"', '\\\\', '\\"',
'\\0', '\\x01', '\\x02', '\\x03', '\\x04', '\\x05', '\\x06', '\\a',
'\\b', '\\t', '\\n', '\\v', '\\f', '\\r', '\\x0e', '\\x0f',
'\\x10', '\\x11', '\\x12', '\\x13', '\\x14', '\\x15', '\\x16', '\\x17',
@@ -66,7 +66,7 @@ public static function requiresDoubleQuoting(string $value): bool
*/
public static function escapeWithDoubleQuotes(string $value): string
{
return sprintf('"%s"', str_replace(self::$escapees, self::$escaped, $value));
return sprintf('"%s"', str_replace(self::ESCAPEES, self::ESCAPED, $value));
}

/**
@@ -86,7 +86,7 @@ public static function requiresSingleQuoting(string $value): bool

// Determines if the PHP value contains any single characters that would
// cause it to require single quoting in YAML.
return 0 < preg_match('/[ \s \' " \: \{ \} \[ \] , & \* \# \?] | \A[ \- ? | < > = ! % @ ` ]/x', $value);
return 0 < preg_match('/[ \s \' " \: \{ \} \[ \] , & \* \# \?] | \A[ \- ? | < > = ! % @ ` \p{Zs}]/xu', $value);
}

/**
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2004-2020 Fabien Potencier
Copyright (c) 2004-2021 Fabien Potencier

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
4 changes: 2 additions & 2 deletions Parser.php
Original file line number Diff line number Diff line change
@@ -200,7 +200,7 @@ private function doParse(string $value, int $flags)
array_pop($this->refsBeingParsed);
}
} elseif (
self::preg_match('#^(?P<key>(?:![^\s]++\s++)?(?:'.Inline::REGEX_QUOTED_STRING.'|(?:!?!php/const:)?[^ \'"\[\{!].*?)) *\:(\s++(?P<value>.+))?$#u', rtrim($this->currentLine), $values)
self::preg_match('#^(?P<key>(?:![^\s]++\s++)?(?:'.Inline::REGEX_QUOTED_STRING.'|(?:!?!php/const:)?[^ \'"\[\{!].*?)) *\:( ++(?P<value>.+))?$#u', rtrim($this->currentLine), $values)
&& (false === strpos($values['key'], ' #') || \in_array($values['key'][0], ['"', "'"]))
) {
if ($context && 'sequence' == $context) {
@@ -724,7 +724,7 @@ private function parseValue(string $value, int $flags, string $context)
}

if (\in_array($value[0], ['!', '|', '>'], true) && self::preg_match('/^(?:'.self::TAG_PATTERN.' +)?'.self::BLOCK_SCALAR_HEADER_PATTERN.'$/', $value, $matches)) {
$modifiers = isset($matches['modifiers']) ? $matches['modifiers'] : '';
$modifiers = $matches['modifiers'] ?? '';

$data = $this->parseBlockScalar($matches['separator'], preg_replace('#\d+#', '', $modifiers), abs((int) $modifiers));

2 changes: 1 addition & 1 deletion Tests/Command/LintCommandTest.php
Original file line number Diff line number Diff line change
@@ -92,7 +92,7 @@ public function testCustomTagsError()

public function testLintFileNotReadable()
{
$this->expectException('RuntimeException');
$this->expectException(\RuntimeException::class);
$tester = $this->createCommandTester();
$filename = $this->createFile('');
unlink($filename);
99 changes: 82 additions & 17 deletions Tests/DumperTest.php
Original file line number Diff line number Diff line change
@@ -13,6 +13,7 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\Yaml\Dumper;
use Symfony\Component\Yaml\Exception\DumpException;
use Symfony\Component\Yaml\Parser;
use Symfony\Component\Yaml\Tag\TaggedValue;
use Symfony\Component\Yaml\Yaml;
@@ -194,7 +195,7 @@ public function testObjectSupportDisabledButNoExceptions()

public function testObjectSupportDisabledWithExceptions()
{
$this->expectException('Symfony\Component\Yaml\Exception\DumpException');
$this->expectException(DumpException::class);
$this->dumper->dump(['foo' => new A(), 'bar' => 1], 0, 0, Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE);
}

@@ -493,19 +494,16 @@ public function testDumpingMultiLineStringAsScalarBlockTaggedValue()
$data = [
'foo' => new TaggedValue('bar', "foo\nline with trailing spaces:\n \nbar\ninteger like line:\n123456789\nempty line:\n\nbaz"),
];
$expected = <<<YAML
foo: !bar |
foo
line with trailing spaces:
bar
integer like line:
123456789
empty line:
baz
YAML;
$expected = "foo: !bar |\n".
" foo\n".
" line with trailing spaces:\n".
" \n".
" bar\n".
" integer like line:\n".
" 123456789\n".
" empty line:\n".
" \n".
' baz';

$this->assertSame($expected, $this->dumper->dump($data, 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK));
}
@@ -545,7 +543,9 @@ public function testDumpMultiLineStringAsScalarBlockWhenFirstLineHasLeadingSpace
],
];

$this->assertSame(file_get_contents(__DIR__.'/Fixtures/multiple_lines_as_literal_block_leading_space_in_first_line.yml'), $this->dumper->dump($data, 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK));
$expected = "data:\n multi_line: |4-\n the first line has leading spaces\n The second line does not.";

$this->assertSame($expected, $this->dumper->dump($data, 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK));
}

public function testCarriageReturnFollowedByNewlineIsMaintainedWhenDumpingAsMultiLineLiteralBlock()
@@ -568,16 +568,66 @@ public function testCarriageReturnNotFollowedByNewlineIsPreservedWhenDumpingAsMu
], 4, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK));
}

public function testNoExtraTrailingNewlineWhenDumpingAsMultiLineLiteralBlock()
{
$data = [
"a\nb",
"c\nd",
];
$yaml = $this->dumper->dump($data, 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK);

$this->assertSame("- |-\n a\n b\n- |-\n c\n d", $yaml);
$this->assertSame($data, Yaml::parse($yaml));
}

public function testDumpTrailingNewlineInMultiLineLiteralBlocks()
{
$data = [
'clip 1' => "one\ntwo\n",
'clip 2' => "one\ntwo\n",
'keep 1' => "one\ntwo\n",
'keep 2' => "one\ntwo\n\n",
'strip 1' => "one\ntwo",
'strip 2' => "one\ntwo",
];
$yaml = $this->dumper->dump($data, 2, 0, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK);

$expected = <<<YAML
'clip 1': |
one
two
'clip 2': |
one
two
'keep 1': |
one
two
'keep 2': |+
one
two
'strip 1': |-
one
two
'strip 2': |-
one
two
YAML;

$this->assertSame($expected, $yaml);
$this->assertSame($data, Yaml::parse($yaml));
}

public function testZeroIndentationThrowsException()
{
$this->expectException('InvalidArgumentException');
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('The indentation must be greater than zero');
new Dumper(0);
}

public function testNegativeIndentationThrowsException()
{
$this->expectException('InvalidArgumentException');
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('The indentation must be greater than zero');
new Dumper(-4);
}
@@ -586,6 +636,21 @@ public function testDumpNullAsTilde()
{
$this->assertSame('{ foo: ~ }', $this->dumper->dump(['foo' => null], 0, 0, Yaml::DUMP_NULL_AS_TILDE));
}

public function testDumpIdeographicSpaces()
{
$expected = <<<YAML
alone: ' '
within_string: 'a b'
regular_space: 'a b'
YAML;
$this->assertSame($expected, $this->dumper->dump([
'alone' => ' ',
'within_string' => 'a b',
'regular_space' => 'a b',
], 2));
}
}

class A
4 changes: 2 additions & 2 deletions Tests/Fixtures/multiple_lines_as_literal_block.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
data:
single_line: 'foo bar baz'
multi_line: |
multi_line: |-
foo
line with trailing spaces:
bar
integer like line:
123456789
empty line:
baz
multi_line_with_carriage_return: "foo\nbar\r\nbaz"
nested_inlined_multi_line_string: { inlined_multi_line: "foo\nbar\r\nempty line:\n\nbaz" }

This file was deleted.

Loading