Larastan: Method Authenticate::redirectTo() should return string|null

Created on 30 Aug 2020  路  4Comments  路  Source: nunomaduro/larastan

  • Larastan Version: 0.6.2
  • --level used: 5

Description

Hello!
I'm trying Larastan in a new and really small project, which is a much better experience than doing so in an old and messy one... 馃槄

When running the checks I get this error in a default Laravel file that has no modifications:

 ------ ------------------------------------------------------------------------------------------------------------------ 
  Line   Http/Middleware/Authenticate.php                                                                                  
 ------ ------------------------------------------------------------------------------------------------------------------ 
  17     Method App\Http\Middleware\Authenticate::redirectTo() should return string|null but return statement is missing.  
 ------ ------------------------------------------------------------------------------------------------------------------ 

Laravel code where the issue was found

It's a default Laravel file: app/Http/Middleware/Authenticate.php

namespace App\Http\Middleware;

use Illuminate\Auth\Middleware\Authenticate as Middleware;

class Authenticate extends Middleware
{
    /**
     * Get the path the user should be redirected to when they are not authenticated.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return string|null
     */
    protected function redirectTo($request)
    {
        if (! $request->expectsJson()) {
            return route('login');
        }
    }
}

Is this something PHPStan/Larastan will eventually understand?

question

Most helpful comment

Hahaha 鉂わ笍

All 4 comments

If that condition is false PHP implicitly returns null.

Write readable code! Be explicit! Add return null; to the end of that method.

Yep! Forgot to mention that's exactly the code change I made.

So it's just a matter of PHPStan wanting the explicit return statement?

So it's just a matter of PHPStan wanting the explicit return statement?

We humans 馃 also want explicit code 馃槂

Hahaha 鉂わ笍

Was this page helpful?
0 / 5 - 0 ratings