Completed
Push — master ( 631ec9...a75dc2 )
by Michal
03:19 queued 22s
created

MoLoader::list_locales()   C

Complexity

Conditions 11
Paths 43

Size

Total Lines 45
Code Lines 31

Duplication

Lines 10
Ratio 22.22 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 10
loc 45
rs 5.2653
cc 11
eloc 31
nc 43
nop 1

How to fix   Complexity   

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
    Copyright (c) 2005 Steven Armstrong <sa at c-area dot ch>
4
    Copyright (c) 2009 Danilo Segan <[email protected]>
5
    Copyright (c) 2016 Michal Čihař <[email protected]>
6
7
    This file is part of MoTranslator.
8
9
    This program is free software; you can redistribute it and/or modify
10
    it under the terms of the GNU General Public License as published by
11
    the Free Software Foundation; either version 2 of the License, or
12
    (at your option) any later version.
13
14
    This program is distributed in the hope that it will be useful,
15
    but WITHOUT ANY WARRANTY; without even the implied warranty of
16
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
    GNU General Public License for more details.
18
19
    You should have received a copy of the GNU General Public License along
20
    with this program; if not, write to the Free Software Foundation, Inc.,
21
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22
*/
23
24
namespace MoTranslator;
25
26
class MoLoader {
27
    /**
28
     * @var string Default gettext domain to use.
29
     */
30
    private $default_domain = '';
31
32
    /**
33
     * @var array Loaded domains
34
     */
35
    private $domains = array();
0 ignored issues
show
Unused Code introduced by
The property $domains is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
36
37
    /**
38
     * @var array Bound paths for domains
39
     */
40
    private $paths = array('' => './');
41
42
    /**
43
     * Figure out all possible locale names and start with the most
44
     * specific ones.  I.e. for sr_CS.UTF-8@latin, look through all of
45
     * sr_CS.UTF-8@latin, sr_CS@latin, sr@latin, sr_CS.UTF-8, sr_CS, sr.
46
     *
47
     * @param string $locale Locale code
48
     *
49
     * @return array list of locales to try for any POSIX-style locale specification.
50
     */
51
    public function list_locales($locale) {
52
        $locale_names = array();
53
54
        $lang = NULL;
55
        $country = NULL;
56
        $charset = NULL;
57
        $modifier = NULL;
58
59
        if ($locale) {
60
            if (preg_match("/^(?P<lang>[a-z]{2,3})"              // language code
61
                ."(?:_(?P<country>[A-Z]{2}))?"           // country code
62
                ."(?:\.(?P<charset>[-A-Za-z0-9_]+))?"    // charset
63
                ."(?:@(?P<modifier>[-A-Za-z0-9_]+))?$/",  // @ modifier
64
                $locale, $matches)) {
65
66
                extract($matches);
67
68 View Code Duplication
                if ($modifier) {
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...
69
                    if ($country) {
70
                        if ($charset) {
71
                            array_push($locale_names, "${lang}_$country.$charset@$modifier");
72
                        }
73
                        array_push($locale_names, "${lang}_$country@$modifier");
74
                    } elseif ($charset)
75
                        array_push($locale_names, "${lang}.$charset@$modifier");
76
                        array_push($locale_names, "$lang@$modifier");
77
                    }
78
                if ($country) {
79
                    if ($charset) {
80
                        array_push($locale_names, "${lang}_$country.$charset");
81
                    }
82
                    array_push($locale_names, "${lang}_$country");
83
                } elseif ($charset) {
84
                    array_push($locale_names, "${lang}.$charset");
85
                }
86
                array_push($locale_names, $lang);
87
            }
88
89
            // If the locale name doesn't match POSIX style, just include it as-is.
90
            if (!in_array($locale, $locale_names)) {
91
                array_push($locale_names, $locale);
92
            }
93
        }
94
        return $locale_names;
95
    }
96
97
    public function get_translator($domain='')
98
    {
99
        if (empty($domain)) {
100
            $domain = $this->default_domain;
101
        }
102
103
        if (isset($this->paths[$domain])) {
104
            $base = $this->paths[$domain];
0 ignored issues
show
Unused Code introduced by
$base is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
105
        } else {
106
            $base = './';
0 ignored issues
show
Unused Code introduced by
$base is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
107
        }
108
109
        return '';
110
    }
111
112
    public function bind_domain($domain, $path)
113
    {
114
        $this->paths[$domain] = $path;
115
    }
116
117
    public function set_default_domain($domain)
118
    {
119
        $this->default_domain = $domain;
120
    }
121
}
122