Skip to content

Commit

Permalink
Fixed detection of closing multiline-quotes (#342)
Browse files Browse the repository at this point in the history
  • Loading branch information
GrahamCampbell authored Jan 30, 2019
1 parent be85d43 commit 1ee9369
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
17 changes: 10 additions & 7 deletions src/Lines.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ public static function process(array $lines)
private static function multilineProcess($multiline, $line, array $buffer)
{
// check if $line can be multiline variable
if (self::looksLikeMultilineStart($line)) {
if ($started = self::looksLikeMultilineStart($line)) {
$multiline = true;
}

if ($multiline) {
array_push($buffer, $line);

if (self::looksLikeMultilineStop($line)) {
if (self::looksLikeMultilineStop($line, $started)) {
$multiline = false;
$line = implode("\n", $buffer);
$buffer = [];
Expand All @@ -72,29 +72,32 @@ private static function looksLikeMultilineStart($line)
return false;
}

return self::looksLikeMultilineStop($line) === false;
return self::looksLikeMultilineStop($line, true) === false;
}

/**
* Determine if the given line can be the start of a multiline variable.
*
* @param string $line
* @param bool $started
*
* @return bool
*/
private static function looksLikeMultilineStop($line)
private static function looksLikeMultilineStop($line, $started)
{
if ($line === '"') {
return true;
}

$seen = $started ? 0 : 1;

foreach (self::getCharPairs(str_replace('\\\\', '', $line)) as $pair) {
if ($pair[0] !== '\\' && $pair[0] !== '=' && $pair[1] === '"') {
return true;
if ($pair[0] !== '\\' && $pair[1] === '"') {
$seen++;
}
}

return false;
return $seen > 1;
}

/**
Expand Down
2 changes: 2 additions & 0 deletions tests/Dotenv/DotenvTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,8 @@ public function testMutlilineLoading()
$dotenv = Dotenv::create($this->fixturesFolder, 'multiline.env');
$dotenv->load();
$this->assertSame("test\n test\"test\"\n test", getenv('TEST'));
$this->assertSame('https://vision.googleapis.com/v1/images:annotate?key=', getenv('TEST_EQD'));
$this->assertSame('https://vision.googleapis.com/v1/images:annotate?key=', getenv('TEST_EQS'));
}

public function testDotenvAssertions()
Expand Down
2 changes: 2 additions & 0 deletions tests/Dotenv/LinesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public function testProcessQuotes()

$expected = [
"TEST=\"test\n test\\\"test\\\"\n test\"",
'TEST_EQD="https://vision.googleapis.com/v1/images:annotate?key="',
'TEST_EQS=\'https://vision.googleapis.com/v1/images:annotate?key=\'',
];

$this->assertSame($expected, Lines::process(preg_split("/(\r\n|\n|\r)/", $content)));
Expand Down
3 changes: 3 additions & 0 deletions tests/fixtures/env/multiline.env
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
TEST="test
test\"test\"
test"

TEST_EQD="https://vision.googleapis.com/v1/images:annotate?key="
TEST_EQS='https://vision.googleapis.com/v1/images:annotate?key='

0 comments on commit 1ee9369

Please sign in to comment.