Skip to content

Commit 0f085a9

Browse files
committed
Leverage the match expression
1 parent e495e55 commit 0f085a9

12 files changed

+63
-66
lines changed

Dumper/PhpDumper.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1496,7 +1496,7 @@ private function addDefaultParametersMethod(): string
14961496
$export = explode('0 => ', substr(rtrim($export, " ]\n"), 2, -1), 2);
14971497

14981498
if ($hasEnum || preg_match("/\\\$this->(?:getEnv\('(?:[-.\w]*+:)*+\w++'\)|targetDir\.'')/", $export[1])) {
1499-
$dynamicPhp[$key] = sprintf('%scase %s: $value = %s; break;', $export[0], $this->export($key), $export[1]);
1499+
$dynamicPhp[$key] = sprintf('%s%s => %s,', $export[0], $this->export($key), $export[1]);
15001500
} else {
15011501
$php[] = sprintf('%s%s => %s,', $export[0], $this->export($key), $export[1]);
15021502
}
@@ -1559,10 +1559,10 @@ public function getParameterBag(): ParameterBagInterface
15591559
if ($dynamicPhp) {
15601560
$loadedDynamicParameters = $this->exportParameters(array_combine(array_keys($dynamicPhp), array_fill(0, \count($dynamicPhp), false)), '', 8);
15611561
$getDynamicParameter = <<<'EOF'
1562-
switch ($name) {
1562+
$value = match ($name) {
15631563
%s
1564-
default: throw new InvalidArgumentException(sprintf('The dynamic parameter "%%s" must be defined.', $name));
1565-
}
1564+
default => throw new InvalidArgumentException(sprintf('The dynamic parameter "%%s" must be defined.', $name)),
1565+
};
15661566
$this->loadedDynamicParameters[$name] = true;
15671567
15681568
return $this->dynamicParameters[$name] = $value;

Loader/IniFileLoader.php

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -83,21 +83,18 @@ private function phpize(string $value): mixed
8383
}
8484
$lowercaseValue = strtolower($value);
8585

86-
switch (true) {
87-
case \defined($value):
88-
return \constant($value);
89-
case 'yes' === $lowercaseValue || 'on' === $lowercaseValue:
90-
return true;
91-
case 'no' === $lowercaseValue || 'off' === $lowercaseValue || 'none' === $lowercaseValue:
92-
return false;
93-
case isset($value[1]) && (
86+
return match (true) {
87+
\defined($value) => \constant($value),
88+
'yes' === $lowercaseValue,
89+
'on' === $lowercaseValue => true,
90+
'no' === $lowercaseValue,
91+
'off' === $lowercaseValue,
92+
'none' === $lowercaseValue => false,
93+
isset($value[1]) && (
9494
("'" === $value[0] && "'" === $value[\strlen($value) - 1]) ||
9595
('"' === $value[0] && '"' === $value[\strlen($value) - 1])
96-
):
97-
// quoted string
98-
return substr($value, 1, -1);
99-
default:
100-
return XmlUtils::phpize($value);
101-
}
96+
) => substr($value, 1, -1), // quoted string
97+
default => XmlUtils::phpize($value),
98+
};
10299
}
103100
}

Tests/Dumper/PhpDumperTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,14 +1257,14 @@ public function testDumpHandlesEnumeration()
12571257
%A
12581258
private function getDynamicParameter(string $name)
12591259
{
1260-
switch ($name) {
1261-
case 'unit_enum': $value = \Symfony\Component\DependencyInjection\Tests\Fixtures\FooUnitEnum::BAR; break;
1262-
case 'enum_array': $value = [
1260+
$value = match ($name) {
1261+
'unit_enum' => \Symfony\Component\DependencyInjection\Tests\Fixtures\FooUnitEnum::BAR,
1262+
'enum_array' => [
12631263
0 => \Symfony\Component\DependencyInjection\Tests\Fixtures\FooUnitEnum::BAR,
12641264
1 => \Symfony\Component\DependencyInjection\Tests\Fixtures\FooUnitEnum::FOO,
1265-
]; break;
1266-
default: throw new InvalidArgumentException(sprintf('The dynamic parameter "%s" must be defined.', $name));
1267-
}
1265+
],
1266+
default => throw new InvalidArgumentException(sprintf('The dynamic parameter "%s" must be defined.', $name)),
1267+
};
12681268
%A
12691269
PHP
12701270
, $dumpedContainer

Tests/Fixtures/php/services19.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,10 @@ public function getParameterBag(): ParameterBagInterface
105105

106106
private function getDynamicParameter(string $name)
107107
{
108-
switch ($name) {
109-
case 'foo': $value = $this->getEnv('FOO'); break;
110-
default: throw new InvalidArgumentException(sprintf('The dynamic parameter "%s" must be defined.', $name));
111-
}
108+
$value = match ($name) {
109+
'foo' => $this->getEnv('FOO'),
110+
default => throw new InvalidArgumentException(sprintf('The dynamic parameter "%s" must be defined.', $name)),
111+
};
112112
$this->loadedDynamicParameters[$name] = true;
113113

114114
return $this->dynamicParameters[$name] = $value;

Tests/Fixtures/php/services26.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,13 @@ public function getParameterBag(): ParameterBagInterface
104104

105105
private function getDynamicParameter(string $name)
106106
{
107-
switch ($name) {
108-
case 'bar': $value = $this->getEnv('FOO'); break;
109-
case 'baz': $value = $this->getEnv('int:Baz'); break;
110-
case 'json': $value = $this->getEnv('json:file:json_file'); break;
111-
case 'db_dsn': $value = $this->getEnv('resolve:DB'); break;
112-
default: throw new InvalidArgumentException(sprintf('The dynamic parameter "%s" must be defined.', $name));
113-
}
107+
$value = match ($name) {
108+
'bar' => $this->getEnv('FOO'),
109+
'baz' => $this->getEnv('int:Baz'),
110+
'json' => $this->getEnv('json:file:json_file'),
111+
'db_dsn' => $this->getEnv('resolve:DB'),
112+
default => throw new InvalidArgumentException(sprintf('The dynamic parameter "%s" must be defined.', $name)),
113+
};
114114
$this->loadedDynamicParameters[$name] = true;
115115

116116
return $this->dynamicParameters[$name] = $value;

Tests/Fixtures/php/services_base64_env.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,10 @@ public function getParameterBag(): ParameterBagInterface
7777

7878
private function getDynamicParameter(string $name)
7979
{
80-
switch ($name) {
81-
case 'hello': $value = $this->getEnv('base64:foo'); break;
82-
default: throw new InvalidArgumentException(sprintf('The dynamic parameter "%s" must be defined.', $name));
83-
}
80+
$value = match ($name) {
81+
'hello' => $this->getEnv('base64:foo'),
82+
default => throw new InvalidArgumentException(sprintf('The dynamic parameter "%s" must be defined.', $name)),
83+
};
8484
$this->loadedDynamicParameters[$name] = true;
8585

8686
return $this->dynamicParameters[$name] = $value;

Tests/Fixtures/php/services_csv_env.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,10 @@ public function getParameterBag(): ParameterBagInterface
7777

7878
private function getDynamicParameter(string $name)
7979
{
80-
switch ($name) {
81-
case 'hello': $value = $this->getEnv('csv:foo'); break;
82-
default: throw new InvalidArgumentException(sprintf('The dynamic parameter "%s" must be defined.', $name));
83-
}
80+
$value = match ($name) {
81+
'hello' => $this->getEnv('csv:foo'),
82+
default => throw new InvalidArgumentException(sprintf('The dynamic parameter "%s" must be defined.', $name)),
83+
};
8484
$this->loadedDynamicParameters[$name] = true;
8585

8686
return $this->dynamicParameters[$name] = $value;

Tests/Fixtures/php/services_default_env.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,12 @@ public function getParameterBag(): ParameterBagInterface
7979

8080
private function getDynamicParameter(string $name)
8181
{
82-
switch ($name) {
83-
case 'fallback_env': $value = $this->getEnv('foobar'); break;
84-
case 'hello': $value = $this->getEnv('default:fallback_param:bar'); break;
85-
case 'hello-bar': $value = $this->getEnv('default:fallback_env:key:baz:json:foo'); break;
86-
default: throw new InvalidArgumentException(sprintf('The dynamic parameter "%s" must be defined.', $name));
87-
}
82+
$value = match ($name) {
83+
'fallback_env' => $this->getEnv('foobar'),
84+
'hello' => $this->getEnv('default:fallback_param:bar'),
85+
'hello-bar' => $this->getEnv('default:fallback_env:key:baz:json:foo'),
86+
default => throw new InvalidArgumentException(sprintf('The dynamic parameter "%s" must be defined.', $name)),
87+
};
8888
$this->loadedDynamicParameters[$name] = true;
8989

9090
return $this->dynamicParameters[$name] = $value;

Tests/Fixtures/php/services_json_env.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,11 @@ public function getParameterBag(): ParameterBagInterface
7878

7979
private function getDynamicParameter(string $name)
8080
{
81-
switch ($name) {
82-
case 'hello': $value = $this->getEnv('json:foo'); break;
83-
case 'hello-bar': $value = $this->getEnv('json:bar'); break;
84-
default: throw new InvalidArgumentException(sprintf('The dynamic parameter "%s" must be defined.', $name));
85-
}
81+
$value = match ($name) {
82+
'hello' => $this->getEnv('json:foo'),
83+
'hello-bar' => $this->getEnv('json:bar'),
84+
default => throw new InvalidArgumentException(sprintf('The dynamic parameter "%s" must be defined.', $name)),
85+
};
8686
$this->loadedDynamicParameters[$name] = true;
8787

8888
return $this->dynamicParameters[$name] = $value;

Tests/Fixtures/php/services_query_string_env.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,10 @@ public function getParameterBag(): ParameterBagInterface
7777

7878
private function getDynamicParameter(string $name)
7979
{
80-
switch ($name) {
81-
case 'hello': $value = $this->getEnv('query_string:foo'); break;
82-
default: throw new InvalidArgumentException(sprintf('The dynamic parameter "%s" must be defined.', $name));
83-
}
80+
$value = match ($name) {
81+
'hello' => $this->getEnv('query_string:foo'),
82+
default => throw new InvalidArgumentException(sprintf('The dynamic parameter "%s" must be defined.', $name)),
83+
};
8484
$this->loadedDynamicParameters[$name] = true;
8585

8686
return $this->dynamicParameters[$name] = $value;

0 commit comments

Comments
 (0)