Completed
Push — master ( dd7489...cfb8b6 )
by Alex
02:50
created

Localization   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 102
ccs 47
cts 47
cp 1
rs 10
c 0
b 0
f 0
wmc 19
lcom 1
cbo 2

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A config() 0 4 1
A getLocale() 0 4 1
A setLocale() 0 4 1
A isCurrentLocale() 0 4 1
A getSupportedLocales() 0 4 1
A getSupportedLocalesKeys() 0 4 1
A isSupportedLocale() 0 4 1
A getDefaultLocale() 0 4 1
A isDefaultLocale() 0 4 1
A shouldHideLocaleInUrl() 0 5 2
A route() 0 8 2
A currentRoute() 0 13 2
A renderSwitch() 0 6 1
A getSupportedLocale() 0 10 2
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
Bug introduced by
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
Bug introduced by
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
Bug introduced by
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
Bug introduced by
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
Bug introduced by
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