Localization::detectAndSetLocale()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 7
rs 10
1
<?php
2
3
namespace mindtwo\LaravelMultilingual\Http\Middleware;
4
5
use Closure;
6
use Illuminate\Http\Request;
7
use mindtwo\LaravelMultilingual\Services\Locale;
8
9
class Localization
10
{
11
    /**
12
     * @var Locale
13
     */
14
    protected $locale;
15
16
    /**
17
     * Handle an incoming request.
18
     *
19
     * @param Request     $request
20
     * @param Closure     $next
21
     * @param string|null $guard
22
     *
23
     * @throws \Illuminate\Container\EntryNotFoundException
24
     *
25
     * @return mixed
26
     */
27
    public function handle($request, Closure $next, $guard = null)
0 ignored issues
show
Unused Code introduced by
The parameter $guard is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

27
    public function handle($request, Closure $next, /** @scrutinizer ignore-unused */ $guard = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
28
    {
29
        $this->locale = app(config('laravel-multilingual.locale_class'));
0 ignored issues
show
Documentation Bug introduced by
It seems like app(config('laravel-multilingual.locale_class')) can also be of type Illuminate\Contracts\Foundation\Application. However, the property $locale is declared as type mindtwo\LaravelMultilingual\Services\Locale. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
30
31
        // Detect and save locale
32
        $newLocale = $this->detectAndSetLocale($request);
33
34
        // Create locale cookie
35
        $localeCookie = cookie()->forever('locale', $newLocale);
36
37
        // Redirect with cookie
38
        foreach (config('laravel-multilingual.locale_redirectors') as $redirector) {
39
            $redirector = app($redirector);
40
            if ($redirector->match($request, $newLocale, $this->locale->current)) {
0 ignored issues
show
Bug introduced by
The method match() does not exist on Illuminate\Contracts\Foundation\Application. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

40
            if ($redirector->/** @scrutinizer ignore-call */ match($request, $newLocale, $this->locale->current)) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
41
                dd('test');
42
43
                return $redirector->redirect($newLocale)->withCookie($localeCookie);
44
            }
45
        }
46
47
        // Proceed with cookie
48
        return $next($request)->withCookie($localeCookie);
49
    }
50
51
    /**
52
     * @param $request
53
     *
54
     * @throws \Illuminate\Container\EntryNotFoundException
55
     *
56
     * @return string locale
57
     */
58
    public function detectAndSetLocale($request): string
59
    {
60
        $locale = $this->getRequestedLocale($request);
61
62
        app()->setLocale($locale);
0 ignored issues
show
introduced by
The method setLocale() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

62
        app()->/** @scrutinizer ignore-call */ setLocale($locale);
Loading history...
63
64
        return $locale;
65
    }
66
67
    /**
68
     * @param Request $request
69
     *
70
     * @throws \Illuminate\Container\EntryNotFoundException
71
     *
72
     * @return string
73
     */
74
    protected function getRequestedLocale(Request $request): string
75
    {
76
        foreach (config('laravel-multilingual.locale_detectors') as $detector) {
77
            $detector = app($detector);
78
            if ($detector->match($request, $this->locale)) {
79
                return $detector->get($request, $this->locale);
0 ignored issues
show
Unused Code introduced by
The call to Illuminate\Container\Container::get() has too many arguments starting with $this->locale. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

79
                return $detector->/** @scrutinizer ignore-call */ get($request, $this->locale);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
Bug Best Practice introduced by
The expression return $detector->get($request, $this->locale) could return the type object which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
80
            }
81
        }
82
83
        return $this->locale->default;
0 ignored issues
show
Bug Best Practice introduced by
The property default does not exist on mindtwo\LaravelMultilingual\Services\Locale. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The expression return $this->locale->default could return the type Illuminate\Support\HigherOrderCollectionProxy which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
84
    }
85
}
86