These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace AlexJoffroy\Localization; |
||
4 | |||
5 | use Illuminate\Support\Collection; |
||
6 | use Illuminate\Support\HtmlString; |
||
7 | use Illuminate\Contracts\Container\Container; |
||
8 | |||
9 | class Localization |
||
10 | { |
||
11 | /** @var \Illuminate\Contracts\Container\Container */ |
||
12 | protected $app; |
||
13 | |||
14 | 27 | public function __construct(Container $app) |
|
15 | { |
||
16 | 27 | $this->app = $app; |
|
17 | 27 | } |
|
18 | |||
19 | 23 | protected function config(string $key) |
|
20 | { |
||
21 | 23 | return config("localization.$key"); |
|
22 | } |
||
23 | |||
24 | 21 | public function getLocale(): string |
|
25 | { |
||
26 | 21 | return $this->app->getLocale(); |
|
0 ignored issues
–
show
|
|||
27 | } |
||
28 | |||
29 | 22 | public function setLocale(string $locale = '') |
|
30 | { |
||
31 | 22 | $this->app->setLocale($locale); |
|
0 ignored issues
–
show
It seems like you code against a concrete implementation and not the interface
Illuminate\Contracts\Container\Container as the method setLocale() does only exist in the following implementations of said interface: Illuminate\Foundation\Application .
Let’s take a look at an example: interface User
{
/** @return string */
public function getPassword();
}
class MyUser implements User
{
public function getPassword()
{
// return something
}
public function getDisplayName()
{
// return some name.
}
}
class AuthSystem
{
public function authenticate(User $user)
{
$this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
// do something.
}
}
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break. Available Fixes
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types
inside the if block in such a case.
Loading history...
|
|||
32 | 22 | } |
|
33 | |||
34 | 3 | public function isCurrentLocale(string $locale = ''): bool |
|
35 | { |
||
36 | 3 | return $locale === $this->getLocale(); |
|
37 | } |
||
38 | |||
39 | 22 | public function getSupportedLocale(string $locale = ''): Collection |
|
40 | { |
||
41 | 22 | $locales = $this->getSupportedLocales(); |
|
42 | |||
43 | 22 | if ($locales->has($locale)) { |
|
44 | 22 | return collect($locales->get($locale)); |
|
45 | } |
||
46 | |||
47 | 1 | return collect([]); |
|
48 | } |
||
49 | |||
50 | 23 | public function getSupportedLocales(): Collection |
|
51 | { |
||
52 | 23 | return collect($this->config('supported_locales')); |
|
53 | } |
||
54 | |||
55 | 19 | public function getSupportedLocalesKeys(): Collection |
|
56 | { |
||
57 | 19 | return $this->getSupportedLocales()->keys(); |
|
58 | } |
||
59 | |||
60 | 9 | public function isSupportedLocale(string $locale = ''): bool |
|
61 | { |
||
62 | 9 | return $this->getSupportedLocales()->has($locale); |
|
63 | } |
||
64 | |||
65 | 4 | public function getDefaultLocale(): string |
|
66 | { |
||
67 | 4 | return $this->config('default_locale'); |
|
68 | } |
||
69 | |||
70 | 2 | public function isDefaultLocale(string $locale = ''): bool |
|
71 | { |
||
72 | 2 | return $locale === $this->getDefaultLocale(); |
|
73 | } |
||
74 | |||
75 | 19 | public function shouldHideLocaleInUrl($locale) |
|
76 | { |
||
77 | 19 | return $this->config('hide_default_locale_in_url') |
|
78 | 19 | && $this->isDefaultLocale($locale); |
|
79 | } |
||
80 | |||
81 | 6 | public function route(string $name, array $parameters = [], bool $absolute = true, string $locale = ''): string |
|
82 | { |
||
83 | 6 | $locale = $this->isSupportedLocale($locale) ? $locale : $this->getLocale(); |
|
84 | 6 | $localesPattern = $this->getSupportedLocalesKeys()->implode('|'); |
|
85 | 6 | $name = preg_replace("/^($localesPattern)\./", '', $name); |
|
86 | |||
87 | 6 | return $this->app->url->route("$locale.$name", $parameters, $absolute); |
|
0 ignored issues
–
show
Accessing
url on the interface Illuminate\Contracts\Container\Container suggest that you code against a concrete implementation. How about adding an instanceof check?
If you access a property on an interface, you most likely code against a concrete implementation of the interface. Available Fixes
Loading history...
|
|||
88 | } |
||
89 | |||
90 | 2 | public function currentRoute(string $locale, bool $absolute = true): string |
|
91 | { |
||
92 | 2 | $request = $this->app->request; |
|
0 ignored issues
–
show
Accessing
request on the interface Illuminate\Contracts\Container\Container suggest that you code against a concrete implementation. How about adding an instanceof check?
If you access a property on an interface, you most likely code against a concrete implementation of the interface. Available Fixes
Loading history...
|
|||
93 | 2 | $routeName = $request->route()->getName(); |
|
94 | 2 | $parameters = $request->route()->parameters(); |
|
95 | 2 | $url = $this->route($routeName, $parameters, $absolute, $locale); |
|
96 | |||
97 | 2 | if ($query = $request->query()) { |
|
98 | 1 | $url .= '?' . http_build_query($query); |
|
99 | } |
||
100 | |||
101 | 2 | return $url; |
|
102 | } |
||
103 | |||
104 | 2 | public function renderSwitch(string $view = 'localization::switch', array $data = []): HtmlString |
|
105 | { |
||
106 | 2 | return new HtmlString(view($view, array_merge($data, [ |
|
0 ignored issues
–
show
The method
render does only exist in Illuminate\View\View , but not in Illuminate\Contracts\View\Factory .
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
Loading history...
|
|||
107 | 2 | 'l10n' => $this, |
|
108 | 2 | ]))->render()); |
|
109 | } |
||
110 | } |
||
111 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: