8000 [2.1][Component][Yaml] fix 4022 by gajdaw · Pull Request #4126 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[2.1][Component][Yaml] fix 4022 #4126

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 8, 2012
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
57 changes: 55 additions & 2 deletions src/Symfony/Component/Yaml/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public function parse($value)
// hash
} elseif (!isset($values['value']) || '' == trim($values['value'], ' ') || 0 === strpos(ltrim($values['value'], ' '), '#')) {
// if next line is less indented or equal, then it means that the current value is null
if ($this->isNextLineIndented()) {
if ($this->isNextLineIndented() && !$this->isNextLineUnIndentedCollection()) {
$data[$key] = null;
} else {
$c = $this->getRealCurrentLineNb() + 1;
Expand Down Expand Up @@ -275,7 +275,9 @@ private function getNextEmbedBlock($indentation = null)
if (null === $indentation) {
$newIndent = $this->getCurrentLineIndentation();

if (!$this->isCurrentLineEmpty() && 0 == $newIndent) {
$unindentedEmbedBlock = $this->isStringUnIndentedCollectionItem($this->currentLine);

if (!$this->isCurrentLineEmpty() && 0 === $newIndent && !$unindentedEmbedBlock) {
throw new ParseException('Indentation problem.', $this->getRealCurrentLineNb() + 1, $this->currentLine);
}
} else {
Expand All @@ -284,7 +286,15 @@ private function getNextEmbedBlock($indentation = null)

$data = array(substr($this->currentLine, $newIndent));

$isItUnindentedCollection = $this->isStringUnIndentedCollectionItem($this->currentLine);

while ($this->moveToNextLine()) {

if ($isItUnindentedCollection && !$this->isStringUnIndentedCollectionItem($this->currentLine)) {
$this->moveToPreviousLine();
break;
}

if ($this->isCurrentLineEmpty()) {
if ($this->isCurrentLineBlank()) {
$data[] = substr($this->currentLine, $newIndent);
Expand Down Expand Up @@ -553,4 +563,47 @@ private function cleanup($value)

return $value;
}

/**
* Returns true if the next line starts unindented collection
*
* @return Boolean Returns true if the next line starts unindented collection, false otherwise
*/
private function isNextLineUnIndentedCollection()
{
$currentIndentation = $this->getCurrentLineIndentation();
$notEOF = $this->moveToNextLine();

while ($notEOF && $this->isCurrentLineEmpty()) {
$notEOF = $this->moveToNextLine();
}

if (false === $notEOF) {
return false;
}

$ret = false;
if (
Copy link
Contributor

Choose a reason for hiding this comment

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

extra line breaks after ( and before )

Copy link
Contributor Author

Choose a reason for hiding this comment

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

When the condition is as long as:

if ($this->getCurrentLineIndentation() == $currentIndentation && $this->isStringUnIndentedCollectionItem($this->currentLine)) {

multiline notation is much easier to read. Should I change it into one long line?

$this->getCurrentLineIndentation() == $currentIndentation
&&
$this->isStringUnIndentedCollectionItem($this->currentLine)
) {
$ret = true;
}

$this->moveToPreviousLine();

return $ret;
}

/**
* Returns true if the string is unindented collection item
*
* @return Boolean Returns true if the string is unindented collection item, false otherwise
*/
private function isStringUnIndentedCollectionItem($string)
{
return (0 === strpos($this->currentLine, '- '));
}

}
1 change: 1 addition & 0 deletions src/Symfony/Component/Yaml/Tests/Fixtures/index.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@
- YtsNullsAndEmpties
- YtsSpecificationExamples
- YtsTypeTransfers
- unindentedCollections
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
--- %YAML:1.0
test: Unindented collection
brief: >
Unindented collection
yaml: |
collection:
- item1
- item2
- item3
php: |
array('collection' => array('item1', 'item2', 'item3'))
---
test: Nested unindented collection (two levels)
brief: >
Nested unindented collection
yaml: |
collection:
key:
- a
- b
- c
php: |
array('collection' => array('key' => array('a', 'b', 'c')))
---
test: Nested unindented collection (three levels)
brief: >
Nested unindented collection
yaml: |
collection:
key:
subkey:
- one
- two
- three
php: |
array('collection' => array('key' => array('subkey' => array('one', 'two', 'three'))))
---
test: Key/value after unindented collection (1)
brief: >
Key/value after unindented collection (1)
yaml: |
collection:
key:
- a
- b
- c
foo: bar
php: |
array('collection' => array('key' => array('a', 'b', 'c')), 'foo' => 'bar')
---
test: Key/value after unindented collection (at the same level)
brief: >
Key/value after unindented collection
yaml: |
collection:
key:
- a
- b
- c
foo: bar
php: |
array('collection' => array('key' => array('a', 'b', 'c'), 'foo' => 'bar'))
20 changes: 20 additions & 0 deletions src/Symfony/Component/Yaml/Tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,26 @@ public function testNonUtf8Exception()
}
}
}

/**
*
* @expectedException Symfony\Component\Yaml\Exception\ParseException
*
*/
public function testUnindentedCollectionException()
{
$yaml = <<<EOF

collection:
-item1
-item2
-item3

EOF;

$this->parser->parse($yaml);
}

}

class B
Expand Down
0