Skip to content

Commit 080b04e

Browse files
authored
Merge pull request #36 from imliam/safer-regex
Add safer regex that takes into account more possible whitespace
2 parents 11d2235 + b742eba commit 080b04e

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

src/EnvironmentSetCommand.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public function setEnvVariable(string $envFileContent, string $key, string $valu
108108
public function readKeyValuePair(string $envFileContent, string $key): ?string
109109
{
110110
// Match the given key at the beginning of a line
111-
if (preg_match("#^ *{$key} *= *[^\r\n]*$#uimU", $envFileContent, $matches)) {
111+
if (preg_match("#^\s*{$key}\s*=\s*[^\r\n]*$#uimU", $envFileContent, $matches)) {
112112
return $matches[0];
113113
}
114114

tests/Unit/EnvironmentSetCommandTest.php

+14-13
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,6 @@ public function setUp(): void
2525
/**
2626
* @covers EnvironmentSetCommand::setEnvVariable
2727
* @dataProvider setEnvVariableDataProvider
28-
*
29-
* @param string $originalEnvFileContent
30-
* @param string $key
31-
* @param string $value
32-
* @param string $expectedNewEnvFile
3328
*/
3429
public function testSetEnvVariable(
3530
string $originalEnvFileContent,
@@ -62,15 +57,24 @@ public function testSetEnvVariableTestOfNestedKeys(): void
6257
$this->assertEquals($expectedEnv, $newEnv);
6358
}
6459

60+
/**
61+
* @covers EnvironmentSetCommand::setEnvVariable
62+
*/
63+
public function testWhitespaceAsValueDoesntCreateNewEntry(): void
64+
{
65+
$env = 'APP_KEY = \t' . "\n";
66+
67+
$expectedEnv = 'APP_KEY=test' . "\n";
68+
69+
[$newEnv, $_] = $this->command->setEnvVariable($env, 'APP_KEY', 'test');
70+
$this->assertEquals($expectedEnv, $newEnv);
71+
}
72+
6573
/**
6674
* @covers EnvironmentSetCommand::readKeyValuePair
6775
* @dataProvider readKeyValuePairDataProvider
68-
*
69-
* @param string $envFileContent
70-
* @param string $key
71-
* @param string|null $expectedKeyValuePair
7276
*/
73-
public function testReadKeyValuePair(string $envFileContent, string $key, ?string $expectedKeyValuePair): void
77+
public function testReadKeyValuePair(string $envFileContent, string $key, ?string $expectedKeyValuePair = null): void
7478
{
7579
$realPair = $this->command->readKeyValuePair($envFileContent, $key);
7680
$this->assertEquals($expectedKeyValuePair, $realPair);
@@ -92,9 +96,6 @@ public function testParseCommandArguments(array $params, array $expectedResult):
9296
/**
9397
* @covers EnvironmentSetCommand::assertKeyIsValid
9498
* @dataProvider assertKeyIsValidDataProvider
95-
*
96-
* @param string $key
97-
* @param bool $isGood
9899
*/
99100
public function testAssertKeyIsValid(string $key, bool $isGood): void
100101
{

0 commit comments

Comments
 (0)