Localize   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 55
Duplicated Lines 14.55 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 4
c 1
b 0
f 1
lcom 1
cbo 3
dl 8
loc 55
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A handle() 8 19 3

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
/*
4
 * This file is part of Gitamin.
5
 *
6
 * Copyright (C) 2015-2016 The Gitamin Team
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Gitamin\Http\Middleware;
13
14
use Closure;
15
use Illuminate\Config\Repository;
16
use Jenssegers\Date\Date;
17
18
class Localize
19
{
20
    /**
21
     * Array of languages Gitamin can use.
22
     *
23
     * @var array
24
     */
25
    protected $langs;
26
27
    /**
28
     * Config repository.
29
     *
30
     * @var \Illuminate\Config\Repository
31
     */
32
    protected $config;
33
34
    /**
35
     * Constructs a new localize instance.
36
     *
37
     * @param \Illuminate\Config\Repository $config
38
     */
39
    public function __construct(Repository $config)
40
    {
41
        $this->config = $config;
42
        $this->langs = $config->get('langs');
0 ignored issues
show
Documentation Bug introduced by
It seems like $config->get('langs') of type * is incompatible with the declared type array of property $langs.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
43
    }
44
45
    /**
46
     * Handle an incoming request.
47
     *
48
     * @param \Illuminate\Http\Request $request
49
     * @param \Closure                 $next
50
     *
51
     * @return mixed
52
     */
53
    public function handle($request, Closure $next)
54
    {
55
        $supportedLanguages = $request->getLanguages();
56
        $userLanguage = $this->config->get('app.locale');
57
58 View Code Duplication
        foreach ($supportedLanguages as $language) {
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...
59
            $language = str_replace('_', '-', $language);
60
61
            if (isset($this->langs[$language])) {
62
                $userLanguage = $language;
63
                break;
64
            }
65
        }
66
67
        app('translator')->setLocale($userLanguage);
68
        Date::setLocale($userLanguage);
69
70
        return $next($request);
71
    }
72
}
73