Test Setup Failed
Push — master ( cf92d1...ffbff4 )
by Joël
02:52
created

Gettext::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 1
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
1
<?php
1 ignored issue
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 16 and the first side effect is on line 3.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
// @codeCoverageIgnoreStart
3
if (!defined('PHPUNIT_TESTING')) defined('BASEPATH') OR exit('No direct script access allowed');
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
4
// @codeCoverageIgnoreEnd
5
6
/**
7
 * Codeigniter PHP framework library class for dealing with gettext.
8
 *
9
 * @package     CodeIgniter
10
 * @subpackage  Libraries
11
 * @category	Language
12
 * @author      Joël Gaujard <[email protected]>
13
 * @author      Marko Martinović <[email protected]>
14
 * @link        https://github.com/joel-depiltech/codeigniter-gettext
15
 */
16
class Gettext
1 ignored issue
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
17
{
18
    /**
19
     * Initialize Codeigniter PHP framework and get configuration
20
     *
21
     * @codeCoverageIgnore
22
     * @param array $config Override default configuration
23
     */
24
    public function __construct($config = array())
25
    {
26
        $CI = &get_instance();
27
28
        // Merge $config and config/gettext.php $config
29
        $config = array_merge(
30
            array(
31
                'gettext_locale_dir' => $CI->config->item('gettext_locale_dir'),
32
                'gettext_text_domain' => $CI->config->item('gettext_text_domain'),
33
                'gettext_catalog_codeset' => $CI->config->item('gettext_catalog_codeset'),
34
                'gettext_locale' => $CI->config->item('gettext_locale')
35
            ),
36
            $config
37
        );
38
39
        self::init($config);
40
    }
41
42
    /**
43
     * Initialize gettext inside Codeigniter PHP framework.
44
     *
45
     * @param array $config configuration
46
     */
47
    static public function init(array $config)
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
48
    {
49
        // Gettext catalog codeset
50
        $IsBindTextDomainCodeset = bind_textdomain_codeset(
51
            $config['gettext_text_domain'],
52
            $config['gettext_catalog_codeset']
53
        );
54
55
        log_message(
56
            'info',
57
            'Try to bind gettext catalog_codeset: ' . $config['gettext_catalog_codeset'] . " - " .
58
                ($IsBindTextDomainCodeset ? 'Successful' : '*** FAILED ***')
59
        );
60
61
        // Path to gettext locales directory relative to FCPATH.APPPATH
62
        $IsBindTextDomain = bindtextdomain(
63
            $config['gettext_text_domain'],
64
            APPPATH . $config['gettext_locale_dir']
65
        );
66
67
        log_message(
68
            'info',
69
            'Try to bind gettext text domain (locale dir): ' . (empty($IsBindTextDomain) ? $IsBindTextDomain : APPPATH . $config['gettext_locale_dir']) . " - " .
70
                (isset($IsBindTextDomain) ? 'Successful' : '*** FAILED ***')
71
        );
72
73
        // Gettext domain
74
        $IsSetTextDomain = textdomain(
75
            $config['gettext_text_domain']
76
        );
77
78
        log_message(
79
            'info',
80
            'Try to set gettext text_domain: ' . $config['gettext_text_domain'] . " - " .
81
                ($IsSetTextDomain ? 'Successful' : '*** FAILED ***')
82
        );
83
84
        // Gettext locale
85
        $IsSetLocale = setlocale(
86
            LC_ALL,
87
            $config['gettext_locale']
88
        );
89
90
        log_message(
91
            'info',
92
            'Try to set gettext locale: ' . (is_array($config['gettext_locale']) ? print_r($config['gettext_locale'], TRUE) : $config['gettext_locale']) . " - " .
93
                ($IsSetLocale ? 'Successful' : '*** FAILED ***')
94
        );
95
        
96
        // Change environment language for CLI
97
        $IsPutEnv = putenv('LANGUAGE=' . (is_array($config['gettext_locale']) ? $config['gettext_locale'][0] : $config['gettext_locale']));
98
        
99
        log_message(
100
            'info',
101
            'Try to set Environment LANGUAGE: ' . (is_array($config['gettext_locale']) ? $config['gettext_locale'][0] : $config['gettext_locale']) . " - " .
102
                ($IsPutEnv ? 'Successful' : '*** FAILED ***')
103
        );
104
    }
105
}
106
107
/* End of file Gettext.php */