GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Translation::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 2
b 0
f 0
nc 1
nop 1
dl 0
loc 8
ccs 7
cts 7
cp 1
crap 1
rs 9.4285
1
<?php
2
/**
3
 * Translation.php
4
 *
5
 * @author  Michael Pratt <[email protected]>
6
 * @link    http://www.michael-pratt.com/
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 */
12
13
namespace RelativeTime;
14
15
use \RelativeTime\Languages\English;
16
use \RelativeTime\Languages\Spanish;
17
use \RelativeTime\Languages\German;
18
use \RelativeTime\Languages\PortugueseBR;
19
use \ArrayAccess;
20
21
/**
22
 * This class is responsible for translating
23
 * an array with time units into a string of
24
 * a given language
25
 */
26
class Translation
27
{
28
    /** @var array Array With configuration options **/
29
    protected $config = array();
30
31
    /**
32
     * Construct
33
     *
34
     * @param array $config Associative array with configuration directives
35
     *
36
     */
37 22
    public function __construct(array $config = array())
38
    {
39 22
        $this->config = array_merge(array(
40 22
            'language' => new English(),
41 22
            'separator' => ', ',
42 22
            'suffix' => true,
43 22
        ), $config);
44 22
    }
45
46
    /**
47
     * Actually translates the units into words
48
     *
49
     * @param array $units
50
     * @param int $direction
51
     * @return string
52
     */
53 20
    public function translate(array $units = array(), $direction = 0)
54
    {
55 20
        $lang = $this->loadLanguage();
56 20
        if (empty($units)) {
57 8
            return $lang['now'];
58
        }
59
60 18
        $translation = array();
61 18
        foreach ($units as $unit => $v) {
62 18
            if ($v == 1) {
63 8
                $translation[] = sprintf($lang[$unit]['singular'], $v);
64 8
            } else {
65 18
                $translation[] = sprintf($lang[$unit]['plural'], $v);
66
            }
67 18
        }
68
69 18
        $string = implode($this->config['separator'], $translation);
70 18
        if (!$this->config['suffix']) {
71 4
            return $string;
72
        }
73
74 14
        if ($direction > 0) {
75 10
            return sprintf($lang['ago'], $string);
76
        } else {
77 8
            return sprintf($lang['left'], $string);
78
        }
79
    }
80
81
    /**
82
     * Loads the language definitions
83
     *
84
     * @return ArrayAccess
85
     */
86 20
    protected function LoadLanguage()
87
    {
88 20
        if (is_object($this->config['language'])) {
89 20
            $l = $this->config['language'];
90 20
        } else {
91
            $languages = array(
92 2
                $this->config['language'],
93 2
                '\RelativeTime\Languages\\' . $this->config['language'],
94 2
            );
95
96 2
            $l = null;
97 2
            foreach ($languages as $lang) {
98 2
                if (class_exists($lang)) {
99 2
                    $l = new $lang();
100 2
                    break;
101
                }
102 2
            }
103
        }
104
105 20
        if ($l instanceof ArrayAccess) {
106 20
            return $l;
107
        }
108
109 2
        return new English();
110
    }
111
}
112
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
113