Skip to content

Commit 661acb0

Browse files
committed
remove unnecessary \n adding/removing
1 parent ee7e823 commit 661acb0

File tree

2 files changed

+8
-14
lines changed

2 files changed

+8
-14
lines changed

src/Util/YamlSourceManipulator.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -216,14 +216,6 @@ private function updateData(array $newData)
216216
continue;
217217
}
218218

219-
if (
220-
$this->isMultilineString($newVal)
221-
&& rtrim($currentVal, "\n") !== $currentVal
222-
&& rtrim($newVal, "\n") === $newVal
223-
) {
224-
$newVal .= "\n";
225-
}
226-
227219
// 3b) value DID change
228220
$this->log(sprintf('updating value to {%s}', \is_array($newVal) ? '<array>' : $newVal));
229221
$this->changeValueInYaml($newVal);
@@ -458,7 +450,6 @@ private function changeValueInYaml($value)
458450
$endValuePosition = $this->findEndPositionOfValue($originalVal);
459451

460452
$isMultilineValue = null !== $this->findPositionOfMultilineCharInLine($this->currentPosition);
461-
$originalValEndsWithNewLine = $isMultilineValue ? rtrim($originalVal, "\n") !== $originalVal : false;
462453

463454
// In case of multiline, $value is converted as plain string like "Foo\nBar"
464455
// We need to keep it "as is"
@@ -490,7 +481,7 @@ private function changeValueInYaml($value)
490481

491482
$newContents = substr($this->contents, 0, $this->currentPosition)
492483
.($isMultilineValue ? ' |' : '')
493-
.($originalValEndsWithNewLine ? rtrim($newYamlValue, ' ') : $newYamlValue)
484+
.$newYamlValue
494485
/*
495486
* If the next line is a comment, this means we probably had
496487
* a structure that looks like this:
@@ -806,7 +797,7 @@ private function findEndPositionOfValue($value, $offset = null)
806797
$patternValue = $quotedValue;
807798

808799
// Iterates until we find a new line char or we reach end of file
809-
if ($this->findPositionOfMultilineCharInLine($offset)) {
800+
if (null !== $this->findPositionOfMultilineCharInLine($offset)) {
810801
$patternValue = str_replace(["\r\n", "\n"], '\r?\n\s*', $quotedValue);
811802
}
812803

tests/Util/YamlSourceManipulatorTest.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,16 @@ private function getYamlDataTests()
5959
list($source, $changeCode, $expected) = explode('===', $file->getContents());
6060

6161
// Multiline string ends with an \n
62-
$data = Yaml::parse(trim($source, "\n"));
62+
$source = rtrim($source, "\n");
63+
$expected = ltrim($expected, "\n");
64+
65+
$data = Yaml::parse($source);
6366
eval($changeCode);
6467

6568
yield $file->getFilename() => [
66-
'source' => rtrim($source, "\n"),
69+
'source' => $source,
6770
'newData' => $data,
68-
'expectedSource' => ltrim($expected, "\n"),
71+
'expectedSource' => $expected,
6972
];
7073
}
7174

0 commit comments

Comments
 (0)