GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

LocaleCookieRedirect   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 3
dl 0
loc 34
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B handle() 0 22 7
1
<?php namespace Mcamara\LaravelLocalization\Middleware;
2
3
use Closure;
4
use Illuminate\Http\RedirectResponse;
5
6
class LocaleCookieRedirect extends LaravelLocalizationMiddlewareBase
7
{
8
9
    /**
10
     * Handle an incoming request.
11
     *
12
     * @param  \Illuminate\Http\Request $request
13
     * @param  \Closure $next
14
     *
15
     * @return mixed
16
     */
17
     public function handle($request, Closure $next) {
18
         // If the URL of the request is in exceptions.
19
         if ($this->shouldIgnore($request)) {
20
             return $next($request);
21
         }
22
23
         $params = explode('/', $request->path());
24
         $locale = $request->cookie('locale', false);
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string|array|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
25
26
         if (\count($params) > 0 && app('laravellocalization')->checkLocaleInSupportedLocales($params[0])) {
27
            return $next($request)->withCookie(cookie()->forever('locale', $params[0]));
0 ignored issues
show
Bug introduced by
The method forever does only exist in Illuminate\Cookie\CookieJar, but not in Symfony\Component\HttpFoundation\Cookie.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
28
         }
29
30
         if ($locale && app('laravellocalization')->checkLocaleInSupportedLocales($locale) && !(app('laravellocalization')->isHiddenDefault($locale))) {
31
           $redirection = app('laravellocalization')->getLocalizedURL($locale);
32
           $redirectResponse = new RedirectResponse($redirection, 302, ['Vary' => 'Accept-Language']);
33
34
           return $redirectResponse->withCookie(cookie()->forever('locale', $params[0]));
35
         }
36
37
         return $next($request);
38
     }
39
}
40