Completed
Push — master ( 1c0d18...3252bf )
by Aitor Riba
02:04
created

LocalizerMiddleware   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 65
Duplicated Lines 21.54 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 9
c 2
b 1
f 0
lcom 0
cbo 1
dl 14
loc 65
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setIfAllowed() 0 9 2
B setLang() 14 27 6
A handle() 0 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Aitor24\Localizer\Middlewares;
4
5
use Aitor24\Localizer\Facades\LocalizerFacade as Localizer;
6
use Closure;
7
use Illuminate\Support\Facades\App;
8
use Illuminate\Support\Facades\Auth;
9
use Unicodeveloper\Identify\Facades\IdentityFacade as Identify;
10
11
class LocalizerMiddleware
12
{
13
    /**
14
     * This function checks if language to set is an allowed lang of config.
15
     *
16
     * @param string $lang
17
     **/
18
    private function setIfAllowed($lang)
19
    {
20
        $allowedLangs = array_keys(Localizer::allowedLanguages());
21
        if (in_array($lang, $allowedLangs)) {
22
            App::setLocale($lang);
23
        } else {
24
            App::setLocale('en');
25
        }
26
    }
27
28
    /**
29
     * This function checks if user is logged in, and loads the prefered language.
30
     *
31
     * @param string $lang
0 ignored issues
show
Bug introduced by
There is no parameter named $lang. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
32
     **/
33
    public function setLang()
34
    {
35
        if (Auth::check()) {
36
            $user = Auth::user();
37
38
            // Set user locale
39
            if ($user->locale) {
0 ignored issues
show
Bug introduced by
Accessing locale on the interface Illuminate\Contracts\Auth\Authenticatable 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...
40
                $this->setIfAllowed($user->locale);
0 ignored issues
show
Bug introduced by
Accessing locale on the interface Illuminate\Contracts\Auth\Authenticatable 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...
41 View Code Duplication
            } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
42
                if (config('localizer.set_auto_lang')) {
43
                    $this->setIfAllowed(Identify::lang()->getLanguage());
44
                } else {
45
                    $this->setIfAllowed(config('localizer.default_lang'));
46
                }
47
            }
48
        } else {
49
            if ($request->session()->has('locale')) {
0 ignored issues
show
Bug introduced by
The variable $request does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
50
                $this->setIfAllowed(session('locale'));
51 View Code Duplication
            } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
52
                if (config('localizer.set_auto_lang')) {
53
                    $this->setIfAllowed(Identify::lang()->getLanguage());
54
                } else {
55
                    $this->setIfAllowed(config('localizer.default_lang'));
56
                }
57
            }
58
        }
59
    }
60
61
    /**
62
     * Handle an incoming request.
63
     *
64
     * @param \Illuminate\Http\Request $request
65
     * @param \Closure                 $next
66
     *
67
     * @return mixed
68
     */
69
    public function handle($request, Closure $next)
70
    {
71
        $this->setLang();
72
73
        return $next($request);
74
    }
75
}
76