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 a4b1fa6

Browse filesBrowse files
committed
chomp newlines only at the end of YAML documents
1 parent 76223b2 commit a4b1fa6
Copy full SHA for a4b1fa6

File tree

Expand file treeCollapse file tree

2 files changed

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

2 files changed

+22
-7
lines changed

‎src/Symfony/Component/Yaml/Parser.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/Parser.php
+20-6Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class Parser
2525
const FOLDED_SCALAR_PATTERN = self::BLOCK_SCALAR_HEADER_PATTERN;
2626

2727
private $offset = 0;
28+
private $totalNumberOfLines;
2829
private $lines = array();
2930
private $currentLineNb = -1;
3031
private $currentLine = '';
@@ -33,11 +34,13 @@ class Parser
3334
/**
3435
* Constructor.
3536
*
36-
* @param int $offset The offset of YAML document (used for line numbers in error messages)
37+
* @param int $offset The offset of YAML document (used for line numbers in error messages)
38+
* @param int|null $totalNumberOfLines The overall number of lines being parsed
3739
*/
38-
public function __construct($offset = 0)
40+
public function __construct($offset = 0, $totalNumberOfLines = null)
3941
{
4042
$this->offset = $offset;
43+
$this->totalNumberOfLines = $totalNumberOfLines;
4144
}
4245

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

67+
if (null === $this->totalNumberOfLines) {
68+
$this->totalNumberOfLines = count($this->lines);
69+
}
70+
6471
if (function_exists('mb_internal_encoding') && ((int) ini_get('mbstring.func_overload')) & 2) {
6572
$mbEncoding = mb_internal_encoding();
6673
mb_internal_encoding('UTF-8');
@@ -93,7 +100,7 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
93100
// array
94101
if (!isset($values['value']) || '' == trim($values['value'], ' ') || 0 === strpos(ltrim($values['value'], ' '), '#')) {
95102
$c = $this->getRealCurrentLineNb() + 1;
96-
$parser = new self($c);
103+
$parser = new self($c, $this->totalNumberOfLines);
97104
$parser->refs = &$this->refs;
98105
$data[] = $parser->parse($this->getNextEmbedBlock(null, true), $exceptionOnInvalidType, $objectSupport);
99106
} else {
@@ -102,7 +109,7 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
102109
) {
103110
// this is a compact notation element, add to next block and parse
104111
$c = $this->getRealCurrentLineNb();
105-
$parser = new self($c);
112+
$parser = new self($c, $this->totalNumberOfLines);
106113
$parser->refs = &$this->refs;
107114

108115
$block = $values['value'];
@@ -153,7 +160,7 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
153160
$value = $this->getNextEmbedBlock();
154161
}
155162
$c = $this->getRealCurrentLineNb() + 1;
156-
$parser = new self($c);
163+
$parser = new self($c, $this->totalNumberOfLines);
157164
$parser->refs = &$this->refs;
158165
$parsed = $parser->parse($value, $exceptionOnInvalidType, $objectSupport);
159166

@@ -190,7 +197,7 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
190197
$data[$key] = null;
191198
} else {
192199
$c = $this->getRealCurrentLineNb() + 1;
193-
$parser = new self($c);
200+
$parser = new self($c, $this->totalNumberOfLines);
194201
$parser->refs = &$this->refs;
195202
$data[$key] = $parser->parse($this->getNextEmbedBlock(), $exceptionOnInvalidType, $objectSupport);
196203
}
@@ -528,6 +535,8 @@ private function parseBlockScalar($style, $chomping = '', $indentation = 0)
528535
if ($notEOF) {
529536
$blockLines[] = '';
530537
$this->moveToPreviousLine();
538+
} elseif (!$notEOF && !$this->isCurrentLineLastLineInDocument()) {
539+
$blockLines[] = '';
531540
}
532541

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

646+
private function isCurrentLineLastLineInDocument()
647+
{
648+
return ($this->offset + $this->currentLineNb) >= ($this->totalNumberOfLines - 1);
649+
}
650+
637651
/**
638652
* Cleanups a YAML string to be parsed.
639653
*

‎src/Symfony/Component/Yaml/Tests/ParserTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/Tests/ParserTest.php
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,7 @@ public function getCommentLikeStringInScalarBlockData()
826826
foo
827827
# bar
828828
baz
829+
829830
EOT
830831
,
831832
),
@@ -854,7 +855,7 @@ public function getCommentLikeStringInScalarBlockData()
854855
$expected = array(
855856
'foo' => array(
856857
'bar' => array(
857-
'scalar-block' => 'line1 line2>',
858+
'scalar-block' => "line1 line2>\n",
858859
),
859860
'baz' => array(
860861
'foobar' => null,

0 commit comments

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