From fa398e14c78954319fb6c8ff19ef80129cc1fc85 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Wed, 29 Apr 2015 22:42:57 +0100 Subject: [PATCH] Fix indent detection in json files when an empty line starts the object --- src/Composer/Json/JsonManipulator.php | 2 +- .../Test/Json/JsonManipulatorTest.php | 22 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/Composer/Json/JsonManipulator.php b/src/Composer/Json/JsonManipulator.php index 67020ffad..7099b7ffd 100644 --- a/src/Composer/Json/JsonManipulator.php +++ b/src/Composer/Json/JsonManipulator.php @@ -358,7 +358,7 @@ class JsonManipulator protected function detectIndenting() { - if ($this->pregMatch('{^(\s+)"}m', $this->contents, $match)) { + if ($this->pregMatch('{^([ \t]+)"}m', $this->contents, $match)) { $this->indent = $match[1]; } else { $this->indent = ' '; diff --git a/tests/Composer/Test/Json/JsonManipulatorTest.php b/tests/Composer/Test/Json/JsonManipulatorTest.php index 26c82f28e..a2054e719 100644 --- a/tests/Composer/Test/Json/JsonManipulatorTest.php +++ b/tests/Composer/Test/Json/JsonManipulatorTest.php @@ -1112,6 +1112,28 @@ class JsonManipulatorTest extends \PHPUnit_Framework_TestCase "foo": "qux" } } +', $manipulator->getContents()); + } + + public function testIndentDetection() + { + $manipulator = new JsonManipulator('{ + + "require": { + "php": "5.*" + } +}'); + + $this->assertTrue($manipulator->addMainKey('require-dev', array('foo' => 'qux'))); + $this->assertEquals('{ + + "require": { + "php": "5.*" + }, + "require-dev": { + "foo": "qux" + } +} ', $manipulator->getContents()); } }