Skip to content

Commit 7375a50

Browse files
committed
Refactor handling environment variables to avoid duplicate logic
1 parent 1875a97 commit 7375a50

2 files changed

Lines changed: 16 additions & 10 deletions

File tree

src/Container.php

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,12 @@ public function getEnv(string $name): ?string
9898
{
9999
assert(\preg_match('/^[A-Z][A-Z0-9_]+$/', $name) === 1);
100100

101-
if (\is_array($this->container) && \array_key_exists($name, $this->container)) {
102-
$value = $this->loadVariable($name, 'mixed', true, 64);
103-
} elseif ($this->container instanceof ContainerInterface && $this->container->has($name)) {
101+
if ($this->container instanceof ContainerInterface && $this->container->has($name)) {
104102
$value = $this->container->get($name);
103+
} elseif ($this->hasVariable($name)) {
104+
$value = $this->loadVariable($name, 'mixed', true, 64);
105105
} else {
106-
$value = $_SERVER[$name] ?? null;
106+
return null;
107107
}
108108

109109
if (!\is_string($value) && $value !== null) {
@@ -257,7 +257,7 @@ private function loadParameter(\ReflectionParameter $parameter, int $depth, bool
257257

258258
// load container variables if parameter name is known
259259
assert($type === null || $type instanceof \ReflectionNamedType);
260-
if ($allowVariables && (\array_key_exists($parameter->getName(), $this->container) || (isset($_SERVER[$parameter->getName()]) && \preg_match('/^[A-Z][A-Z0-9_]+$/', $parameter->getName())))) {
260+
if ($allowVariables && $this->hasVariable($parameter->getName())) {
261261
return $this->loadVariable($parameter->getName(), $type === null ? 'mixed' : $type->getName(), $parameter->allowsNull(), $depth);
262262
}
263263

@@ -294,15 +294,21 @@ private function loadParameter(\ReflectionParameter $parameter, int $depth, bool
294294
return $this->loadObject($type->getName(), $depth - 1);
295295
}
296296

297+
private function hasVariable(string $name): bool
298+
{
299+
return (\is_array($this->container) && \array_key_exists($name, $this->container)) || (\is_string($_SERVER[$name] ?? null) && \preg_match('/^[A-Z][A-Z0-9_]+$/', $name));
300+
}
301+
297302
/**
298303
* @return object|string|int|float|bool|null
299304
* @throws \BadMethodCallException if $name is not a valid container variable
300305
*/
301306
private function loadVariable(string $name, string $type, bool $nullable, int $depth) /*: object|string|int|float|bool|null (PHP 8.0+) */
302307
{
303-
assert(\is_array($this->container) && (\array_key_exists($name, $this->container) || isset($_SERVER[$name])));
308+
assert($this->hasVariable($name));
309+
assert(\is_array($this->container) || !$this->container->has($name));
304310

305-
if (($this->container[$name] ?? null) instanceof \Closure) {
311+
if (\is_array($this->container) && ($this->container[$name] ?? null) instanceof \Closure) {
306312
if ($depth < 1) {
307313
throw new \BadMethodCallException('Container variable $' . $name . ' is recursive');
308314
}
@@ -321,10 +327,10 @@ private function loadVariable(string $name, string $type, bool $nullable, int $d
321327
}
322328

323329
$this->container[$name] = $value;
324-
} elseif (\array_key_exists($name, $this->container)) {
330+
} elseif (\is_array($this->container) && \array_key_exists($name, $this->container)) {
325331
$value = $this->container[$name];
326332
} else {
327-
assert(isset($_SERVER[$name]) && \is_string($_SERVER[$name]));
333+
assert(\is_string($_SERVER[$name] ?? null));
328334
$value = $_SERVER[$name];
329335
}
330336

tests/ContainerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2077,7 +2077,7 @@ public function testGetEnvReturnsNullIfPsrContainerHasNoEntry(): void
20772077
public function testGetEnvReturnsStringFromGlobalServerIfPsrContainerHasNoEntry(): void
20782078
{
20792079
$psr = $this->createMock(ContainerInterface::class);
2080-
$psr->expects($this->once())->method('has')->with('X_FOO')->willReturn(false);
2080+
$psr->expects($this->atLeastOnce())->method('has')->with('X_FOO')->willReturn(false);
20812081
$psr->expects($this->never())->method('get');
20822082

20832083
assert($psr instanceof ContainerInterface);

0 commit comments

Comments
 (0)