Completed
Pull Request — master (#149)
by
unknown
13:20
created

LocaleCookieRedirect   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 80%

Importance

Changes 0
Metric Value
dl 0
loc 35
ccs 8
cts 10
cp 0.8
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 4

1 Method

Rating   Name   Duplication   Size   Complexity  
B handle() 0 19 7
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arcanedev\Localization\Middleware;
6
7
use Closure;
8
use Illuminate\Http\Request;
9
10
/**
11
 * Class     LocaleCookieRedirect
12
 *
13
 * @package  Arcanedev\Localization\Middleware
14
 * @author   ARCANEDEV <[email protected]>
15
 */
16
class LocaleCookieRedirect extends Middleware
17
{
18
    /* -----------------------------------------------------------------
19
     |  Main Methods
20
     | -----------------------------------------------------------------
21
     */
22
23
    /**
24
     * Handle an incoming request.
25
     *
26
     * @param  \Illuminate\Http\Request  $request
27
     * @param  \Closure                  $next
28
     *
29
     * @return mixed
30
     */
31 6
    public function handle(Request $request, Closure $next)
32
    {
33
        // If the request URL is ignored from localization.
34 6
        if ($this->shouldIgnore($request) || class_basename($next($request)) === 'StreamedResponse') return $next($request);
35
36 6
        $segment = $request->segment(1, null);
37
38 6
        if ($this->localization->isLocaleSupported($segment))
39
            return $next($request)->withCookie(cookie()->forever('locale', $segment));
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...
40
41 6
        $locale  = $request->cookie('locale', null);
42
43 6
        if ($locale !== null && ! $this->isDefaultLocaleHidden($locale)) {
0 ignored issues
show
Bug introduced by
It seems like $locale defined by $request->cookie('locale', null) on line 41 can also be of type array; however, Arcanedev\Localization\M...isDefaultLocaleHidden() does only seem to accept string|null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
44 6
            if ( ! is_null($redirect = $this->getLocalizedRedirect($locale)))
0 ignored issues
show
Bug introduced by
It seems like $locale defined by $request->cookie('locale', null) on line 41 can also be of type array; however, Arcanedev\Localization\M...:getLocalizedRedirect() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
45 6
                return $redirect->withCookie(cookie()->forever('locale', $segment));
46
        }
47
48
        return $next($request);
49
    }
50
}
51