Completed
Push — master ( 33f65d...fb9c39 )
by Richard
12s
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
 * @since     1.0
24
 */
25
class Language
26
{
27
    /**
28
     * Attempt a translation of a simple string
29
     *
30
     * @param string $string string to translate
31
     * @param string $domain language domain
32
     *
33
     * @return string translated string
34
     *
35
     * @todo do something useful
36
     */
37 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...
38
    {
39 1
        return $string;
40
    }
41
42
    /**
43
     * load - load a language file
44
     *
45
     * @param string $name     name of the language file
46
     * @param string $domain   domain or module supplying language file
47
     * @param string $language language folder name
48
     *
49
     * @return bool true if loaded, otherwise false
50
     */
51 2
    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...
52
    {
53 2
        if (empty($language)) {
54 2
            if (!empty($GLOBALS['xoopsConfig']['language'])) {
55
                $language = $GLOBALS['xoopsConfig']['language'];
56
            } else {
57 2
                $language = 'english';
58
            }
59
        }
60 2
        $path = \XoopsBaseConfig::get('root-path') . '/' . ((empty($domain) || 'global' === $domain) ? ''
61 2
            : "modules/{$domain}/") . 'language';
62 2
        if (!$ret = static::loadFile("{$path}/{$language}/{$name}.php")) {
63 1
            $ret = static::loadFile("{$path}/english/{$name}.php");
64
        }
65
66 1
        return $ret;
67
    }
68
69
    /**
70
     * Load a file
71
     *
72
     * @param string $filename filename to load
73
     *
74
     * @return bool true if file exists and was loaded
75
     *
76
     * @throws \InvalidArgumentException
77
     */
78
    protected static function loadFile($filename)
79
    {
80
        if (preg_match('/[[:cntrl:]]/i', $filename)) {
81
            throw new \InvalidArgumentException('Security check: Illegal character in filename');
82
        }
83
        if (file_exists($filename)) {
84
            include_once $filename;
85
            return true;
86
        }
87
        return false;
88
    }
89
}
90