Skip to content

[DependencyInjection] EnvPlaceholderParameterBag::get() can't return UnitEnum #47635

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Bugfix: add \UnitEnum as a result of get() method
  • Loading branch information
jack.shpartko authored and nicolas-grekas committed Sep 26, 2022
commit bd865f7f68701dba091ecada3ec2cea51139aa6e
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class EnvPlaceholderParameterBag extends ParameterBag
/**
* {@inheritdoc}
*/
public function get(string $name): array|bool|string|int|float|null
public function get(string $name): array|bool|string|int|float|\UnitEnum|null
{
if (str_starts_with($name, 'env(') && str_ends_with($name, ')') && 'env()' !== $name) {
$env = substr($name, 4, -1);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\DependencyInjection\Tests\Fixtures;

enum StringBackedEnum: string
{
case Bar = 'bar';
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag;
use Symfony\Component\DependencyInjection\Tests\Fixtures\StringBackedEnum;

class EnvPlaceholderParameterBagTest extends TestCase
{
Expand Down Expand Up @@ -196,4 +197,12 @@ public function testExtraCharsInProcessor()
$bag->resolve();
$this->assertStringMatchesFormat('env_%s_key_a_b_c_FOO_%s', $bag->get('env(key:a.b-c:FOO)'));
}

public function testGetEnum()
{
$bag = new EnvPlaceholderParameterBag();
$bag->set('ENUM_VAR', StringBackedEnum::Bar);
$this->assertInstanceOf(StringBackedEnum::class, $bag->get('ENUM_VAR'));
$this->assertEquals(StringBackedEnum::Bar, $bag->get('ENUM_VAR'));
}
}