Completed
Pull Request — master (#18)
by Propa
09:42
created

Language   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 120
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 120
loc 120
ccs 23
cts 23
cp 1
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 2

8 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 4 4 1
A name() 4 4 1
A all() 4 4 1
A getLocale() 4 4 1
A setLocale() 8 8 1
A getFallbackLocale() 4 4 1
A setFallbackLocale() 8 8 1
A load() 8 8 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
namespace Propaganistas\LaravelIntl;
4
5
use Illuminate\Support\Arr;
6
use Propaganistas\LaravelIntl\Contracts\Intl;
7
8 View Code Duplication
class Language extends Intl
0 ignored issues
show
Duplication introduced by
This class 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...
9
{
10
    /**
11
     * Loaded localized country data.
12
     *
13
     * @var array
14
     */
15
    protected $data;
16
17
    /**
18
     * The current locale.
19
     *
20
     * @var string $locale
21
     */
22
    protected $locale;
23
24
    /**
25
     * The current locale.
26
     *
27
     * @var string $locale
28
     */
29
    protected $fallbackLocale;
30
31
    /**
32
     * Get a localized record by key.
33
     *
34
     * @param string $key
35
     * @return mixed
36
     */
37 18
    public function get($key)
38
    {
39 18
        return Arr::get($this->all(), $key);
40
    }
41
42
    /**
43
     * Alias of get().
44
     *
45
     * @param string $key
46
     * @return string
47
     */
48 15
    public function name($key)
49
    {
50 15
        return $this->get($key);
51
    }
52
53
    /**
54
     * Get all localized records.
55
     *
56
     * @return array
57
     */
58 21
    public function all()
59
    {
60 21
        return $this->data[$this->getLocale()] + $this->data[$this->getFallbackLocale()];
61
    }
62
63
    /**
64
     * Get the current locale.
65
     *
66
     * @return string
67
     */
68 24
    public function getLocale()
69
    {
70 24
        return $this->locale;
71
    }
72
73
    /**
74
     * Set the current locale.
75
     *
76
     * @param $locale
77
     * @return $this
78
     */
79 24
    public function setLocale($locale)
80
    {
81 24
        $this->locale = $locale;
82
83 24
        $this->load($locale);
84
85 24
        return $this;
86
    }
87
88
    /**
89
     * Get the fallback locale.
90
     *
91
     * @return string
92
     */
93 21
    public function getFallbackLocale()
94
    {
95 21
        return $this->fallbackLocale;
96
    }
97
98
    /**
99
     * Set the fallback locale.
100
     *
101
     * @param $locale
102
     * @return $this
103
     */
104 24
    public function setFallbackLocale($locale)
105
    {
106 24
        $this->fallbackLocale = $locale;
107
108 24
        $this->load($locale);
109
110 24
        return $this;
111
    }
112
113
    /**
114
     * Load the data for the given locale.
115
     *
116
     * @param string $locale
117
     * @return void
118
     */
119 24
    protected function load($locale)
120
    {
121 24
        if (! isset($this->data[$locale])) {
122 24
            $path = base_path('vendor/umpirsky/locale-list/data/'.$locale.'/locales.php');
123
124 24
            $this->data[$locale] = is_file($path) ? require $path : [];
125
        }
126
    }
127
}