Skip to content

Commit d2bea6a

Browse files
committed
[DependencyInjection] Throw on attempting to set enum parameters
1 parent 8ba3fa7 commit d2bea6a

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

src/Symfony/Component/DependencyInjection/ParameterBag/ParameterBag.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,18 @@ public function get($name)
103103
*/
104104
public function set($name, $value)
105105
{
106+
if ($value instanceof \UnitEnum) {
107+
throw new \TypeError(sprintf('Enums are not supported as dependency-injection parameters. Got "%s::%s" in parameter "%s".', \get_class($value), $value->name, $name));
108+
}
109+
110+
if (\is_array($value)) {
111+
array_walk_recursive($value, static function ($value) use ($name) {
112+
if ($value instanceof \UnitEnum) {
113+
throw new \TypeError(sprintf('Enums are not supported as dependency-injection parameters. Got "%s::%s" in parameter "%s".', \get_class($value), $value->name, $name));
114+
}
115+
});
116+
}
117+
106118
$this->parameters[(string) $name] = $value;
107119
}
108120

src/Symfony/Component/DependencyInjection/Tests/ParameterBag/ParameterBagTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;
1717
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
1818
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
19+
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooUnitEnum;
1920

2021
class ParameterBagTest extends TestCase
2122
{
@@ -253,4 +254,30 @@ public function stringsWithSpacesProvider()
253254
['50% is less than 100%', '50% is less than 100%', 'Text between % signs is allowed, if there are spaces.'],
254255
];
255256
}
257+
258+
/**
259+
* @requires PHP 8.1
260+
*
261+
* @dataProvider provideEnumParameters
262+
*/
263+
public function testSetEnumValueThrows($value)
264+
{
265+
$this->expectException(\TypeError::class);
266+
$this->expectExceptionMessage('Enums are not supported as dependency-injection parameters. Got "Symfony\Component\DependencyInjection\Tests\Fixtures\FooUnitEnum::BAR" in parameter "param"');
267+
268+
$bag = new ParameterBag();
269+
270+
$bag->set('param', $value);
271+
}
272+
273+
public function provideEnumParameters()
274+
{
275+
if (\PHP_VERSION_ID < 80100) {
276+
return;
277+
}
278+
279+
yield [FooUnitEnum::BAR];
280+
yield [[FooUnitEnum::BAR]];
281+
yield [['foo' => ['bar' => FooUnitEnum::BAR]]];
282+
}
256283
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy