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

[10.x] Fix enums uses with optional implicit parameters (#46483) #46515

Closed
Faks opened this issue Mar 19, 2023 · 4 comments
Closed

[10.x] Fix enums uses with optional implicit parameters (#46483) #46515

Faks opened this issue Mar 19, 2023 · 4 comments

Comments

@Faks
Copy link

Faks commented Mar 19, 2023

Hi, I checked code in Reflector, and seems there is a bug..., and left a fix in comment.

  • Laravel Version: 10.#.#
  • PHP Version: 8.2.0
  • Database Driver & Version:

Description:

[2023-03-19 14:16:15] local.ERROR: Call to undefined method ReflectionUnionType::getName() {"userId":7,"exception":"[object] (Error(code: 0): Call to undefined method ReflectionUnionType::getName() at /var/www/dev/api.noker.dev/vendor/laravel/framework/src/Illuminate/Support/Reflector.php:151) [stacktrace] #0 /var/www/dev/api.noker.dev/vendor/laravel/framework/src/Illuminate/Routing/RouteSignatureParameters.php(31): Illuminate\\Support\\Reflector::isParameterBackedEnumWithStringBackingType(Object(ReflectionParameter)) #1 [internal function]: Illuminate\\Routing\\RouteSignatureParameters::Illuminate\\Routing\\{closure}(Object(ReflectionParameter)) #2 /var/www/dev/api.noker.dev/vendor/laravel/framework/src/Illuminate/Routing/RouteSignatureParameters.php(31): array_filter(Array, Object(Closure)) #3 /var/www/dev/api.noker.dev/vendor/laravel/framework/src/Illuminate/Routing/Route.php(533): Illuminate\\Routing\\RouteSignatureParameters::fromAction(Array, Array) #4 /var/www/dev/api.noker.dev/vendor/laravel/framework/src/Illuminate/Routing/ImplicitRouteBinding.php(80): Illuminate\\Routing\\Route->signatureParameters(Array) #5 /var/www/dev/api.noker.dev/vendor/laravel/framework/src/Illuminate/Routing/ImplicitRouteBinding.php(28): Illuminate\\Routing\\ImplicitRouteBinding::resolveBackedEnumsForRoute(Object(Illuminate\\Routing\\Route), Array) #6 /var/www/dev/api.noker.dev/vendor/laravel/framework/src/Illuminate/Routing/Router.php(947): Illuminate\\Routing\\ImplicitRouteBinding::resolveForRoute(Object(Illuminate\\Foundation\\Application), Object(Illuminate\\Routing\\Route)) #7 /var/www/dev/api.noker.dev/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php(41): Illuminate\\Routing\\Router->substituteImplicitBindings(Object(Illuminate\\Routing\\Route)) #8 /var/www/dev/api.noker.dev/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(180): Illuminate\\Routing\\Middleware\\SubstituteBindings->handle(Object(Illuminate\\Http\\Request), Object(Closure)) #9 /var/www/dev/api.noker.dev/vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php(126): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request)) #10 /var/www/dev/api.noker.dev/vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php(92): Illuminate\\Routing\\Middleware\\ThrottleRequests->handleRequest(Object(Illuminate\\Http\\Request), Object(Closure), Array) #11 /var/www/dev/api.noker.dev/vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php(54): Illuminate\\Routing\\Middleware\\ThrottleRequests->handleRequestUsingNamedLimiter(Object(Illuminate\\Http\\Request), Object(Closure), 'api', Object(Closure)) #12 /var/www/dev/api.noker.dev/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(180): Illuminate\\Routing\\Middleware\\ThrottleRequests->handle(Object(Illuminate\\Http\\Request), Object(Closure), 'api') #13 /var/www/dev/api.noker.dev/vendor/laravel/framework/src/Illuminate/Auth/Middleware/Authenticate.php(45): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request)) #14 /var/www/dev/api.noker.dev/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(180): Illuminate\\Auth\\Middleware\\Authenticate->handle(Object(Illuminate\\Http\\Request), Object(Closure), 'sanctum') #15 /var/www/dev/api.noker.dev/vendor/laravel/sanctum/src/Http/Middleware/EnsureFrontendRequestsAreStateful.php(33): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request)) #16 /var/www/dev/api.noker.dev/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(141): Laravel\\Sanctum\\Http\\Middleware\\EnsureFrontendRequestsAreStateful->Laravel\\Sanctum\\Http\\Middleware\\{closure}(Object(Illuminate\\Http\\Request)) #17 /var/www/dev/api.noker.dev/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(116): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request)) #18 /var/www/dev/api.noker.dev/vendor/laravel/sanctum/src/Http/Middleware/EnsureFrontendRequestsAreStateful.php(32): Illuminate\\Pipeline\\Pipeline->then(Object(Closure)) #19 /var/www/dev/api.noker.dev/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(180): Laravel\\Sanctum\\Http\\Middleware\\EnsureFrontendRequestsAreStateful->handle(Object(Illuminate\\Http\\Request), Object(Closure)) #20 /var/www/dev/api.noker.dev/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(116): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request)) #21 /var/www/dev/api.noker.dev/vendor/laravel/framework/src/Illuminate/Routing/Router.php(797): Illuminate\\Pipeline\\Pipeline->then(Object(Closure)) #22 /var/www/dev/api.noker.dev/vendor/laravel/framework/src/Illuminate/Routing/Router.php(776): Illuminate\\Routing\\Router->runRouteWithinStack(Object(Illuminate\\Routing\\Route), Object(Illuminate\\Http\\Request)) #23 /var/www/dev/api.noker.dev/vendor/laravel/framework/src/Illuminate/Routing/Router.php(740): Illuminate\\Routing\\Router->runRoute(Object(Illuminate\\Http\\Request), Object(Illuminate\\Routing\\Route)) #24 /var/www/dev/api.noker.dev/vendor/laravel/framework/src/Illuminate/Routing/Router.php(729): Illuminate\\Routing\\Router->dispatchToRoute(Object(Illuminate\\Http\\Request)) #25 /var/www/dev/api.noker.dev/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(200): Illuminate\\Routing\\Router->dispatch(Object(Illuminate\\Http\\Request)) #26 /var/www/dev/api.noker.dev/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(141): Illuminate\\Foundation\\Http\\Kernel->Illuminate\\Foundation\\Http\\{closure}(Object(Illuminate\\Http\\Request)) #27 /var/www/dev/api.noker.dev/vendor/barryvdh/laravel-debugbar/src/Middleware/InjectDebugbar.php(66): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request)) #28 /var/www/dev/api.noker.dev/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(180): Barryvdh\\Debugbar\\Middleware\\InjectDebugbar->handle(Object(Illuminate\\Http\\Request), Object(Closure)) #29 /var/www/dev/api.noker.dev/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request)) #30 /var/www/dev/api.noker.dev/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ConvertEmptyStringsToNull.php(31): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle(Object(Illuminate\\Http\\Request), Object(Closure)) #31 /var/www/dev/api.noker.dev/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(180): Illuminate\\Foundation\\Http\\Middleware\\ConvertEmptyStringsToNull->handle(Object(Illuminate\\Http\\Request), Object(Closure)) #32 /var/www/dev/api.noker.dev/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request)) #33 /var/www/dev/api.noker.dev/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TrimStrings.php(40): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle(Object(Illuminate\\Http\\Request), Object(Closure)) #34 /var/www/dev/api.noker.dev/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(180): Illuminate\\Foundation\\Http\\Middleware\\TrimStrings->handle(Object(Illuminate\\Http\\Request), Object(Closure)) #35 /var/www/dev/api.noker.dev/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php(27): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request)) #36 /var/www/dev/api.noker.dev/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(180): Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize->handle(Object(Illuminate\\Http\\Request), Object(Closure)) #37 /var/www/dev/api.noker.dev/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php(86): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request)) #38 /var/www/dev/api.noker.dev/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(180): Illuminate\\Foundation\\Http\\Middleware\\PreventRequestsDuringMaintenance->handle(Object(Illuminate\\Http\\Request), Object(Closure)) #39 /var/www/dev/api.noker.dev/vendor/laravel/framework/src/Illuminate/Http/Middleware/TrustProxies.php(39): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request)) #40 /var/www/dev/api.noker.dev/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(180): Illuminate\\Http\\Middleware\\TrustProxies->handle(Object(Illuminate\\Http\\Request), Object(Closure)) #41 /var/www/dev/api.noker.dev/vendor/laravel/framework/src/Illuminate/Http/Middleware/TrustHosts.php(48): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request)) #42 /var/www/dev/api.noker.dev/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(180): Illuminate\\Http\\Middleware\\TrustHosts->handle(Object(Illuminate\\Http\\Request), Object(Closure)) #43 /var/www/dev/api.noker.dev/vendor/laravel/framework/src/Illuminate/Http/Middleware/CheckResponseForModifications.php(19): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request)) #44 /var/www/dev/api.noker.dev/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(180): Illuminate\\Http\\Middleware\\CheckResponseForModifications->handle(Object(Illuminate\\Http\\Request), Object(Closure)) #45 /var/www/dev/api.noker.dev/vendor/laravel/framework/src/Illuminate/Http/Middleware/SetCacheHeaders.php(23): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request)) #46 /var/www/dev/api.noker.dev/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(180): Illuminate\\Http\\Middleware\\SetCacheHeaders->handle(Object(Illuminate\\Http\\Request), Object(Closure)) #47 /var/www/dev/api.noker.dev/vendor/laravel/framework/src/Illuminate/Http/Middleware/HandleCors.php(62): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request)) #48 /var/www/dev/api.noker.dev/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(180): Illuminate\\Http\\Middleware\\HandleCors->handle(Object(Illuminate\\Http\\Request), Object(Closure)) #49 /var/www/dev/api.noker.dev/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(116): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request)) #50 /var/www/dev/api.noker.dev/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(175): Illuminate\\Pipeline\\Pipeline->then(Object(Closure)) #51 /var/www/dev/api.noker.dev/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(144): Illuminate\\Foundation\\Http\\Kernel->sendRequestThroughRouter(Object(Illuminate\\Http\\Request)) #52 /var/www/dev/api.noker.dev/public/index.php(53): Illuminate\\Foundation\\Http\\Kernel->handle(Object(Illuminate\\Http\\Request)) #53 {main} "}

Steps To Reproduce:

Install lastest laravel, register route (api) without a name and bug will be present.

@Faks
Copy link
Author

Faks commented Mar 19, 2023

The fix is since reflector doesn't have a method in chain.
/**
* Determine if the parameter's type is a Backed Enum with a string backing type.
*
* @param \ReflectionParameter $parameter
* @return bool
*/
public static function isParameterBackedEnumWithStringBackingType($parameter)
{
$backedEnumClass = $parameter?->getType();

    if (is_null($backedEnumClass)) {
        return false;
    }

    if (enum_exists($backedEnumClass)) {
        $reflectionBackedEnum = new ReflectionEnum($backedEnumClass);

        return $reflectionBackedEnum->isBacked()
            && $reflectionBackedEnum->getBackingType()->getName() == 'string';
    }

    return false;
}

@Faks
Copy link
Author

Faks commented Mar 19, 2023

#46516

@driesvints
Copy link
Member

Right now, we don't support union types here. But let's see how your PR goes.

@driesvints
Copy link
Member

It seems I was incorrect and we do support union types here. A better PR was sent in here: #46511

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants