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

[Yaml] chomp newlines only at the end of YAML documents #18828

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

Merged
merged 1 commit into from
May 23, 2016
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions 26 src/Symfony/Component/Yaml/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Parser
const FOLDED_SCALAR_PATTERN = self::BLOCK_SCALAR_HEADER_PATTERN;

private $offset = 0;
private $totalNumberOfLines;
private $lines = array();
private $currentLineNb = -1;
private $currentLine = '';
Expand All @@ -33,11 +34,13 @@ class Parser
/**
* Constructor.
*
* @param int $offset The offset of YAML document (used for line numbers in error messages)
* @param int $offset The offset of YAML document (used for line numbers in error messages)
* @param int|null $totalNumberOfLines The overall number of lines being parsed
*/
public function __construct($offset = 0)
public function __construct($offset = 0, $totalNumberOfLines = null)
{
$this->offset = $offset;
$this->totalNumberOfLines = $totalNumberOfLines;
}

/**
Expand All @@ -61,6 +64,10 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
$value = $this->cleanup($value);
$this->lines = explode("\n", $value);

if (null === $this->totalNumberOfLines) {
$this->totalNumberOfLines = count($this->lines);
}

if (function_exists('mb_internal_encoding') && ((int) ini_get('mbstring.func_overload')) & 2) {
$mbEncoding = mb_internal_encoding();
mb_internal_encoding('UTF-8');
Expand Down Expand Up @@ -93,7 +100,7 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
// array
if (!isset($values['value']) || '' == trim($values['value'], ' ') || 0 === strpos(ltrim($values['value'], ' '), '#')) {
$c = $this->getRealCurrentLineNb() + 1;
$parser = new self($c);
$parser = new self($c, $this->totalNumberOfLines);
$parser->refs = &$this->refs;
$data[] = $parser->parse($this->getNextEmbedBlock(null, true), $exceptionOnInvalidType, $objectSupport);
} else {
Expand All @@ -102,7 +109,7 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
) {
// this is a compact notation element, add to next block and parse
$c = $this->getRealCurrentLineNb();
$parser = new self($c);
$parser = new self($c, $this->totalNumberOfLines);
$parser->refs = &$this->refs;

$block = $values['value'];
Expand Down Expand Up @@ -153,7 +160,7 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
$value = $this->getNextEmbedBlock();
}
$c = $this->getRealCurrentLineNb() + 1;
$parser = new self($c);
$parser = new self($c, $this->totalNumberOfLines);
$parser->refs = &$this->refs;
$parsed = $parser->parse($value, $exceptionOnInvalidType, $objectSupport);

Expand Down Expand Up @@ -190,7 +197,7 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
$data[$key] = null;
} else {
$c = $this->getRealCurrentLineNb() + 1;
$parser = new self($c);
$parser = new self($c, $this->totalNumberOfLines);
$parser->refs = &$this->refs;
$data[$key] = $parser->parse($this->getNextEmbedBlock(), $exceptionOnInvalidType, $objectSupport);
}
Expand Down Expand Up @@ -528,6 +535,8 @@ private function parseBlockScalar($style, $chomping = '', $indentation = 0)
if ($notEOF) {
$blockLines[] = '';
$this->moveToPreviousLine();
} elseif (!$notEOF && !$this->isCurrentLineLastLineInDocument()) {
$blockLines[] = '';
}

// folded style
Expand Down Expand Up @@ -634,6 +643,11 @@ private function isCurrentLineComment()
return '' !== $ltrimmedLine && $ltrimmedLine[0] === '#';
}

private function isCurrentLineLastLineInDocument()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's "nitpick" a bit: would isLastDocumentLine() be a better name for isCurrentLineLastLineInDocument()?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure, we don't pass any argument so it's not really clear to what the method name refers to imo. Note that the isCurrentLine prefix is a common pattern in the parser (e.g. isCurrentLineComment(), isCurrentLineBlank() and so on).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK then.

{
return ($this->offset + $this->currentLineNb) >= ($this->totalNumberOfLines - 1);
}

/**
* Cleanups a YAML string to be parsed.
*
Expand Down
3 changes: 2 additions & 1 deletion 3 src/Symfony/Component/Yaml/Tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,7 @@ public function getCommentLikeStringInScalarBlockData()
foo
# bar
baz

EOT
,
),
Expand Down Expand Up @@ -854,7 +855,7 @@ public function getCommentLikeStringInScalarBlockData()
$expected = array(
'foo' => array(
'bar' => array(
'scalar-block' => 'line1 line2>',
'scalar-block' => "line1 line2>\n",
),
'baz' => array(
'foobar' => null,
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.