Skip to content

Commit

Permalink
Add some extra type checks on names (#351)
Browse files Browse the repository at this point in the history
  • Loading branch information
GrahamCampbell committed Mar 6, 2019
1 parent e57f2fc commit dbcc609
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Environment/AbstractVariables.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function isImmutable()
*/
public function has($name)
{
return $this->get($name) !== null;
return is_string($name) && $this->get($name) !== null;
}

/**
Expand Down
20 changes: 20 additions & 0 deletions src/Environment/DotenvVariables.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Dotenv\Environment;

use InvalidArgumentException;

/**
* The default implementation of the environment variables interface.
*/
Expand Down Expand Up @@ -35,10 +37,16 @@ public function __construct(array $adapters, $immutable)
*
* @param string $name
*
* @throws \InvalidArgumentException
*
* @return string|null
*/
public function get($name)
{
if (!is_string($name)) {
throw new InvalidArgumentException('Expected name to be a string.');
}

foreach ($this->adapters as $adapter) {
$result = $adapter->get($name);
if ($result->isDefined()) {
Expand All @@ -53,10 +61,16 @@ public function get($name)
* @param string $name
* @param string|null $value
*
* @throws \InvalidArgumentException
*
* @return void
*/
public function set($name, $value = null)
{
if (!is_string($name)) {
throw new InvalidArgumentException('Expected name to be a string.');
}

// Don't overwrite existing environment variables if we're immutable
// Ruby's dotenv does this with `ENV[key] ||= value`.
if ($this->isImmutable() && $this->get($name) !== null) {
Expand All @@ -73,10 +87,16 @@ public function set($name, $value = null)
*
* @param string $name
*
* @throws \InvalidArgumentException
*
* @return void
*/
public function clear($name)
{
if (!is_string($name)) {
throw new InvalidArgumentException('Expected name to be a string.');
}

// Don't clear anything if we're immutable.
if ($this->isImmutable()) {
return;
Expand Down
6 changes: 6 additions & 0 deletions src/Environment/VariablesInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public function has($name);
*
* @param string $name
*
* @throws \InvalidArgumentException
*
* @return string|null
*/
public function get($name);
Expand All @@ -40,6 +42,8 @@ public function get($name);
* @param string $name
* @param string|null $value
*
* @throws \InvalidArgumentException
*
* @return void
*/
public function set($name, $value = null);
Expand All @@ -49,6 +53,8 @@ public function set($name, $value = null);
*
* @param string $name
*
* @throws \InvalidArgumentException
*
* @return void
*/
public function clear($name);
Expand Down
41 changes: 41 additions & 0 deletions tests/Dotenv/EnvironmentVariablesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,32 @@ public function testCheckingWhetherVariableExists()
$this->assertFalse($envVars->has('NON_EXISTING_VARIABLE'));
}

public function testCheckingHasWithBadType()
{
$envVars = $this->envFactory->create();

$this->assertFalse($envVars->has(123));
$this->assertFalse($envVars->has(null));
}

public function testGettingVariableByName()
{
$envVars = $this->envFactory->create();

$this->assertSame('bar', $envVars->get('FOO'));
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Expected name to be a string.
*/
public function testGettingBadVariable()
{
$envVars = $this->envFactory->create();

$envVars->get(null);
}

public function testSettingVariable()
{
$envVars = $this->envFactory->create();
Expand All @@ -43,6 +62,17 @@ public function testSettingVariable()
$this->assertSame('new', $envVars->get('FOO'));
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Expected name to be a string.
*/
public function testSettingBadVariable()
{
$envVars = $this->envFactory->create();

$envVars->set(null, 'foo');
}

public function testClearingVariable()
{
$envVars = $this->envFactory->create();
Expand All @@ -52,6 +82,17 @@ public function testClearingVariable()
$this->assertFalse($envVars->has('FOO'));
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Expected name to be a string.
*/
public function testClearingBadVariable()
{
$envVars = $this->envFactory->create();

$envVars->clear(null);
}

public function testCannotSetVariableOnImmutableInstance()
{
$envVars = $this->envFactory->createImmutable();
Expand Down

0 comments on commit dbcc609

Please sign in to comment.