Passed
Push — 2.x ( e8dd26...ba8025 )
by Quentin
07:25
created

getCodeToLanguageMappings()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 153
Code Lines 151

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 151
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 153
ccs 0
cts 1
cp 0
crap 2
rs 8

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
use Illuminate\Support\Collection;
4
5 1
if (!function_exists('twillTrans')) {
6
    function twillTrans($key, $replace = [])
7
    {
8 52
        $locale = config('twill.locale', config('twill.fallback_locale', 'en'));
9 52
        return trans($key, $replace, $locale);
10
    }
11
}
12
13 1
if (!function_exists('getLocales')) {
14
    /**
15
     * @return string[]
16
     */
17
    function getLocales()
18
    {
19 69
        return config('translatable.locales') ?? [config('app.locale')];
20
    }
21
}
22
23 1
if (!function_exists('getLanguagesForVueStore')) {
24
    /**
25
     * @param array $form_fields
26
     * @param bool $translate
27
     * @return array
28
     */
29
    function getLanguagesForVueStore($form_fields = [], $translate = true)
30
    {
31 49
        $manageMultipleLanguages = count(getLocales()) > 1;
32 49
        if ($manageMultipleLanguages && $translate) {
33
            $allLanguages = Collection::make(config('translatable.locales'))->map(function ($locale, $index) use ($form_fields) {
34
                return [
35 49
                    'shortlabel' => strtoupper($locale),
36 49
                    'label' => getLanguageLabelFromLocaleCode($locale),
37 49
                    'value' => $locale,
38
                    'disabled' => false,
39 49
                    'published' => $form_fields['translations']['active'][$locale] ?? ($index === 0),
40
                ];
41 49
            });
42
43
            return [
44 49
                'all' => $allLanguages,
45 49
                'active' => request()->has('lang') ? $allLanguages->where('value', request('lang'))->first() : null,
46
            ];
47
        }
48
49 1
        $locale = config('app.locale');
50
51
        return [
52
            'all' => [
53
                [
54 1
                    'shortlabel' => strtoupper($locale),
55 1
                    'label' => getLanguageLabelFromLocaleCode($locale),
56 1
                    'value' => $locale,
57
                    'disabled' => false,
58
                    'published' => true,
59
                ],
60
            ],
61
        ];
62
    }
63
}
64
65 1
if (!function_exists('getLanguageLabelFromLocaleCode')) {
66
    /**
67
     * @param string $code
68
     * @return string
69
     */
70
    function getLanguageLabelFromLocaleCode($code, $native = false)
71
    {
72 49
        if (class_exists(Locale::class)) {
73 49
            if ($native) {
74 1
                return ucfirst(Locale::getDisplayLanguage($code, $code));
75
            } else {
76 49
                return ucfirst(Locale::getDisplayLanguage($code, 'en'));
77
            }
78
        }
79
80
        $codeToLanguageMappings = getCodeToLanguageMappings();
81
82
        if (isset($codeToLanguageMappings[$code])) {
83
            $lang = $codeToLanguageMappings[$code];
84
            if (is_array($lang) && isset($lang[1]) && $native) {
85
                return $lang[1];
86
            }
87
            return $lang;
88
        }
89
        return $code;
90
    }
91
}
92
93
/**
94
 * Converts camelCase string to have spaces between each.
95
 * @param string $camelCaseString
96
 * @return string (ex.: camel case string)
97
 */
98 1
if (!function_exists('camelCaseToWords')) {
99
    function camelCaseToWords($camelCaseString)
100
    {
101 44
        $re = '/(?<=[a-z])(?=[A-Z])/x';
102 44
        $a = preg_split($re, $camelCaseString);
103 44
        $words = join(" ", $a);
0 ignored issues
show
Bug introduced by
It seems like $a can also be of type false; however, parameter $pieces of join() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

103
        $words = join(" ", /** @scrutinizer ignore-type */ $a);
Loading history...
104 44
        return ucfirst(strtolower($words));
105
    }
106
}
107
108 1
if (!function_exists('getCodeToLanguageMappings')) {
109
    function getCodeToLanguageMappings()
110
    {
111
        return [
112
            'ab' => 'Abkhazian',
113
            'aa' => 'Afar',
114
            'af' => 'Afrikaans',
115
            'sq' => 'Albanian',
116
            'am' => 'Amharic',
117
            'ar' => ['Arabic', 'العربية'],
118
            'an' => 'Aragonese',
119
            'hy' => 'Armenian',
120
            'as' => 'Assamese',
121
            'ay' => 'Aymara',
122
            'az' => 'Azerbaijani',
123
            'ba' => 'Bashkir',
124
            'eu' => 'Basque',
125
            'bn' => 'Bengali (Bangla)',
126
            'dz' => 'Bhutani',
127
            'bh' => 'Bihari',
128
            'bi' => 'Bislama',
129
            'br' => 'Breton',
130
            'bg' => ['Bulgarian', 'български език'],
131
            'my' => 'Burmese',
132
            'be' => 'Byelorussian (Belarusian)',
133
            'km' => 'Cambodian',
134
            'ca' => 'Catalan',
135
            'zh' => 'Chinese',
136
            'zh-Hans' => ['Chinese (simplified)', '简体中文'],
137
            'zh-Hant' => ['Chinese (traditional)', '繁體中文'],
138
            'co' => 'Corsican',
139
            'hr' => 'Croatian',
140
            'cs' => ['Czech', 'čeština'],
141
            'da' => ['Danish', 'Dansk'],
142
            'nl' => ['Dutch', 'Nederlands'],
143
            'en' => 'English',
144
            'eo' => 'Esperanto',
145
            'et' => 'Estonian',
146
            'fo' => 'Faeroese',
147
            'fa' => 'Farsi',
148
            'fj' => ['Fiji', 'Suomi'],
149
            'fi' => 'Finnish',
150
            'fr' => ['French', 'Français'],
151
            'fy' => 'Frisian',
152
            'gd' => 'Gaelic (Scottish)',
153
            'gv' => 'Gaelic (Manx)',
154
            'gl' => 'Galician',
155
            'ka' => 'Georgian',
156
            'de' => ['German', 'Deutsch'],
157
            'el' => ['Greek', 'Ελληνικά'],
158
            'kl' => 'Kalaallisut (Greenlandic)',
159
            'gn' => 'Guarani',
160
            'gu' => 'Gujarati',
161
            'ht' => 'Haitian Creole',
162
            'ha' => 'Hausa',
163
            'he' => 'Hebrew',
164
            'iw' => 'Hebrew',
165
            'hi' => 'Hindi',
166
            'hu' => ['Hungarian', 'Magyar'],
167
            'is' => 'Icelandic',
168
            'io' => 'Ido',
169
            'id' => 'Indonesian',
170
            'in' => 'Indonesian',
171
            'ia' => 'Interlingua',
172
            'ie' => 'Interlingue',
173
            'iu' => 'Inuktitut',
174
            'ik' => 'Inupiak',
175
            'ga' => 'Irish',
176
            'it' => ['Italian', 'Italiano'],
177
            'ja' => ['Japanese', '日本語'],
178
            'jv' => 'Javanese',
179
            'kn' => 'Kannada',
180
            'ks' => 'Kashmiri',
181
            'kk' => 'Kazakh',
182
            'rw' => 'Kinyarwanda (Ruanda)',
183
            'ky' => 'Kirghiz',
184
            'rn' => 'Kirundi (Rundi)',
185
            'ko' => ['Korean', '한국어'],
186
            'ku' => 'Kurdish',
187
            'lo' => 'Laothian',
188
            'la' => 'Latin',
189
            'lv' => 'Latvian (Lettish)',
190
            'li' => 'Limburgish ( Limburger)',
191
            'ln' => 'Lingala',
192
            'lt' => 'Lithuanian',
193
            'mk' => 'Macedonian',
194
            'mg' => 'Malagasy',
195
            'ms' => 'Malay',
196
            'ml' => 'Malayalam',
197
            'mt' => 'Maltese',
198
            'mi' => 'Maori',
199
            'mr' => 'Marathi',
200
            'mo' => 'Moldavian',
201
            'mn' => 'Mongolian',
202
            'na' => 'Nauru',
203
            'ne' => 'Nepali',
204
            'no' => 'Norwegian',
205
            'oc' => 'Occitan',
206
            'or' => 'Oriya',
207
            'om' => 'Oromo (Afan, Galla)',
208
            'ps' => 'Pashto (Pushto)',
209
            'pl' => ['Polish', 'Polski'],
210
            'pt' => ['Portuguese', 'Português'],
211
            'pa' => 'Punjabi',
212
            'qu' => 'Quechua',
213
            'rm' => 'Rhaeto-Romance',
214
            'ro' => ['Romanian', 'Română'],
215
            'ru' => ['Russian', 'Русский'],
216
            'sm' => 'Samoan',
217
            'sg' => 'Sango',
218
            'sa' => 'Sanskrit',
219
            'sr' => 'Serbian',
220
            'sh' => 'Serbo-Croatian',
221
            'st' => 'Sesotho',
222
            'tn' => 'Setswana',
223
            'sn' => 'Shona',
224
            'ii' => 'Sichuan Yi',
225
            'sd' => 'Sindhi',
226
            'si' => 'Sinhalese',
227
            'ss' => 'Siswati',
228
            'sk' => 'Slovak',
229
            'sl' => 'Slovenian',
230
            'so' => 'Somali',
231
            'es' => ['Spanish', 'Español'],
232
            'su' => 'Sundanese',
233
            'sw' => 'Swahili (Kiswahili)',
234
            'sv' => ['Swedish', 'Svenska'],
235
            'tl' => 'Tagalog',
236
            'tg' => 'Tajik',
237
            'ta' => 'Tamil',
238
            'tt' => 'Tatar',
239
            'te' => 'Telugu',
240
            'th' => ['Thai', 'ไทย'],
241
            'bo' => 'Tibetan',
242
            'ti' => 'Tigrinya',
243
            'to' => 'Tonga',
244
            'ts' => 'Tsonga',
245
            'tr' => ['Turkish', 'Türkçe'],
246
            'tk' => 'Turkmen',
247
            'tw' => 'Twi',
248
            'ug' => 'Uighur',
249
            'uk' => ['Ukrainian', 'Українська'],
250
            'ur' => 'Urdu',
251
            'uz' => 'Uzbek',
252
            'vi' => 'Vietnamese',
253
            'vo' => 'Volapük',
254
            'wa' => 'Wallon',
255
            'cy' => 'Welsh',
256
            'wo' => 'Wolof',
257
            'xh' => 'Xhosa',
258
            'yi' => 'Yiddish',
259
            'ji' => 'Yiddish',
260
            'yo' => 'Yoruba',
261
            'zu' => 'Zulu',
262
        ];
263
    }
264
}
265