10000 [Routing] Remove variadic constructor signature by wouterj · Pull Request #46157 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Routing] Remove variadic constructor signature #46157

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
Apr 27, 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
42 changes: 24 additions & 18 deletions src/Symfony/Component/Routing/Requirement/EnumRequirement.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,42 @@

final class EnumRequirement implements \Stringable
{
/**
* @var string[]
*/
private readonly array $values;
private string $requirement;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nicolas-grekas Why did you remove the readonly?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not intentional! (But read-only on private is not useful either so 🤷‍♂️ 😆)


/**
* @template T of \BackedEnum
* @param class-string<T> $enum
* @param T ...$cases
*
* @param class-string<T>|list<T> $cases
*/
public function __construct(string $enum, \BackedEnum ...$cases)
public function __construct(string|array $cases = [])
{
if (!\is_subclass_of($enum, \BackedEnum::class, true)) {
throw new InvalidArgumentException(sprintf('"%s" is not a \BackedEnum class.', $enum));
}
if (\is_string($cases)) {
if (!is_subclass_of($cases, \BackedEnum::class, true)) {
throw new InvalidArgumentException(sprintf('"%s" is not a "BackedEnum" class.', $cases));
}

$cases = $cases::cases();
} else {
$class = null;

foreach ($cases as $case) {
if (!$case instanceof \BackedEnum) {
throw new InvalidArgumentException(sprintf('Case must be a "BackedEnum" instance, "%s" given.', get_debug_type($case)));
}

$class ??= \get_class($case);

foreach ($cases as $case) {
if (!$case instanceof $enum) {
throw new InvalidArgumentException(sprintf('"%s::%s" is not a case of "%s".', \get_class($case), $case->name, $enum));
if (!$case instanceof $class) {
throw new InvalidArgumentException(sprintf('"%s::%s" is not a case of "%s".', get_debug_type($case), $case->name, $class));
}
}
}

$this->values = array_map(
static fn (\BackedEnum $e): string => $e->value,
$cases ?: $enum::cases(),
);
$this->requirement = implode('|', array_map(static fn ($e) => preg_quote($e->value), $cases));
}

public function __toString(): string
{
return implode('|', array_map(preg_quote(...), $this->values));
return $this->requirement;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,33 +25,42 @@ class EnumRequirementTest extends TestCase
public function testNotABackedEnum()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('"Symfony\Component\Routing\Tests\Fixtures\Enum\TestUnitEnum" is not a \BackedEnum class.');
$this->expectExceptionMessage('"Symfony\Component\Routing\Tests\Fixtures\Enum\TestUnitEnum" is not a "BackedEnum" class.');

new EnumRequirement(TestUnitEnum::class);
}

public function testCaseNotABackedEnum()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Case must be a "BackedEnum" instance, "string" given.');

new EnumRequirement(['wrong']);
}

public function testCaseFromAnotherEnum()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('"Symfony\Component\Routing\Tests\Fixtures\Enum\TestStringBackedEnum2::Spades" is not a case of "Symfony\Component\Routing\Tests\Fixtures\Enum\TestStringBackedEnum".');

new EnumRequirement(TestStringBackedEnum::class, TestStringBackedEnum::Diamonds, TestStringBackedEnum2::Spades);
new EnumRequirement([TestStringBackedEnum::Diamonds, TestStringBackedEnum2::Spades]);
}

/**
* @dataProvider provideToString
*/
public function testToString(string $expected, string $enum, \BackedEnum ...$cases)
public function testToString(string $expected, string|array $cases = [])
{
$this->assertSame($expected, (string) new EnumRequirement($enum, ...$cases));
$this->assertSame($expected, (string) new EnumRequirement($cases));
}

public function provideToString()
{
return [
['hearts|diamonds|clubs|spades', TestStringBackedEnum::class],
['10|20|30|40', TestIntBackedEnum::class],
['diamonds|spades', TestStringBackedEnum::class, TestStringBackedEnum::Diamonds, TestStringBackedEnum::Spades],
['diamonds|spades', [TestStringBackedEnum::Diamonds, TestStringBackedEnum::Spades]],
['diamonds', [TestStringBackedEnum::Diamonds]],
['hearts|diamonds|clubs|spa\|des', TestStringBackedEnum2::class],
];
}
Expand Down
0