Completed
Pull Request — master (#149)
by
unknown
13:20
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
 * @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