Skip to content

Commit ed39317

Browse files
committed
enclose multi-line strings in triple quotes
1 parent ae5946e commit ed39317

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

src/Util/StringLiteralFormatter.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ public static function formatValueForRHS($value): string
2222
// Do not treat value as a string if it starts with '$', which indicates that it's a variable name
2323
if (strpos($value, '$') !== 0) {
2424
$value = str_replace('"', '\"', $value);
25-
$value = "\"$value\"";
25+
if (strpos($value, "\n") !== false) {
26+
$value = '"""' . $value . '"""';
27+
} else {
28+
$value = "\"$value\"";
29+
}
2630
}
2731
} elseif (is_bool($value)) {
2832
if ($value) {
@@ -84,4 +88,4 @@ public static function formatLowerCamelCase(string $stringValue): string
8488
{
8589
return lcfirst(static::formatUpperCamelCase($stringValue));
8690
}
87-
}
91+
}

tests/StringLiteralFormatterTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ public function testFormatForClassRHSValue()
3737
$formattedString = StringLiteralFormatter::formatValueForRHS('\'singleQuotes\'');
3838
$this->assertEquals('"\'singleQuotes\'"', $formattedString);
3939

40+
$formattedString = StringLiteralFormatter::formatValueForRHS("with \n newlines");
41+
$this->assertEquals("\"\"\"with \n newlines\"\"\"", $formattedString);
42+
4043
// Integer tests
4144
$integerString = StringLiteralFormatter::formatValueForRHS(25);
4245
$this->assertEquals('25', $integerString);
@@ -112,4 +115,4 @@ public function testFormatLowerCamelCase()
112115
}
113116

114117

115-
}
118+
}

0 commit comments

Comments
 (0)