Language   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 1
dl 0
loc 88
c 0
b 0
f 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B init() 0 31 6
A __() 0 5 3
A __construct() 0 12 3
1
<?php
2
/**
3
 * Jaeger
4
 *
5
 * @copyright	Copyright (c) 2015-2016, mithra62
6
 * @link		http://jaeger-app.com
7
 * @version		1.0
8
 * @filesource 	./Language.php
9
 */
10
namespace JaegerApp;
11
12
use JaegerApp\Traits\Log;
13
14
/**
15
 * Jaeger - Language Handler Object
16
 *
17
 * Provides basic and generic string replacement via loaded php arrays ONLY.
18
 *
19
 * Shouldn't be used for anything more than keeping generic phrasing out of the code
20
 *
21
 * @package Language
22
 * @author Eric Lamb <[email protected]>
23
 */
24
class Language
25
{
26
    use Log;
27
28
    /**
29
     * Container of the loaded language files
30
     * 
31
     * @var array
32
     */
33
    private $is_loaded = array();
34
35
    /**
36
     * Container for the various languages
37
     * 
38
     * @var unknown
39
     */
40
    private $language = array();
41
42
    /**
43
     * Sets up the Language object
44
     * 
45
     * @param array $paths            
46
     */
47
    public function __construct($paths = array())
48
    {
49
        if (! is_array($paths)) {
50
            $paths = array(
51
                $paths
52
            );
53
        }
54
        
55
        foreach($paths As $path) {
56
            $this->init($path);
57
        };
58
    }
59
60
    /**
61
     * Loads a language array file for use
62
     * 
63
     * @param string $path
64
     *            The path to the language direcotry to load
65
     * @return void|unknown|boolean
66
     */
67
    public function init($path)
68
    {
69
        if (in_array($path, $this->is_loaded, TRUE)) {
70
            return;
71
        }
72
        
73
        $lang = array();
74
        if (is_dir($path)) {
75
            $d = dir($path);
76
            while (false !== ($entry = $d->read())) {
77
                if (! in_array($entry, array(
78
                    '.',
79
                    '..'
80
                ))) {
81
                    $file = $path . '/' . $entry;
82
                    $lang = include ($file);
83
                    
84
                    $this->is_loaded[] = $file;
85
                    
86
                    if (! $lang) {
87
                        $this->logWarning('Language file contains no data: ' . $file);
88
                    }
89
                    
90
                    $this->language = array_merge($this->language, $lang);
0 ignored issues
show
Documentation Bug introduced by
It seems like array_merge($this->language, $lang) of type array is incompatible with the declared type object<JaegerApp\unknown> of property $language.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
91
                }
92
            }
93
        }
94
        
95
        unset($lang);
96
        return TRUE;
97
    }
98
99
    /**
100
     * Fetch a single line of text from the language array
101
     *
102
     * @param string $line
103
     *            line
104
     * @return string
105
     */
106
    public function __($line = '')
107
    {
108
        $line = ($line == '' or ! isset($this->language[$line])) ? $line : $this->language[$line];
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...
109
        return $line;
110
    }
111
}