Completed
Pull Request — master (#498)
by Richard
11:14
created

Language::load()   B

Complexity

Conditions 6
Paths 24

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 6.0359

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 6
eloc 11
nc 24
nop 3
dl 0
loc 17
ccs 9
cts 10
cp 0.9
crap 6.0359
rs 8.8571
c 1
b 1
f 0
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
 * @link      http://xoops.org
23
 */
24
class Language
25
{
26
    /**
27
     * Attempt a translation of a simple string
28
     *
29
     * @param string $string string to translate
30
     * @param string $domain language domain
31
     *
32
     * @return string translated string
33
     *
34
     * @todo do something useful
35
     */
36 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...
37
    {
38 1
        return $string;
39
    }
40
41
    /**
42
     * load - load a language file
43
     *
44
     * @param string $name     name of the language file
45
     * @param string $domain   domain or module supplying language file
46
     * @param string $language language folder name
47
     *
48
     * @return bool true if loaded, otherwise false
49
     */
50 3
    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...
51
    {
52 3
        if (empty($language)) {
53 3
            if (!empty($GLOBALS['xoopsConfig']['language'])) {
54
                $language = $GLOBALS['xoopsConfig']['language'];
55
            } else {
56 3
                $language = 'english';
57
            }
58
        }
59 3
        $path = \XoopsBaseConfig::get('root-path') . '/' . ((empty($domain) || 'global' === $domain) ? ''
60 3
            : "modules/{$domain}/") . 'language';
61 3
        if (!$ret = static::loadFile("{$path}/{$language}/{$name}.php")) {
62 1
            $ret = static::loadFile("{$path}/english/{$name}.php");
63
        }
64
65 2
        return $ret;
66
    }
67
68
    /**
69
     * Load a file
70
     *
71
     * @param string $filename filename to load
72
     *
73
     * @return bool true if file exists and was loaded
74
     *
75
     * @throws \InvalidArgumentException
76
     */
77 1
    protected static function loadFile($filename)
78
    {
79 1
        if (preg_match('/[[:cntrl:]]/i', $filename)) {
80
            throw new \InvalidArgumentException('Security check: Illegal character in filename');
81
        }
82 1
        if (file_exists($filename)) {
83 1
            include_once $filename;
84 1
            return true;
85
        }
86
        return false;
87
    }
88
}
89