Skip to content

Commit dbcc609

Browse files
Add some extra type checks on names (vlucas#351)
1 parent e57f2fc commit dbcc609

File tree

4 files changed

+68
-1
lines changed

4 files changed

+68
-1
lines changed

src/Environment/AbstractVariables.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function isImmutable()
4747
*/
4848
public function has($name)
4949
{
50-
return $this->get($name) !== null;
50+
return is_string($name) && $this->get($name) !== null;
5151
}
5252

5353
/**

src/Environment/DotenvVariables.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Dotenv\Environment;
44

5+
use InvalidArgumentException;
6+
57
/**
68
* The default implementation of the environment variables interface.
79
*/
@@ -35,10 +37,16 @@ public function __construct(array $adapters, $immutable)
3537
*
3638
* @param string $name
3739
*
40+
* @throws \InvalidArgumentException
41+
*
3842
* @return string|null
3943
*/
4044
public function get($name)
4145
{
46+
if (!is_string($name)) {
47+
throw new InvalidArgumentException('Expected name to be a string.');
48+
}
49+
4250
foreach ($this->adapters as $adapter) {
4351
$result = $adapter->get($name);
4452
if ($result->isDefined()) {
@@ -53,10 +61,16 @@ public function get($name)
5361
* @param string $name
5462
* @param string|null $value
5563
*
64+
* @throws \InvalidArgumentException
65+
*
5666
* @return void
5767
*/
5868
public function set($name, $value = null)
5969
{
70+
if (!is_string($name)) {
71+
throw new InvalidArgumentException('Expected name to be a string.');
72+
}
73+
6074
// Don't overwrite existing environment variables if we're immutable
6175
// Ruby's dotenv does this with `ENV[key] ||= value`.
6276
if ($this->isImmutable() && $this->get($name) !== null) {
@@ -73,10 +87,16 @@ public function set($name, $value = null)
7387
*
7488
* @param string $name
7589
*
90+
* @throws \InvalidArgumentException
91+
*
7692
* @return void
7793
*/
7894
public function clear($name)
7995
{
96+
if (!is_string($name)) {
97+
throw new InvalidArgumentException('Expected name to be a string.');
98+
}
99+
80100
// Don't clear anything if we're immutable.
81101
if ($this->isImmutable()) {
82102
return;

src/Environment/VariablesInterface.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ public function has($name);
3030
*
3131
* @param string $name
3232
*
33+
* @throws \InvalidArgumentException
34+
*
3335
* @return string|null
3436
*/
3537
public function get($name);
@@ -40,6 +42,8 @@ public function get($name);
4042
* @param string $name
4143
* @param string|null $value
4244
*
45+
* @throws \InvalidArgumentException
46+
*
4347
* @return void
4448
*/
4549
public function set($name, $value = null);
@@ -49,6 +53,8 @@ public function set($name, $value = null);
4953
*
5054
* @param string $name
5155
*
56+
* @throws \InvalidArgumentException
57+
*
5258
* @return void
5359
*/
5460
public function clear($name);

tests/Dotenv/EnvironmentVariablesTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,32 @@ public function testCheckingWhetherVariableExists()
2525
$this->assertFalse($envVars->has('NON_EXISTING_VARIABLE'));
2626
}
2727

28+
public function testCheckingHasWithBadType()
29+
{
30+
$envVars = $this->envFactory->create();
31+
32+
$this->assertFalse($envVars->has(123));
33+
$this->assertFalse($envVars->has(null));
34+
}
35+
2836
public function testGettingVariableByName()
2937
{
3038
$envVars = $this->envFactory->create();
3139

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

43+
/**
44+
* @expectedException \InvalidArgumentException
45+
* @expectedExceptionMessage Expected name to be a string.
46+
*/
47+
public function testGettingBadVariable()
48+
{
49+
$envVars = $this->envFactory->create();
50+
51+
$envVars->get(null);
52+
}
53+
3554
public function testSettingVariable()
3655
{
3756
$envVars = $this->envFactory->create();
@@ -43,6 +62,17 @@ public function testSettingVariable()
4362
$this->assertSame('new', $envVars->get('FOO'));
4463
}
4564

65+
/**
66+
* @expectedException \InvalidArgumentException
67+
* @expectedExceptionMessage Expected name to be a string.
68+
*/
69+
public function testSettingBadVariable()
70+
{
71+
$envVars = $this->envFactory->create();
72+
73+
$envVars->set(null, 'foo');
74+
}
75+
4676
public function testClearingVariable()
4777
{
4878
$envVars = $this->envFactory->create();
@@ -52,6 +82,17 @@ public function testClearingVariable()
5282
$this->assertFalse($envVars->has('FOO'));
5383
}
5484

85+
/**
86+
* @expectedException \InvalidArgumentException
87+
* @expectedExceptionMessage Expected name to be a string.
88+
*/
89+
public function testClearingBadVariable()
90+
{
91+
$envVars = $this->envFactory->create();
92+
93+
$envVars->clear(null);
94+
}
95+
5596
public function testCannotSetVariableOnImmutableInstance()
5697
{
5798
$envVars = $this->envFactory->createImmutable();

0 commit comments

Comments
 (0)