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
|
|||||
28 | { |
||||
29 | $this->locale = app(config('laravel-multilingual.locale_class')); |
||||
0 ignored issues
–
show
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 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;
}
![]() |
|||||
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
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
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. ![]() |
|||||
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
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
![]() |
|||||
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
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
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. ![]() |
|||||
80 | } |
||||
81 | } |
||||
82 | |||||
83 | return $this->locale->default; |
||||
0 ignored issues
–
show
The property
default does not exist on mindtwo\LaravelMultilingual\Services\Locale . Since you implemented __get , consider adding a @property annotation.
![]() |
|||||
84 | } |
||||
85 | } |
||||
86 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.