Completed
Push — master ( fe4c2e...b05117 )
by Michael
12s
created

Language   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 52.63%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 10
c 1
b 1
f 0
lcom 0
cbo 1
dl 0
loc 65
rs 10
ccs 10
cts 19
cp 0.5263

3 Methods

Rating   Name   Duplication   Size   Complexity  
A translate() 0 4 1
B load() 0 17 6
A loadFile() 0 11 3
1
<?php
2
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
 */
11
12
namespace Xmf;
13
14
/**
15
 * Language
16
 *
17
 * @category  Xmf\Language
18
 * @package   Xmf
19
 * @author    trabis <[email protected]>
20
 * @copyright 2011-2016 XOOPS Project (http://xoops.org)
21
 * @license   GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
22
 * @version   Release: 1.0
23
 * @link      http://xoops.org
24
 * @since     1.0
25
 */
26
class Language
27
{
28
    /**
29
     * Attempt a translation of a simple string
30
     *
31
     * @param string $string string to translate
32
     * @param string $domain language domain
33
     *
34
     * @return string translated string
35
     *
36
     * @todo do something useful
37
     */
38 1
    public static function translate($string, $domain = null)
0 ignored issues
show
Unused Code introduced by
The parameter $domain is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
39
    {
40 1
        return $string;
41
    }
42
43
    /**
44
     * load - load a language file
45
     *
46
     * @param string $name     name of the language file
47
     * @param string $domain   domain or module supplying language file
48
     * @param string $language language folder name
49
     *
50
     * @return bool true if loaded, otherwise false
51
     */
52 1
    public static function load($name, $domain = '', $language = null)
0 ignored issues
show
Coding Style introduced by
load uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
53
    {
54 1
        if (empty($language)) {
55 1
            if (!empty($GLOBALS['xoopsConfig']['language'])) {
56
                $language = $GLOBALS['xoopsConfig']['language'];
57
            } else {
58 1
                $language = 'english';
59
            }
60
        }
61 1
        $path = \XoopsBaseConfig::get('root-path') . '/' . ((empty($domain) || 'global' === $domain) ? ''
62 1
            : "modules/{$domain}/") . 'language';
63 1
        if (!$ret = static::loadFile("{$path}/{$language}/{$name}.php")) {
64
            $ret = static::loadFile("{$path}/english/{$name}.php");
65
        }
66
67 1
        return $ret;
68
    }
69
70
    /**
71
     * Load a file
72
     *
73
     * @param string $filename filename to load
74
     *
75
     * @return bool true if file exists and was loaded
76
     *
77
     * @throws \InvalidArgumentException
78
     */
79
    protected static function loadFile($filename)
80
    {
81
        if (preg_match('/[^a-z0-9\\/\\\\_.:-]/i', $filename)) {
82
            throw new \InvalidArgumentException('Security check: Illegal character in filename');
83
        }
84
        if (file_exists($filename)) {
85
            include_once $filename;
86
            return true;
87
        }
88
        return false;
89
    }
90
}
91