Skip to content

[13.x] Fix Request::route() return type when no route is resolved - #62191

Merged
taylorotwell merged 1 commit into
laravel:13.xfrom
yoannblot:fix-request-route-return-type
Oct 26, 2026
Merged

taylorotwell merged 1 commit into
laravel:13.xfrom
yoannblot:fix-request-route-return-type

Conversation

@yoannblot

Copy link
Copy Markdown
Contributor

#55631 narrowed the no-argument return type of Request::route() from \Illuminate\Routing\Route|object|string|null to \Illuminate\Routing\Route.
The method still returns null when no route is resolved:

public function route($param = null, $default = null)
{
    $route = call_user_func($this->getRouteResolver());

    if (is_null($route) || is_null($param)) {
        return $route;
    }

    return $route->parameter($param, $default);
}

getRouteResolver() falls back to a closure returning null, which is the case for a request that matched no route: a 404 seen by a terminable middleware, a request built in a test or a console context, or a fallback before routing.

Static analysers now trust the docblock. Mago (since 1.58) reports $request->route()?->getName() as a redundant nullsafe operator, and its autofix removes the ?->. That turns working code into Call to a member function getName() on null at runtime. PHPStan and Psalm users get the same "nullsafe on non-nullable" diagnostics.

This PR keeps the conditional type and adds null to the no-argument branch:

-     * @return ($param is null ? \Illuminate\Routing\Route : object|string|null)
+     * @return ($param is null ? \Illuminate\Routing\Route|null : object|string|null)

and updates the type test:

-assertType('Illuminate\Routing\Route', $request->route());
+assertType('Illuminate\Routing\Route|null', $request->route());

No runtime change.

🤖 Generated with Claude Code

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown

Thanks for submitting a PR!

Note that draft PRs are not reviewed. If you would like a review, please mark your pull request as ready for review in the GitHub user interface.

Pull requests that are abandoned in draft may be closed due to inactivity.

@yoannblot
yoannblot marked this pull request as ready for review October 26, 2026 06:19
@taylorotwell
taylorotwell merged commit ba834b2 into laravel:13.x Oct 26, 2026
64 checks passed
@yoannblot
yoannblot deleted the fix-request-route-return-type branch October 26, 2026 13:18
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

Successfully merging this pull request may close these issues.

2 participants