Issues (7)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/Localization.php (5 issues)

Upgrade to new PHP Analysis Engine

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\Contracts\Container\Container;
6
use Illuminate\Support\Collection;
7
use Illuminate\Support\HtmlString;
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
It seems like you code against a concrete implementation and not the interface Illuminate\Contracts\Container\Container as the method getLocale() 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

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
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

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
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

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
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

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
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

  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...
107 2
            'l10n' => $this,
108 2
        ]))->render());
109
    }
110
}
111