Completed
Push — master ( d95bde...2ad848 )
by Markus
07:09
created

SimpleTranslator::translate()   B

Complexity

Conditions 7
Paths 10

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 13
nc 10
nop 3
dl 0
loc 21
rs 7.551
c 0
b 0
f 0
1
<?php
2
namespace Agavi\Translation;
3
4
// +---------------------------------------------------------------------------+
5
// | This file is part of the Agavi package.                                   |
6
// | Copyright (c) 2005-2011 the Agavi Project.                                |
7
// |                                                                           |
8
// | For the full copyright and license information, please view the LICENSE   |
9
// | file that was distributed with this source code. You can also view the    |
10
// | LICENSE file online at http://www.agavi.org/LICENSE.txt                   |
11
// |   vi: set noexpandtab:                                                    |
12
// |   Local Variables:                                                        |
13
// |   indent-tabs-mode: t                                                     |
14
// |   End:                                                                    |
15
// +---------------------------------------------------------------------------+
16
use Agavi\Core\Context;
17
use Agavi\Exception\AgaviException;
18
use Agavi\Util\Toolkit;
19
20
/**
21
 * SimpleTranslator defines the translator which loads the data from its
22
 * parameters.
23
 *
24
 * @package    agavi
25
 * @subpackage translation
26
 *
27
 * @author     Dominik del Bondio <[email protected]>
28
 * @copyright  Authors
29
 * @copyright  The Agavi Project
30
 *
31
 * @since      0.11.0
32
 *
33
 * @version    $Id$
34
 */
35
class SimpleTranslator extends BasicTranslator
36
{
37
    /**
38
     * @var        array The data for each domain
39
     */
40
    protected $domainData = array();
41
42
    /**
43
     * @var        array The data for the currently active locale
44
     */
45
    protected $currentData = array();
46
47
    /**
48
     * @var        Locale The currently set locale
49
     */
50
    protected $locale = null;
51
52
    /**
53
     * Initialize this Translator.
54
     *
55
     * @param      Context $context An Context instance.
56
     * @param      array   $parameters An associative array of initialization parameters.
57
     *
58
     * @author     Dominik del Bondio <[email protected]>
59
     * @since      0.11.0
60
     */
61
    public function initialize(Context $context, array $parameters = array())
62
    {
63
        parent::initialize($context);
64
65
        $domainData = array();
66
67
        foreach ((array)$parameters as $domain => $locales) {
68
            foreach ((array)$locales as $locale => $translations) {
69
                foreach ((array)$translations as $key => $translation) {
70
                    if (is_array($translation)) {
71
                        $domainData[$locale][$domain][$translation['from']] = $translation['to'];
72
                    } else {
73
                        $domainData[$locale][$domain][$key] = $translation;
74
                    }
75
                }
76
            }
77
        }
78
79
        $this->domainData = $domainData;
80
    }
81
82
    /**
83
     * Translates a message into the defined language.
84
     *
85
     * @param      mixed       $message The message to be translated.
86
     * @param      string      $domain The domain of the message.
87
     * @param      Locale      $locale The locale to which the message should be
88
     *                         translated.
89
     *
90
     * @return     string The translated message.
91
     *
92
     * @author     Dominik del Bondio <[email protected]>
93
     * @since      0.11.0
94
     */
95
    public function translate($message, $domain, Locale $locale = null)
96
    {
97
        if ($locale && $locale !== $this->locale) {
98
            $oldCurrentData = $this->currentData;
99
            $oldLocale = $this->locale;
100
            $this->localeChanged($locale);
101
        }
102
103
        if (is_array($message)) {
104
            throw new AgaviException('The simple translator doesn\'t support pluralized input');
105
        } else {
106
            $data = isset($this->currentData[(string)$domain][$message]) ? $this->currentData[(string)$domain][$message] : $message;
107
        }
108
109
        if ($locale && $locale !== $this->locale) {
110
            $this->currentData = $oldCurrentData;
0 ignored issues
show
Bug introduced by
The variable $oldCurrentData does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
111
            $this->locale = $oldLocale;
0 ignored issues
show
Bug introduced by
The variable $oldLocale does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
112
        }
113
114
        return $data;
115
    }
116
117
    /**
118
     * This method gets called by the translation manager when the default locale
119
     * has been changed.
120
     *
121
     * @param      Locale $newLocale The new default locale.
122
     *
123
     * @author     David Zülke <[email protected]>
124
     * @since      0.11.0
125
     */
126
    public function localeChanged($newLocale)
127
    {
128
        $this->locale = $newLocale;
129
        $this->currentData = Toolkit::getValueByKeyList($this->domainData, Locale::getLookupPath($this->locale->getIdentifier()), array());
0 ignored issues
show
Documentation Bug introduced by
It seems like \Agavi\Util\Toolkit::get...Identifier()), array()) of type * is incompatible with the declared type array of property $currentData.

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...
130
    }
131
}
132