Skip to content

Commit ef736ad

Browse files
Merge branch '4.2'
2 parents b90f49f + d009f2e commit ef736ad

File tree

4 files changed

+50
-8
lines changed

4 files changed

+50
-8
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111

1212
strategy:
1313
matrix:
14-
php: ['7.1', '7.2', '7.3', '7.4', '8.0', '8.1']
14+
php: ['7.1', '7.2', '7.3', '7.4', '8.0']
1515

1616
steps:
1717
- name: Checkout Code

src/Parser/EntryParser.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ final class EntryParser
1919
private const ESCAPE_SEQUENCE_STATE = 4;
2020
private const WHITESPACE_STATE = 5;
2121
private const COMMENT_STATE = 6;
22+
private const REJECT_STATES = [self::SINGLE_QUOTED_STATE, self::DOUBLE_QUOTED_STATE, self::ESCAPE_SEQUENCE_STATE];
2223

2324
/**
2425
* This class is a singleton.
@@ -156,16 +157,20 @@ private static function parseValue(string $value)
156157
return Success::create(Value::blank());
157158
}
158159

159-
return \array_reduce(\iterator_to_array(Lexer::lex($value)), static function (Result $data, string $token) use ($value) {
160-
return $data->flatMap(static function (array $data) use ($token, $value) {
161-
return self::processToken($data[1], $token)->mapError(static function (string $err) use ($value) {
162-
return self::getErrorMessage($err, $value);
163-
})->map(static function (array $val) use ($data) {
160+
return \array_reduce(\iterator_to_array(Lexer::lex($value)), static function (Result $data, string $token) {
161+
return $data->flatMap(static function (array $data) use ($token) {
162+
return self::processToken($data[1], $token)->map(static function (array $val) use ($data) {
164163
return [$data[0]->append($val[0], $val[1]), $val[2]];
165164
});
166165
});
167-
}, Success::create([Value::blank(), self::INITIAL_STATE]))->map(static function (array $data) {
168-
return $data[0];
166+
}, Success::create([Value::blank(), self::INITIAL_STATE]))->flatMap(static function (array $result) {
167+
if (in_array($result[1], self::REJECT_STATES, true)) {
168+
return Error::create('a missing closing quote');
169+
}
170+
171+
return Success::create($result[0]);
172+
})->mapError(static function (string $err) use ($value) {
173+
return self::getErrorMessage($err, $value);
169174
});
170175
}
171176

tests/Dotenv/Parser/EntryParserTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,30 @@ public function testParserEscapingSingle()
157157
$this->checkPositiveResult($result, 'FOO_BAD', 'iiiiviiiixiiiiviiii\\a');
158158
}
159159

160+
public function testParserMissingClosingSingleQuote()
161+
{
162+
$result = EntryParser::parse('TEST=\'erert');
163+
$this->checkErrorResult($result, 'Encountered a missing closing quote at [\'erert].');
164+
}
165+
166+
public function testParserMissingClosingDoubleQuote()
167+
{
168+
$result = EntryParser::parse('TEST="erert');
169+
$this->checkErrorResult($result, 'Encountered a missing closing quote at ["erert].');
170+
}
171+
172+
public function testParserMissingClosingQuotes()
173+
{
174+
$result = EntryParser::parse("TEST=\"erert\nTEST='erert\n");
175+
$this->checkErrorResult($result, 'Encountered a missing closing quote at ["erert].');
176+
}
177+
178+
public function testParserClosingQuoteWithEscape()
179+
{
180+
$result = EntryParser::parse('TEST="\\');
181+
$this->checkErrorResult($result, 'Encountered a missing closing quote at ["\\].');
182+
}
183+
160184
/**
161185
* @param \GrahamCampbell\ResultType\Result<\Dotenv\Parser\Entry,string> $result
162186
* @param string $name

tests/Dotenv/Parser/LinesTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,17 @@ public function testProcessClosingSlash()
6262

6363
self::assertSame($expected, $lines);
6464
}
65+
66+
public function testProcessBadQuotes()
67+
{
68+
$lines = [
69+
"TEST=\"erert\nTEST='erert\n",
70+
];
71+
72+
$expected = [
73+
"TEST=\"erert\nTEST='erert\n",
74+
];
75+
76+
self::assertSame($expected, $lines);
77+
}
6578
}

0 commit comments

Comments
 (0)