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 81b59b9

Browse filesBrowse files
committed
bug #17978 [Yaml] ensure dump indentation to be greather than zero (xabbuh)
This PR was merged into the 2.3 branch. Discussion ---------- [Yaml] ensure dump indentation to be greather than zero | Q | A | ------------- | --- | Branch | 2.3 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #17943 (comment), #17977 | License | MIT | Doc PR | Commits ------- 3464282 ensure dump indentation to be greather than zero
2 parents b5a3a56 + 3464282 commit 81b59b9
Copy full SHA for 81b59b9

File tree

4 files changed

+44
-0
lines changed
Filter options

4 files changed

+44
-0
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/Dumper.php
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ class Dumper
3232
*/
3333
public function setIndentation($num)
3434
{
35+
if ($num < 1) {
36+
throw new \InvalidArgumentException('The indentation must be greater than zero.');
37+
}
38+
3539
$this->indentation = (int) $num;
3640
}
3741

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/Tests/DumperTest.php
+18Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,24 @@ public function getEscapeSequences()
229229
'paragraph-separator' => array("\t\\P", '"\t\\\\P"'),
230230
);
231231
}
232+
233+
/**
234+
* @expectedException \InvalidArgumentException
235+
* @expectedExceptionMessage The indentation must be greater than zero
236+
*/
237+
public function testZeroIndentationThrowsException()
238+
{
239+
$this->dumper->setIndentation(0);
240+
}
241+
242+
/**
243+
* @expectedException \InvalidArgumentException
244+
* @expectedExceptionMessage The indentation must be greater than zero
245+
*/
246+
public function testNegativeIndentationThrowsException()
247+
{
248+
$this->dumper->setIndentation(-4);
249+
}
232250
}
233251

234252
class A

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/Tests/YamlTest.php
+18Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,22 @@ public function testParseAndDump()
2828
$parsedByContents = Yaml::parse($contents);
2929
$this->assertEquals($parsedByFilename, $parsedByContents);
3030
}
31+
32+
/**
33+
* @expectedException \InvalidArgumentException
34+
* @expectedExceptionMessage The indentation must be greater than zero
35+
*/
36+
public function testZeroIndentationThrowsException()
37+
{
38+
Yaml::dump(array('lorem' => 'ipsum', 'dolor' => 'sit'), 2, 0);
39+
}
40+
41+
/**
42+
* @expectedException \InvalidArgumentException
43+
* @expectedExceptionMessage The indentation must be greater than zero
44+
*/
45+
public function testNegativeIndentationThrowsException()
46+
{
47+
Yaml::dump(array('lorem' => 'ipsum', 'dolor' => 'sit'), 2, -4);
48+
}
3149
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Yaml/Yaml.php
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ public static function parse($input, $exceptionOnInvalidType = false, $objectSup
8383
*/
8484
public static function dump($array, $inline = 2, $indent = 4, $exceptionOnInvalidType = false, $objectSupport = false)
8585
{
86+
if ($indent < 1) {
87+
throw new \InvalidArgumentException('The indentation must be greater than zero.');
88+
}
89+
8690
$yaml = new Dumper();
8791
$yaml->setIndentation($indent);
8892

0 commit comments

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