8000 [Yaml] Fix wrong line number when comments are inserted in the middle of a block. by paradajozsef · Pull Request #17733 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Yaml] Fix wrong line number when comments are inserted in the middle of a block. #17733

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

Prev Previous commit
Next Next commit
[Yaml] Fix wrong line number when comments are inserted in the middle…
… of a block.
  • Loading branch information
paradajozsef committed May 6, 2016
commit dac814cc2f45096af92476e910232ef33e8ec283
51 changes: 49 additions & 2 deletions src/Symfony/Component/Yaml/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ class Parser
private $currentLineNb = -1;
private $currentLine = '';
private $refs = array();
private $skippedCommentLines = 0;

/**
* Constructor.
Expand Down Expand Up @@ -155,7 +154,7 @@ public function parse($value, $flags = 0)
try {
$key = Inline::parseScalar($values['key']);
} catch (ParseException $e) {
$e->setParsedLine($this->getRealCurrentLineNb() + $this->skippedCommentLines + 1);
$e->setParsedLine($this->getRealCurrentLineNb() + 1);
$e->setSnippet($this->currentLine);

throw $e;
Expand Down Expand Up @@ -485,10 +484,18 @@ private function moveToNextLine()

/**
* Moves the parser to the previous line.
*
* @return bool
*/
private function moveToPreviousLine()
{
if ($this->currentLineNb < 1) {
return false;
}

$this->currentLine = $this->lines[--$this->currentLineNb];

return true;
}

/**
Expand Down Expand Up @@ -805,4 +812,44 @@ private function isBlockScalarHeader()
{
return (bool) preg_match('~'.self::BLOCK_SCALAR_HEADER_PATTERN.'$~', $this->currentLine);
}

/**
* Returns true if the current line is a collection item.
*
* @return bool Returns true if the current line is a collection item line, false otherwise.
Copy link
< 10000 /details>
Member

Choose a reason for hiding this comment

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

The description of the returnt ype here is not needed (it's already clear from the method's description), just use @return true.

*/
private function isCurrentLineCollectionItem()
{
$ltrimmedLine = ltrim($this->currentLine, ' ');

return '' !== $ltrimmedLine && $ltrimmedLine[0] === '-';
}

/**
* Tests whether or not the current comment line is in a collection.
Copy link
Member

Choose a reason for hiding this comment

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

or not is useless here

*
* @return bool
*/
private function isPreviousNonCommentLineIsCollectionItem()
Copy link
Member

Choose a reason for hiding this comment

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

method name is weird. It contains is twice

{
$isCollectionItem = false;
$moves = 0;
while($this->moveToPreviousLine()) {
Copy link
Member
@xabbuh xabbuh May 10, 2016

Choose a reason for hiding this comment

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

please add a space before the opening parenthesis (right after the while keyword)

++$moves;
// If previous line is a comment, move back again.
if ($this->isCurrentLineComment()) {
continue;
}
$isCollectionItem = $this->isCurrentLineCollectionItem();
break;
}

// Move parser back to previous line.
while($moves > 0) {
Copy link
Member

Choose a reason for hiding this comment

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

same here

$this->moveToNextLine();
--$moves;
}

return $isCollectionItem;
}
}
15 changes: 15 additions & 0 deletions src/Symfony/Component/Yaml/Tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1250,6 +1250,21 @@ public function parserThrowsExceptionWithCorrectLineNumberProvider()
-
# bar
bar: "123",
YAML
),
array(
10,
<<<YAML
foo:
-
# foobar
# foobar
baz: 123
bar:
-
# bar
# bar
bar: "123",
YAML
),
);
Expand Down
0