Skip to content

Commit

Permalink
Merge branch '4.1' into 4.2
Browse files Browse the repository at this point in the history
* 4.1:
  fixed tests
  fixed CS
  fixed CS
  fixed CS
  fixed short array CS in comments
  fixed CS in ExpressionLanguage fixtures
  fixed CS in generated files
  fixed CS on generated container files
  fixed CS on Form PHP templates
  fixed CS on YAML fixtures
  fixed fixtures
  switched array() to []
  • Loading branch information
fabpot committed Jan 16, 2019
2 parents 36e4e47 + 6027ad5 commit 4165a06
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 97 deletions.
10 changes: 5 additions & 5 deletions Dotenv.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,12 @@ public function populate(array $values, bool $overrideExistingVars = false): voi
public function parse(string $data, string $path = '.env'): array
{
$this->path = $path;
$this->data = str_replace(array("\r\n", "\r"), "\n", $data);
$this->data = str_replace(["\r\n", "\r"], "\n", $data);
$this->lineno = 1;
$this->cursor = 0;
$this->end = \strlen($this->data);
$this->state = self::STATE_VARNAME;
$this->values = array();
$this->values = [];
$name = '';

$this->skipEmptyLines();
Expand All @@ -189,7 +189,7 @@ public function parse(string $data, string $path = '.env'): array
try {
return $this->values;
} finally {
$this->values = array();
$this->values = [];
$this->data = null;
$this->path = null;
}
Expand Down Expand Up @@ -275,7 +275,7 @@ private function lexValue()
throw $this->createFormatException('Missing quote to end the value');
}
++$this->cursor;
$value = str_replace(array('\\"', '\r', '\n'), array('"', "\r", "\n"), $value);
$value = str_replace(['\\"', '\r', '\n'], ['"', "\r", "\n"], $value);
$resolvedValue = $value;
$resolvedValue = $this->resolveVariables($resolvedValue);
$resolvedValue = $this->resolveCommands($resolvedValue);
Expand All @@ -284,7 +284,7 @@ private function lexValue()
} else {
$value = '';
$prevChr = $this->data[$this->cursor - 1];
while ($this->cursor < $this->end && !\in_array($this->data[$this->cursor], array("\n", '"', "'"), true) && !((' ' === $prevChr || "\t" === $prevChr) && '#' === $this->data[$this->cursor])) {
while ($this->cursor < $this->end && !\in_array($this->data[$this->cursor], ["\n", '"', "'"], true) && !((' ' === $prevChr || "\t" === $prevChr) && '#' === $this->data[$this->cursor])) {
if ('\\' === $this->data[$this->cursor] && isset($this->data[$this->cursor + 1]) && ('"' === $this->data[$this->cursor + 1] || "'" === $this->data[$this->cursor + 1])) {
++$this->cursor;
}
Expand Down
184 changes: 92 additions & 92 deletions Tests/DotenvTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,18 @@ public function testParseWithFormatError($data, $error)

public function getEnvDataWithFormatErrors()
{
$tests = array(
array('FOO=BAR BAZ', "A value containing spaces must be surrounded by quotes in \".env\" at line 1.\n...FOO=BAR BAZ...\n ^ line 1 offset 11"),
array('FOO BAR=BAR', "Whitespace are not supported after the variable name in \".env\" at line 1.\n...FOO BAR=BAR...\n ^ line 1 offset 3"),
array('FOO', "Missing = in the environment variable declaration in \".env\" at line 1.\n...FOO...\n ^ line 1 offset 3"),
array('FOO="foo', "Missing quote to end the value in \".env\" at line 1.\n...FOO=\"foo...\n ^ line 1 offset 8"),
array('FOO=\'foo', "Missing quote to end the value in \".env\" at line 1.\n...FOO='foo...\n ^ line 1 offset 8"),
array('export FOO', "Unable to unset an environment variable in \".env\" at line 1.\n...export FOO...\n ^ line 1 offset 10"),
array('FOO=${FOO', "Unclosed braces on variable expansion in \".env\" at line 1.\n...FOO=\${FOO...\n ^ line 1 offset 9"),
);
$tests = [
['FOO=BAR BAZ', "A value containing spaces must be surrounded by quotes in \".env\" at line 1.\n...FOO=BAR BAZ...\n ^ line 1 offset 11"],
['FOO BAR=BAR', "Whitespace are not supported after the variable name in \".env\" at line 1.\n...FOO BAR=BAR...\n ^ line 1 offset 3"],
['FOO', "Missing = in the environment variable declaration in \".env\" at line 1.\n...FOO...\n ^ line 1 offset 3"],
['FOO="foo', "Missing quote to end the value in \".env\" at line 1.\n...FOO=\"foo...\n ^ line 1 offset 8"],
['FOO=\'foo', "Missing quote to end the value in \".env\" at line 1.\n...FOO='foo...\n ^ line 1 offset 8"],
['export FOO', "Unable to unset an environment variable in \".env\" at line 1.\n...export FOO...\n ^ line 1 offset 10"],
['FOO=${FOO', "Unclosed braces on variable expansion in \".env\" at line 1.\n...FOO=\${FOO...\n ^ line 1 offset 9"],
];

if ('\\' !== \DIRECTORY_SEPARATOR) {
$tests[] = array('FOO=$((1dd2))', "Issue expanding a command (%s\n) in \".env\" at line 1.\n...FOO=$((1dd2))...\n ^ line 1 offset 13");
$tests[] = ['FOO=$((1dd2))', "Issue expanding a command (%s\n) in \".env\" at line 1.\n...FOO=$((1dd2))...\n ^ line 1 offset 13"];
}

return $tests;
Expand All @@ -65,104 +65,104 @@ public function getEnvData()
putenv('LOCAL=local');
$_ENV['REMOTE'] = 'remote';

$tests = array(
$tests = [
// backslashes
array('FOO=foo\\\\bar', array('FOO' => 'foo\\bar')),
array("FOO='foo\\\\bar'", array('FOO' => 'foo\\\\bar')),
array('FOO="foo\\\\bar"', array('FOO' => 'foo\\bar')),
['FOO=foo\\\\bar', ['FOO' => 'foo\\bar']],
["FOO='foo\\\\bar'", ['FOO' => 'foo\\\\bar']],
['FOO="foo\\\\bar"', ['FOO' => 'foo\\bar']],

// escaped backslash in front of variable
array("BAR=bar\nFOO=foo\\\\\$BAR", array('BAR' => 'bar', 'FOO' => 'foo\\bar')),
array("BAR=bar\nFOO='foo\\\\\$BAR'", array('BAR' => 'bar', 'FOO' => 'foo\\\\$BAR')),
array("BAR=bar\nFOO=\"foo\\\\\$BAR\"", array('BAR' => 'bar', 'FOO' => 'foo\\bar')),
["BAR=bar\nFOO=foo\\\\\$BAR", ['BAR' => 'bar', 'FOO' => 'foo\\bar']],
["BAR=bar\nFOO='foo\\\\\$BAR'", ['BAR' => 'bar', 'FOO' => 'foo\\\\$BAR']],
["BAR=bar\nFOO=\"foo\\\\\$BAR\"", ['BAR' => 'bar', 'FOO' => 'foo\\bar']],

array('FOO=foo\\\\\\$BAR', array('FOO' => 'foo\\$BAR')),
array('FOO=\'foo\\\\\\$BAR\'', array('FOO' => 'foo\\\\\\$BAR')),
array('FOO="foo\\\\\\$BAR"', array('FOO' => 'foo\\$BAR')),
['FOO=foo\\\\\\$BAR', ['FOO' => 'foo\\$BAR']],
['FOO=\'foo\\\\\\$BAR\'', ['FOO' => 'foo\\\\\\$BAR']],
['FOO="foo\\\\\\$BAR"', ['FOO' => 'foo\\$BAR']],

// spaces
array('FOO=bar', array('FOO' => 'bar')),
array(' FOO=bar ', array('FOO' => 'bar')),
array('FOO=', array('FOO' => '')),
array("FOO=\n\n\nBAR=bar", array('FOO' => '', 'BAR' => 'bar')),
array('FOO= ', array('FOO' => '')),
array("FOO=\nBAR=bar", array('FOO' => '', 'BAR' => 'bar')),
['FOO=bar', ['FOO' => 'bar']],
[' FOO=bar ', ['FOO' => 'bar']],
['FOO=', ['FOO' => '']],
["FOO=\n\n\nBAR=bar", ['FOO' => '', 'BAR' => 'bar']],
['FOO= ', ['FOO' => '']],
["FOO=\nBAR=bar", ['FOO' => '', 'BAR' => 'bar']],

// newlines
array("\n\nFOO=bar\r\n\n", array('FOO' => 'bar')),
array("FOO=bar\r\nBAR=foo", array('FOO' => 'bar', 'BAR' => 'foo')),
array("FOO=bar\rBAR=foo", array('FOO' => 'bar', 'BAR' => 'foo')),
array("FOO=bar\nBAR=foo", array('FOO' => 'bar', 'BAR' => 'foo')),
["\n\nFOO=bar\r\n\n", ['FOO' => 'bar']],
["FOO=bar\r\nBAR=foo", ['FOO' => 'bar', 'BAR' => 'foo']],
["FOO=bar\rBAR=foo", ['FOO' => 'bar', 'BAR' => 'foo']],
["FOO=bar\nBAR=foo", ['FOO' => 'bar', 'BAR' => 'foo']],

// quotes
array("FOO=\"bar\"\n", array('FOO' => 'bar')),
array("FOO=\"bar'foo\"\n", array('FOO' => 'bar\'foo')),
array("FOO='bar'\n", array('FOO' => 'bar')),
array("FOO='bar\"foo'\n", array('FOO' => 'bar"foo')),
array("FOO=\"bar\\\"foo\"\n", array('FOO' => 'bar"foo')),
array('FOO="bar\nfoo"', array('FOO' => "bar\nfoo")),
array('FOO="bar\rfoo"', array('FOO' => "bar\rfoo")),
array('FOO=\'bar\nfoo\'', array('FOO' => 'bar\nfoo')),
array('FOO=\'bar\rfoo\'', array('FOO' => 'bar\rfoo')),
array('FOO=" FOO "', array('FOO' => ' FOO ')),
array('FOO=" "', array('FOO' => ' ')),
array('PATH="c:\\\\"', array('PATH' => 'c:\\')),
array("FOO=\"bar\nfoo\"", array('FOO' => "bar\nfoo")),
array('FOO=BAR\\"', array('FOO' => 'BAR"')),
array("FOO=BAR\\'BAZ", array('FOO' => "BAR'BAZ")),
array('FOO=\\"BAR', array('FOO' => '"BAR')),
["FOO=\"bar\"\n", ['FOO' => 'bar']],
["FOO=\"bar'foo\"\n", ['FOO' => 'bar\'foo']],
["FOO='bar'\n", ['FOO' => 'bar']],
["FOO='bar\"foo'\n", ['FOO' => 'bar"foo']],
["FOO=\"bar\\\"foo\"\n", ['FOO' => 'bar"foo']],
['FOO="bar\nfoo"', ['FOO' => "bar\nfoo"]],
['FOO="bar\rfoo"', ['FOO' => "bar\rfoo"]],
['FOO=\'bar\nfoo\'', ['FOO' => 'bar\nfoo']],
['FOO=\'bar\rfoo\'', ['FOO' => 'bar\rfoo']],
['FOO=" FOO "', ['FOO' => ' FOO ']],
['FOO=" "', ['FOO' => ' ']],
['PATH="c:\\\\"', ['PATH' => 'c:\\']],
["FOO=\"bar\nfoo\"", ['FOO' => "bar\nfoo"]],
['FOO=BAR\\"', ['FOO' => 'BAR"']],
["FOO=BAR\\'BAZ", ['FOO' => "BAR'BAZ"]],
['FOO=\\"BAR', ['FOO' => '"BAR']],

// concatenated values
array("FOO='bar''foo'\n", array('FOO' => 'barfoo')),
array("FOO='bar '' baz'", array('FOO' => 'bar baz')),
array("FOO=bar\nBAR='baz'\"\$FOO\"", array('FOO' => 'bar', 'BAR' => 'bazbar')),
array("FOO='bar '\\'' baz'", array('FOO' => "bar ' baz")),
["FOO='bar''foo'\n", ['FOO' => 'barfoo']],
["FOO='bar '' baz'", ['FOO' => 'bar baz']],
["FOO=bar\nBAR='baz'\"\$FOO\"", ['FOO' => 'bar', 'BAR' => 'bazbar']],
["FOO='bar '\\'' baz'", ['FOO' => "bar ' baz"]],

// comments
array("#FOO=bar\nBAR=foo", array('BAR' => 'foo')),
array("#FOO=bar # Comment\nBAR=foo", array('BAR' => 'foo')),
array("FOO='bar foo' # Comment", array('FOO' => 'bar foo')),
array("FOO='bar#foo' # Comment", array('FOO' => 'bar#foo')),
array("# Comment\r\nFOO=bar\n# Comment\nBAR=foo", array('FOO' => 'bar', 'BAR' => 'foo')),
array("FOO=bar # Another comment\nBAR=foo", array('FOO' => 'bar', 'BAR' => 'foo')),
array("FOO=\n\n# comment\nBAR=bar", array('FOO' => '', 'BAR' => 'bar')),
array('FOO=NOT#COMMENT', array('FOO' => 'NOT#COMMENT')),
array('FOO= # Comment', array('FOO' => '')),
["#FOO=bar\nBAR=foo", ['BAR' => 'foo']],
["#FOO=bar # Comment\nBAR=foo", ['BAR' => 'foo']],
["FOO='bar foo' # Comment", ['FOO' => 'bar foo']],
["FOO='bar#foo' # Comment", ['FOO' => 'bar#foo']],
["# Comment\r\nFOO=bar\n# Comment\nBAR=foo", ['FOO' => 'bar', 'BAR' => 'foo']],
["FOO=bar # Another comment\nBAR=foo", ['FOO' => 'bar', 'BAR' => 'foo']],
["FOO=\n\n# comment\nBAR=bar", ['FOO' => '', 'BAR' => 'bar']],
['FOO=NOT#COMMENT', ['FOO' => 'NOT#COMMENT']],
['FOO= # Comment', ['FOO' => '']],

// edge cases (no conversions, only strings as values)
array('FOO=0', array('FOO' => '0')),
array('FOO=false', array('FOO' => 'false')),
array('FOO=null', array('FOO' => 'null')),
['FOO=0', ['FOO' => '0']],
['FOO=false', ['FOO' => 'false']],
['FOO=null', ['FOO' => 'null']],

// export
array('export FOO=bar', array('FOO' => 'bar')),
array(' export FOO=bar', array('FOO' => 'bar')),
['export FOO=bar', ['FOO' => 'bar']],
[' export FOO=bar', ['FOO' => 'bar']],

// variable expansion
array("FOO=BAR\nBAR=\$FOO", array('FOO' => 'BAR', 'BAR' => 'BAR')),
array("FOO=BAR\nBAR=\"\$FOO\"", array('FOO' => 'BAR', 'BAR' => 'BAR')),
array("FOO=BAR\nBAR='\$FOO'", array('FOO' => 'BAR', 'BAR' => '$FOO')),
array("FOO_BAR9=BAR\nBAR=\$FOO_BAR9", array('FOO_BAR9' => 'BAR', 'BAR' => 'BAR')),
array("FOO=BAR\nBAR=\${FOO}Z", array('FOO' => 'BAR', 'BAR' => 'BARZ')),
array("FOO=BAR\nBAR=\$FOO}", array('FOO' => 'BAR', 'BAR' => 'BAR}')),
array("FOO=BAR\nBAR=\\\$FOO", array('FOO' => 'BAR', 'BAR' => '$FOO')),
array('FOO=" \\$ "', array('FOO' => ' $ ')),
array('FOO=" $ "', array('FOO' => ' $ ')),
array('BAR=$LOCAL', array('BAR' => 'local')),
array('BAR=$REMOTE', array('BAR' => 'remote')),
array('FOO=$NOTDEFINED', array('FOO' => '')),
);
["FOO=BAR\nBAR=\$FOO", ['FOO' => 'BAR', 'BAR' => 'BAR']],
["FOO=BAR\nBAR=\"\$FOO\"", ['FOO' => 'BAR', 'BAR' => 'BAR']],
["FOO=BAR\nBAR='\$FOO'", ['FOO' => 'BAR', 'BAR' => '$FOO']],
["FOO_BAR9=BAR\nBAR=\$FOO_BAR9", ['FOO_BAR9' => 'BAR', 'BAR' => 'BAR']],
["FOO=BAR\nBAR=\${FOO}Z", ['FOO' => 'BAR', 'BAR' => 'BARZ']],
["FOO=BAR\nBAR=\$FOO}", ['FOO' => 'BAR', 'BAR' => 'BAR}']],
["FOO=BAR\nBAR=\\\$FOO", ['FOO' => 'BAR', 'BAR' => '$FOO']],
['FOO=" \\$ "', ['FOO' => ' $ ']],
['FOO=" $ "', ['FOO' => ' $ ']],
['BAR=$LOCAL', ['BAR' => 'local']],
['BAR=$REMOTE', ['BAR' => 'remote']],
['FOO=$NOTDEFINED', ['FOO' => '']],
];

if ('\\' !== \DIRECTORY_SEPARATOR) {
$tests = array_merge($tests, array(
$tests = array_merge($tests, [
// command expansion
array('FOO=$(echo foo)', array('FOO' => 'foo')),
array('FOO=$((1+2))', array('FOO' => '3')),
array('FOO=FOO$((1+2))BAR', array('FOO' => 'FOO3BAR')),
array('FOO=$(echo "$(echo "$(echo "$(echo foo)")")")', array('FOO' => 'foo')),
array("FOO=$(echo \"Quotes won't be a problem\")", array('FOO' => 'Quotes won\'t be a problem')),
array("FOO=bar\nBAR=$(echo \"FOO is \$FOO\")", array('FOO' => 'bar', 'BAR' => 'FOO is bar')),
));
['FOO=$(echo foo)', ['FOO' => 'foo']],
['FOO=$((1+2))', ['FOO' => '3']],
['FOO=FOO$((1+2))BAR', ['FOO' => 'FOO3BAR']],
['FOO=$(echo "$(echo "$(echo "$(echo foo)")")")', ['FOO' => 'foo']],
["FOO=$(echo \"Quotes won't be a problem\")", ['FOO' => 'Quotes won\'t be a problem']],
["FOO=bar\nBAR=$(echo \"FOO is \$FOO\")", ['FOO' => 'bar', 'BAR' => 'FOO is bar']],
]);
}

return $tests;
Expand Down Expand Up @@ -310,7 +310,7 @@ public function testServerSuperglobalIsNotOverriden()
$originalValue = $_SERVER['argc'];

$dotenv = new Dotenv();
$dotenv->populate(array('argc' => 'new_value'));
$dotenv->populate(['argc' => 'new_value']);

$this->assertSame($originalValue, $_SERVER['argc']);
}
Expand All @@ -321,7 +321,7 @@ public function testEnvVarIsNotOverriden()
$_SERVER['TEST_ENV_VAR'] = 'original_value';

$dotenv = new Dotenv();
$dotenv->populate(array('TEST_ENV_VAR' => 'new_value'));
$dotenv->populate(['TEST_ENV_VAR' => 'new_value']);

$this->assertSame('original_value', getenv('TEST_ENV_VAR'));
}
Expand All @@ -331,7 +331,7 @@ public function testHttpVarIsPartiallyOverriden()
$_SERVER['HTTP_TEST_ENV_VAR'] = 'http_value';

$dotenv = new Dotenv();
$dotenv->populate(array('HTTP_TEST_ENV_VAR' => 'env_value'));
$dotenv->populate(['HTTP_TEST_ENV_VAR' => 'env_value']);

$this->assertSame('env_value', getenv('HTTP_TEST_ENV_VAR'));
$this->assertSame('env_value', $_ENV['HTTP_TEST_ENV_VAR']);
Expand Down Expand Up @@ -365,7 +365,7 @@ public function testMemorizingLoadedVarsNamesInSpecialVar()
putenv('DATABASE_URL');

$dotenv = new Dotenv();
$dotenv->populate(array('APP_DEBUG' => '1', 'DATABASE_URL' => 'mysql://root@localhost/db'));
$dotenv->populate(['APP_DEBUG' => '1', 'DATABASE_URL' => 'mysql://root@localhost/db']);

$this->assertSame('APP_DEBUG,DATABASE_URL', getenv('SYMFONY_DOTENV_VARS'));

Expand All @@ -382,8 +382,8 @@ public function testMemorizingLoadedVarsNamesInSpecialVar()
putenv('DATABASE_URL');

$dotenv = new Dotenv();
$dotenv->populate(array('APP_DEBUG' => '0', 'DATABASE_URL' => 'mysql://root@localhost/db'));
$dotenv->populate(array('DATABASE_URL' => 'sqlite:///somedb.sqlite'));
$dotenv->populate(['APP_DEBUG' => '0', 'DATABASE_URL' => 'mysql://root@localhost/db']);
$dotenv->populate(['DATABASE_URL' => 'sqlite:///somedb.sqlite']);

$this->assertSame('APP_ENV,DATABASE_URL', getenv('SYMFONY_DOTENV_VARS'));
}
Expand All @@ -398,7 +398,7 @@ public function testOverridingEnvVarsWithNamesMemorizedInSpecialVar()
putenv('DOCUMENT_ROOT=/var/www');

$dotenv = new Dotenv();
$dotenv->populate(array('FOO' => 'foo1', 'BAR' => 'bar1', 'BAZ' => 'baz1', 'DOCUMENT_ROOT' => '/boot'));
$dotenv->populate(['FOO' => 'foo1', 'BAR' => 'bar1', 'BAZ' => 'baz1', 'DOCUMENT_ROOT' => '/boot']);

$this->assertSame('foo1', getenv('FOO'));
$this->assertSame('bar1', getenv('BAR'));
Expand Down

0 comments on commit 4165a06

Please sign in to comment.