Completed
Pull Request — master (#157)
by
unknown
10:43
created

LocaleCookieRedirect::handle()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 7.392

Importance

Changes 0
Metric Value
cc 7
nc 5
nop 2
dl 0
loc 19
ccs 8
cts 10
cp 0.8
crap 7.392
rs 8.8333
c 0
b 0
f 0
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