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
alies-dev added a commit to psalm/psalm-plugin-laravel that referenced this pull request Nov 20, 2026
…l Laravel versions

The route resolver returns null when no route matched (error pages,
console, queue), and the body is identical on every supported Laravel.
Only the PHPDoc changed in 13.39 (laravel/framework#62191), so the
non-null Route on 12.x contradicted runtime. Move the nullable return
into the common stub and drop the stubs/13.39.0 override.

Refs #1926
alies-dev added a commit to psalm/psalm-plugin-laravel that referenced this pull request Nov 20, 2026
* fix(stubs): type Request::route() without arguments as nullable on Laravel 13.39+

Laravel 13.39 (laravel/framework#62191) declares the no-arg branch as
Route|null; the resolver returns null when no route matched. Override
route() in stubs/13.39.0 and keep the non-null branch for older versions.

Refs #1926

* fix(stubs): type Request::route() without arguments as nullable on all Laravel versions

The route resolver returns null when no route matched (error pages,
console, queue), and the body is identical on every supported Laravel.
Only the PHPDoc changed in 13.39 (laravel/framework#62191), so the
non-null Route on 12.x contradicted runtime. Move the nullable return
into the common stub and drop the stubs/13.39.0 override.

Refs #1926
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