Completed
Push — master ( 8877c9...0f6ba2 )
by Aitor Riba
01:52
created

Builder   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 129
Duplicated Lines 31.01 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 5
Bugs 0 Features 2
Metric Value
wmc 17
c 5
b 0
f 2
lcom 1
cbo 0
dl 40
loc 129
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A allowedLanguages() 0 8 2
A isAllowedLanguage() 0 4 1
A addNames() 20 20 4
A addCodes() 20 20 4
A setRoute() 0 4 1
A setRouteHome() 0 4 1
A getCode() 0 6 2
A getLanguage() 0 5 2

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;
4
5
use Illuminate\Support\Facades\App;
6
7
class Builder
8
{
9
    /**
10
     * Get all allowed languages.
11
     *
12
     * @return array
13
     **/
14
    public static function allowedLanguages()
15
    {
16
        if (config('localizer.allowed_langs')) {
17
            return self::addNames(array_merge(config('localizer.allowed_langs'), [config('localizer.default_lang')]));
18
        } else {
19
            return self::addNames([config('localizer.default_lang')]);
20
        }
21
    }
22
23
    /**
24
     * Return true if $code is an allowed lang.
25
     *
26
     * @return bool
27
     **/
28
    public static function isAllowedLanguage($code)
29
    {
30
        return in_array($code, array_keys(self::allowedLanguages()) );
31
    }
32
33
    /**
34
     * Add names to an array of language codes as [$code => $language].
35
     *
36
     * @param array $codes
37
     *
38
     * @return array
39
     **/
40 View Code Duplication
    public static function addNames($codes)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
41
    {
42
        // Read and decode JSON
43
        $json_data = json_decode(file_get_contents(__DIR__.'/languages.json'), true);
44
45
        $array = [];
46
47
        // Generate an array with $code as key and $code language as value
48
        foreach ($codes as $code) {
49
            $lang_name = 'Unknoun';
50
            foreach ($json_data as $lang_data) {
51
                if ($lang_data['code'] == $code) {
52
                    $lang_name = $lang_data['name'];
53
                }
54
            }
55
            $array[$code] = $lang_name;
56
        }
57
58
        return $array;
59
    }
60
61
    /**
62
     * Add names to an array of language codes as [$language => $code].
63
     *
64
     * @param array $langs
65
     *
66
     * @return array
67
     **/
68 View Code Duplication
    public static function addCodes($langs)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
69
    {
70
        // Read and decode JSON
71
        $json_data = json_decode(file_get_contents(__DIR__.'/languages.json'), true);
72
73
        $array = [];
74
75
        // Generate an array with $lang as key and $lang code as value
76
        foreach ($langs as $lang) {
77
            $code = 'unk';
78
            foreach ($json_data as $lang_data) {
79
                if ($lang_data['name'] == $lang) {
80
                    $code = $lang_data['code'];
81
                }
82
            }
83
            $array[$lang] = $code;
84
        }
85
86
        return $array;
87
    }
88
89
    /**
90
     * Returns the url to set up language and return back.
91
     *
92
     * @param string $code
93
     *
94
     * @return string
95
     **/
96
    public static function setRoute($code)
97
    {
98
        return route('localizer::setLocale', ['locale' => $code]);
99
    }
100
101
    /**
102
     * Returns the url to set up language and return to url('/').
103
     *
104
     * @param string $code
105
     *
106
     * @return string
107
     **/
108
    public static function setRouteHome($code)
109
    {
110
        return route('localizer::setLocaleHome', ['locale' => $code]);
111
    }
112
113
    /**
114
     * Returns the current language code.
115
     *
116
     * @return string
117
     **/
118
    public static function getCode($name = 'default')
119
    {
120
        if ($name == 'default') $name = self::getLanguage();
121
122
        return self::addCodes([$name])[$name];
123
    }
124
125
    /**
126
     * Returns the language name.
127
     *
128
     * @return string
129
     **/
130
    public static function getLanguage($code = 'default')
131
    {
132
        if ($code == 'default') $code = App::getLocale();
133
        return self::addNames([$code])[$code];
134
    }
135
}
136