Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 3b996e9

Browse filesBrowse files
committed
[DependencyInjection] Fix autocasting null env values to empty string
1 parent 7a8457d commit 3b996e9
Copy full SHA for 3b996e9

File tree

3 files changed

+24
-4
lines changed
Filter options

3 files changed

+24
-4
lines changed

‎src/Symfony/Component/DependencyInjection/Container.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Container.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ protected function getEnv(string $name)
387387
$prefix = substr($name, 0, $i);
388388
$localName = substr($name, 1 + $i);
389389
} else {
390-
$prefix = 'string';
390+
$prefix = '';
391391
$localName = $name;
392392
}
393393
$processor = $processors->has($prefix) ? $processors->get($prefix) : new EnvVarProcessor($this);

‎src/Symfony/Component/DependencyInjection/EnvVarProcessor.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/EnvVarProcessor.php
+6-2Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public function getEnv(string $prefix, string $name, \Closure $getEnv)
126126
}
127127
}
128128

129-
if (false !== $i || 'string' !== $prefix) {
129+
if (false !== $i || !\in_array($prefix, ['', 'string'], true)) {
130130
$env = $getEnv($name);
131131
} elseif (isset($_ENV[$name])) {
132132
$env = $_ENV[$name];
@@ -177,6 +177,10 @@ public function getEnv(string $prefix, string $name, \Closure $getEnv)
177177
}
178178

179179
if (null === $env) {
180+
if ('' === $prefix) {
181+
return null;
182+
}
183+
180184
if (!isset($this->getProvidedTypes()[$prefix])) {
181185
throw new RuntimeException(sprintf('Unsupported env var prefix "%s".', $prefix));
182186
}
@@ -190,7 +194,7 @@ public function getEnv(string $prefix, string $name, \Closure $getEnv)
190194
throw new RuntimeException(sprintf('Non-scalar env var "%s" cannot be cast to "%s".', $name, $prefix));
191195
}
192196

193-
if ('string' === $prefix) {
197+
if (\in_array($prefix, ['', 'string'], true)) {
194198
return (string) $env;
195199
}
196200

‎src/Symfony/Component/DependencyInjection/Tests/EnvVarProcessorTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/EnvVarProcessorTest.php
+17-1Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -763,12 +763,13 @@ public static function provideGetEnvUrlPath()
763763

764764
/**
765765
* @testWith ["", "string"]
766+
* [null, ""]
766767
* [false, "bool"]
767768
* [true, "not"]
768769
* [0, "int"]
769770
* [0.0, "float"]
770771
*/
771-
public function testGetEnvCastsNull($expected, string $prefix)
772+
public function testGetEnvCastsNullBehavior($expected, string $prefix)
772773
{
773774
$processor = new EnvVarProcessor(new Container());
774775

@@ -778,4 +779,19 @@ public function testGetEnvCastsNull($expected, string $prefix)
778779
});
779780
}));
780781
}
782+
783+
public function testGetEnvWithEmptyStringPrefixCastsToString()
784+
{
785+
$processor = new EnvVarProcessor(new Container());
786+
unset($_ENV['FOO']);
787+
$_ENV['FOO'] = 4;
788+
789+
try {
790+
$this->assertSame('4', $processor->getEnv('', 'FOO', static function () use ($processor) {
791+
$this->fail('Should not be called');
792+
}));
793+
} finally {
794+
unset($_ENV['FOO']);
795+
}
796+
}
781797
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.