Skip to content

Commit 540425a

Browse files
bug #47635 [DependencyInjection] EnvPlaceholderParameterBag::get() can't return UnitEnum (jack.shpartko)
This PR was submitted for the 6.1 branch but it was merged into the 6.0 branch instead. Discussion ---------- [DependencyInjection] EnvPlaceholderParameterBag::get() can't return UnitEnum | Q | A | ------------- | --- | Branch? | 6.0 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - This PR allows to get enums from container in the next situation: ``` parameters: app.someEnum.yes !php/const \App\Context\SomeEnum::YES ``` Currently, we'll have an error: ``` [critical] Uncaught Error: Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag::get(): Return value must be of type array|string|int|float|bool|null, \App\Context\SomeEnum returned ``` This bugfix fixes this error. `EnvPlaceholderParameterBag::get()` has a signature: array|bool|string|int|float|null `ParameterBag::get()` has another signature: array|bool|string|int|float|**\UnitEnum**|null Why do we need this fix? To make the results of two dependent methods consistent. `EnvPlaceholderParameterBag::get()` call `ParameterBag::get()` here: https://github.com/symfony/symfony/blob/6.1/src/Symfony/Component/DependencyInjection/ParameterBag/EnvPlaceholderParameterBag.php#L61 This PR makes it consistent and adding UnitEnum to `EnvPlaceholderParameterBag::get()` result. Commits ------- bd865f7 Bugfix: add \UnitEnum as a result of get() method
2 parents 30a9506 + bd865f7 commit 540425a

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class EnvPlaceholderParameterBag extends ParameterBag
2929
/**
3030
* {@inheritdoc}
3131
*/
32-
public function get(string $name): array|bool|string|int|float|null
32+
public function get(string $name): array|bool|string|int|float|\UnitEnum|null
3333
{
3434
if (str_starts_with($name, 'env(') && str_ends_with($name, ')') && 'env()' !== $name) {
3535
$env = substr($name, 4, -1);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\DependencyInjection\Tests\Fixtures;
13+
14+
enum StringBackedEnum: string
15+
{
16+
case Bar = 'bar';
17+
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
1616
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
1717
use Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag;
18+
use Symfony\Component\DependencyInjection\Tests\Fixtures\StringBackedEnum;
1819

1920
class EnvPlaceholderParameterBagTest extends TestCase
2021
{
@@ -196,4 +197,12 @@ public function testExtraCharsInProcessor()
196197
$bag->resolve();
197198
$this->assertStringMatchesFormat('env_%s_key_a_b_c_FOO_%s', $bag->get('env(key:a.b-c:FOO)'));
198199
}
200+
201+
public function testGetEnum()
202+
{
203+
$bag = new EnvPlaceholderParameterBag();
204+
$bag->set('ENUM_VAR', StringBackedEnum::Bar);
205+
$this->assertInstanceOf(StringBackedEnum::class, $bag->get('ENUM_VAR'));
206+
$this->assertEquals(StringBackedEnum::Bar, $bag->get('ENUM_VAR'));
207+
}
199208
}

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