Skip to content

[DI] Allow null as default env value #20590

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public function get($name)
if ($this->has($name)) {
$defaultValue = parent::get($name);

if (!is_scalar($defaultValue)) {
throw new RuntimeException(sprintf('The default value of an env() parameter must be scalar, but "%s" given to "%s".', gettype($defaultValue), $name));
if (null !== $defaultValue && !is_scalar($defaultValue)) {
throw new RuntimeException(sprintf('The default value of an env() parameter must be scalar or null, but "%s" given to "%s".', gettype($defaultValue), $name));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Meanwhile, can you please update the below message on line 100, to say "scalar or null" there also instead of "string or null"? (you should have a test to tweak also).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated 👍

}
}

Expand Down Expand Up @@ -96,8 +96,8 @@ public function resolve()
}
if (is_numeric($default = $this->parameters[$name])) {
$this->parameters[$name] = (string) $default;
} elseif (null !== $default && !is_string($default)) {
throw new RuntimeException(sprintf('The default value of env parameter "%s" must be string or null, %s given.', $env, gettype($default)));
} elseif (null !== $default && !is_scalar($default)) {
throw new RuntimeException(sprintf('The default value of env parameter "%s" must be scalar or null, %s given.', $env, gettype($default)));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public function testResolveEnvAllowsNull()

/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
* @expectedExceptionMessage The default value of env parameter "ARRAY_VAR" must be string or null, array given.
* @expectedExceptionMessage The default value of env parameter "ARRAY_VAR" must be scalar or null, array given.
*/
public function testResolveThrowsOnBadDefaultValue()
{
Expand All @@ -139,4 +139,26 @@ public function testResolveThrowsOnBadDefaultValue()
$bag->set('env(Array_Var)', array());
$bag->resolve();
}

public function testGetEnvAllowsNull()
{
$bag = new EnvPlaceholderParameterBag();
$bag->set('env(NULL_VAR)', null);
$bag->get('env(NULL_VAR)');
$bag->resolve();

$this->assertNull($bag->all()['env(null_var)']);
}

/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
* @expectedExceptionMessage The default value of an env() parameter must be scalar or null, but "array" given to "env(ARRAY_VAR)".
*/
public function testGetThrowsOnBadDefaultValue()
{
$bag = new EnvPlaceholderParameterBag();
$bag->set('env(ARRAY_VAR)', array());
$bag->get('env(ARRAY_VAR)');
$bag->resolve();
}
}