Skip to content
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

Revert "[11.x] Adds support for int backed enums to implicit Enum route binding" #51119

Merged
merged 1 commit into from
Apr 18, 2024
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
9 changes: 1 addition & 8 deletions src/Illuminate/Routing/ImplicitRouteBinding.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Illuminate\Routing\Exceptions\BackedEnumCaseNotFoundException;
use Illuminate\Support\Reflector;
use Illuminate\Support\Str;
use ReflectionEnum;

class ImplicitRouteBinding
{
Expand Down Expand Up @@ -75,7 +74,6 @@ public static function resolveForRoute($container, $route)
* @return \Illuminate\Routing\Route
*
* @throws \Illuminate\Routing\Exceptions\BackedEnumCaseNotFoundException
* @throws \ReflectionException
*/
protected static function resolveBackedEnumsForRoute($route, $parameters)
{
Expand All @@ -88,12 +86,7 @@ protected static function resolveBackedEnumsForRoute($route, $parameters)

$backedEnumClass = $parameter->getType()?->getName();

$reflectionEnum = new ReflectionEnum($backedEnumClass);

match ($reflectionEnum->getBackingType()->getName()) {
'int' => $backedEnum = collect($backedEnumClass::cases())->first(fn ($case) => (string) $case->value === (string) $parameterValue),
'string' => $backedEnum = $backedEnumClass::tryFrom((string) $parameterValue),
};
$backedEnum = $backedEnumClass::tryFrom((string) $parameterValue);

if (is_null($backedEnum)) {
throw new BackedEnumCaseNotFoundException($backedEnumClass, $parameterValue);
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Routing/RouteSignatureParameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static function fromAction(array $action, $conditions = [])

return match (true) {
! empty($conditions['subClass']) => array_filter($parameters, fn ($p) => Reflector::isParameterSubclassOf($p, $conditions['subClass'])),
! empty($conditions['backedEnum']) => array_filter($parameters, fn ($p) => Reflector::isParameterBackedEnumWithValidBackingType($p)),
! empty($conditions['backedEnum']) => array_filter($parameters, fn ($p) => Reflector::isParameterBackedEnumWithStringBackingType($p)),
default => $parameters,
};
}
Expand Down
6 changes: 3 additions & 3 deletions src/Illuminate/Support/Reflector.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,12 @@ public static function isParameterSubclassOf($parameter, $className)
}

/**
* Determine if the parameter's type is a Backed Enum with a string or int backing type.
* Determine if the parameter's type is a Backed Enum with a string backing type.
*
* @param \ReflectionParameter $parameter
* @return bool
*/
public static function isParameterBackedEnumWithValidBackingType($parameter)
public static function isParameterBackedEnumWithStringBackingType($parameter)
{
if (! $parameter->getType() instanceof ReflectionNamedType) {
return false;
Expand All @@ -162,7 +162,7 @@ public static function isParameterBackedEnumWithValidBackingType($parameter)
$reflectionBackedEnum = new ReflectionEnum($backedEnumClass);

return $reflectionBackedEnum->isBacked()
&& in_array($reflectionBackedEnum->getBackingType()->getName(), ['int', 'string']);
&& $reflectionBackedEnum->getBackingType()->getName() == 'string';
}

return false;
Expand Down
6 changes: 0 additions & 6 deletions tests/Routing/Enums.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,3 @@ enum CategoryBackedEnum: string
case People = 'people';
case Fruits = 'fruits';
}

enum CategoryIntBackedEnum: int
{
case People = 1;
case Fruits = 2;
}
41 changes: 0 additions & 41 deletions tests/Routing/ImplicitRouteBindingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,6 @@ public function test_it_can_resolve_the_implicit_backed_enum_route_bindings_for_
$this->assertSame('fruits', $route->parameter('category')->value);
}

public function test_it_can_resolve_the_implicit_int_backed_enum_route_bindings_for_the_given_route()
{
$action = ['uses' => function (CategoryIntBackedEnum $category) {
return $category->value;
}];

$route = new Route('GET', '/test', $action);
$route->parameters = ['category' => '1'];

$route->prepareForSerialization();

$container = Container::getInstance();

ImplicitRouteBinding::resolveForRoute($container, $route);

$this->assertSame(1, $route->parameter('category')->value);
}

public function test_it_can_resolve_the_implicit_backed_enum_route_bindings_for_the_given_route_with_optional_parameter()
{
$action = ['uses' => function (?CategoryBackedEnum $category = null) {
Expand Down Expand Up @@ -109,29 +91,6 @@ public function test_implicit_backed_enum_internal_exception()
ImplicitRouteBinding::resolveForRoute($container, $route);
}

public function test_implicit_int_backed_enum_internal_exception()
{
$action = ['uses' => function (CategoryIntBackedEnum $category) {
return $category->value;
}];

$route = new Route('GET', '/test', $action);
$route->parameters = ['category' => ' 00001.'];

$route->prepareForSerialization();

$container = Container::getInstance();

$this->expectException(BackedEnumCaseNotFoundException::class);
$this->expectExceptionMessage(sprintf(
'Case [%s] not found on Backed Enum [%s].',
' 00001.',
CategoryIntBackedEnum::class,
));

ImplicitRouteBinding::resolveForRoute($container, $route);
}

public function test_it_can_resolve_the_implicit_model_route_bindings_for_the_given_route()
{
$this->expectNotToPerformAssertions();
Expand Down