Skip to content

Commit 8abb4f9

Browse files
Merge pull request #277 from Korbeil/276-variable-values-starting-with-are-being-converted-into-empty-strings
276 variable values starting with are being converted into empty strings
2 parents 6ae3e2e + 5b31537 commit 8abb4f9

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

src/Loader.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -253,14 +253,14 @@ protected function sanitiseVariableValue($name, $value)
253253
$parts = explode(' #', $value, 2);
254254
$value = trim($parts[0]);
255255

256-
// Check if value is a comment (usually triggered when empty value with comment)
257-
if (preg_match('/^#/', $value) > 0) {
258-
$value = '';
259-
}
260-
261256
// Unquoted values cannot contain whitespace
262257
if (preg_match('/\s+/', $value) > 0) {
263-
throw new InvalidFileException('Dotenv values containing spaces must be surrounded by quotes.');
258+
// Check if value is a comment (usually triggered when empty value with comment)
259+
if (preg_match('/^#/', $value) > 0) {
260+
$value = '';
261+
} else {
262+
throw new InvalidFileException('Dotenv values containing spaces must be surrounded by quotes.');
263+
}
264264
}
265265
}
266266

tests/Dotenv/DotenvTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public function testCommentedDotenvLoadsEnvironmentVars()
5252
$this->assertSame('a value with a # character', getenv('CQUOTES'));
5353
$this->assertSame('a value with a # character & a quote " character inside quotes', getenv('CQUOTESWITHQUOTE'));
5454
$this->assertEmpty(getenv('CNULL'));
55+
$this->assertEmpty(getenv('EMPTY'));
5556
}
5657

5758
public function testQuotedDotenvLoadsEnvironmentVars()
@@ -261,23 +262,27 @@ public function testDotenvAssertions()
261262
$this->assertEmpty(getenv('ASSERTVAR2'));
262263
$this->assertEmpty(getenv('ASSERTVAR3'));
263264
$this->assertSame('0', getenv('ASSERTVAR4'));
265+
$this->assertSame('#foo', getenv('ASSERTVAR5'));
264266

265267
$dotenv->required(array(
266268
'ASSERTVAR1',
267269
'ASSERTVAR2',
268270
'ASSERTVAR3',
269271
'ASSERTVAR4',
272+
'ASSERTVAR5',
270273
));
271274

272275
$dotenv->required(array(
273276
'ASSERTVAR1',
274277
'ASSERTVAR4',
278+
'ASSERTVAR5',
275279
))->notEmpty();
276280

277281
$dotenv->required(array(
278282
'ASSERTVAR1',
279283
'ASSERTVAR4',
280-
))->notEmpty()->allowedValues(array('0', 'val1'));
284+
'ASSERTVAR5',
285+
))->notEmpty()->allowedValues(array('0', 'val1', '#foo'));
281286

282287
$this->assertTrue(true); // anything wrong an an exception will be thrown
283288
}

tests/fixtures/env/assertions.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ ASSERTVAR1="val1"
22
ASSERTVAR2=""
33
ASSERTVAR3=" "
44
ASSERTVAR4="0" # empty looking value
5+
ASSERTVAR5=#foo

0 commit comments

Comments
 (0)