Completed
Pull Request — master (#157)
by
unknown
10:43
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
 * @author   ARCANEDEV <[email protected]>
14
 */
15
class LocaleCookieRedirect extends Middleware
16
{
17
    /* -----------------------------------------------------------------
18
     |  Main Methods
19
     | -----------------------------------------------------------------
20
     */
21
22
    /**
23
     * Handle an incoming request.
24
     *
25
     * @param  \Illuminate\Http\Request  $request
26
     * @param  \Closure                  $next
27
     *
28
     * @return mixed
29
     */
30 4
    public function handle(Request $request, Closure $next)
31
    {
32
        // If the request URL is ignored from localization.
33 4
        if ($this->shouldIgnore($request)) return $next($request);
34
35 4
        $segment = $request->segment(1, null);
36
37 4
        if ($this->localization->isLocaleSupported($segment))
38
            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...
39
40 4
        $locale  = $request->cookie('locale', null);
41
42 4
        if ($this->localization->isLocaleSupported($locale) && $locale !== null && ! $this->isDefaultLocaleHidden($locale)) {
0 ignored issues
show
Bug introduced by
It seems like $locale defined by $request->cookie('locale', null) on line 40 can also be of type array or null; however, Arcanedev\Localization\C...on::isLocaleSupported() does only seem to accept string|boolean, 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...
Bug introduced by
It seems like $locale defined by $request->cookie('locale', null) on line 40 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...
43 4
            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 40 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...
44 4
                return $redirect->withCookie(cookie()->forever('locale', $locale));
0 ignored issues
show
Bug introduced by
It seems like $locale defined by $request->cookie('locale', null) on line 40 can also be of type array; however, Illuminate\Cookie\CookieJar::forever() 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
        }
46
47
        return $next($request);
48
    }
49
}
50