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.

Issues (910)

framework/i18n/I18N.php (1 issue)

Labels
Severity
1
<?php
2
/**
3
 * @link https://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license https://www.yiiframework.com/license/
6
 */
7
8
namespace yii\i18n;
9
10
use Yii;
11
use yii\base\Component;
12
use yii\base\InvalidConfigException;
13
14
/**
15
 * I18N provides features related with internationalization (I18N) and localization (L10N).
16
 *
17
 * I18N is configured as an application component in [[\yii\base\Application]] by default.
18
 * You can access that instance via `Yii::$app->i18n`.
19
 *
20
 * @property MessageFormatter $messageFormatter The message formatter to be used to format message via ICU
21
 * message format. Note that the type of this property differs in getter and setter. See
22
 * [[getMessageFormatter()]] and [[setMessageFormatter()]] for details.
23
 *
24
 * @author Qiang Xue <[email protected]>
25
 * @since 2.0
26
 */
27
class I18N extends Component
28
{
29
    /**
30
     * @var array list of [[MessageSource]] configurations or objects. The array keys are message
31
     * category patterns, and the array values are the corresponding [[MessageSource]] objects or the configurations
32
     * for creating the [[MessageSource]] objects.
33
     *
34
     * The message category patterns can contain the wildcard `*` at the end to match multiple categories with the same prefix.
35
     * For example, `app/*` matches both `app/cat1` and `app/cat2`.
36
     *
37
     * The `*` category pattern will match all categories that do not match any other category patterns.
38
     *
39
     * This property may be modified on the fly by extensions who want to have their own message sources
40
     * registered under their own namespaces.
41
     *
42
     * The category `yii` and `app` are always defined. The former refers to the messages used in the Yii core
43
     * framework code, while the latter refers to the default message category for custom application code.
44
     * By default, both of these categories use [[PhpMessageSource]] and the corresponding message files are
45
     * stored under `@yii/messages` and `@app/messages`, respectively.
46
     *
47
     * You may override the configuration of both categories.
48
     */
49
    public $translations;
50
51
52
    /**
53
     * Initializes the component by configuring the default message categories.
54
     */
55 795
    public function init()
56
    {
57 795
        parent::init();
58 795
        if (!isset($this->translations['yii']) && !isset($this->translations['yii*'])) {
59 795
            $this->translations['yii'] = [
60 795
                'class' => 'yii\i18n\PhpMessageSource',
61 795
                'sourceLanguage' => 'en-US',
62 795
                'basePath' => '@yii/messages',
63 795
            ];
64
        }
65
66 795
        if (!isset($this->translations['app']) && !isset($this->translations['app*'])) {
67 795
            $this->translations['app'] = [
68 795
                'class' => 'yii\i18n\PhpMessageSource',
69 795
                'sourceLanguage' => Yii::$app->sourceLanguage,
70 795
                'basePath' => '@app/messages',
71 795
            ];
72
        }
73
    }
74
75
    /**
76
     * Translates a message to the specified language.
77
     *
78
     * After translation the message will be formatted using [[MessageFormatter]] if it contains
79
     * ICU message format and `$params` are not empty.
80
     *
81
     * @param string $category the message category.
82
     * @param string $message the message to be translated.
83
     * @param array $params the parameters that will be used to replace the corresponding placeholders in the message.
84
     * @param string $language the language code (e.g. `en-US`, `en`).
85
     * @return string the translated and formatted message.
86
     */
87 791
    public function translate($category, $message, $params, $language)
88
    {
89 791
        $messageSource = $this->getMessageSource($category);
90 791
        $translation = $messageSource->translate($category, $message, $language);
91 791
        if ($translation === false) {
92 639
            return $this->format($message, $params, $messageSource->sourceLanguage);
93
        }
94
95 174
        return $this->format($translation, $params, $language);
0 ignored issues
show
It seems like $translation can also be of type true; however, parameter $message of yii\i18n\I18N::format() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

95
        return $this->format(/** @scrutinizer ignore-type */ $translation, $params, $language);
Loading history...
96
    }
97
98
    /**
99
     * Formats a message using [[MessageFormatter]].
100
     *
101
     * @param string $message the message to be formatted.
102
     * @param array $params the parameters that will be used to replace the corresponding placeholders in the message.
103
     * @param string $language the language code (e.g. `en-US`, `en`).
104
     * @return string the formatted message.
105
     */
106 795
    public function format($message, $params, $language)
107
    {
108 795
        $params = (array) $params;
109 795
        if ($params === []) {
110 779
            return $message;
111
        }
112
113 211
        if (preg_match('~{\s*[\w.]+\s*,~u', $message)) {
114 129
            $formatter = $this->getMessageFormatter();
115 129
            $result = $formatter->format($message, $params, $language);
116 129
            if ($result === false) {
117
                $errorMessage = $formatter->getErrorMessage();
118
                Yii::warning("Formatting message for language '$language' failed with error: $errorMessage. The message being formatted was: $message.", __METHOD__);
119
120
                return $message;
121
            }
122
123 129
            return $result;
124
        }
125
126 89
        $p = [];
127 89
        foreach ($params as $name => $value) {
128 89
            $p['{' . $name . '}'] = $value;
129
        }
130
131 89
        return strtr($message, $p);
132
    }
133
134
    /**
135
     * @var string|array|MessageFormatter
136
     */
137
    private $_messageFormatter;
138
139
    /**
140
     * Returns the message formatter instance.
141
     * @return MessageFormatter the message formatter to be used to format message via ICU message format.
142
     */
143 129
    public function getMessageFormatter()
144
    {
145 129
        if ($this->_messageFormatter === null) {
146 129
            $this->_messageFormatter = new MessageFormatter();
147 105
        } elseif (is_array($this->_messageFormatter) || is_string($this->_messageFormatter)) {
148
            $this->_messageFormatter = Yii::createObject($this->_messageFormatter);
149
        }
150
151 129
        return $this->_messageFormatter;
152
    }
153
154
    /**
155
     * @param string|array|MessageFormatter $value the message formatter to be used to format message via ICU message format.
156
     * Can be given as array or string configuration that will be given to [[Yii::createObject]] to create an instance
157
     * or a [[MessageFormatter]] instance.
158
     */
159
    public function setMessageFormatter($value)
160
    {
161
        $this->_messageFormatter = $value;
162
    }
163
164
    /**
165
     * Returns the message source for the given category.
166
     * @param string $category the category name.
167
     * @return MessageSource the message source for the given category.
168
     * @throws InvalidConfigException if there is no message source available for the specified category.
169
     */
170 791
    public function getMessageSource($category)
171
    {
172 791
        if (isset($this->translations[$category])) {
173 789
            $source = $this->translations[$category];
174 789
            if ($source instanceof MessageSource) {
175 632
                return $source;
176
            }
177
178 787
            return $this->translations[$category] = Yii::createObject($source);
179
        }
180
        // try wildcard matching
181 4
        foreach ($this->translations as $pattern => $source) {
182 4
            if (strpos($pattern, '*') > 0 && strpos($category, rtrim($pattern, '*')) === 0) {
183
                if ($source instanceof MessageSource) {
184
                    return $source;
185
                }
186
187
                return $this->translations[$category] = $this->translations[$pattern] = Yii::createObject($source);
188
            }
189
        }
190
191
        // match '*' in the last
192 4
        if (isset($this->translations['*'])) {
193 4
            $source = $this->translations['*'];
194 4
            if ($source instanceof MessageSource) {
195 4
                return $source;
196
            }
197
198 2
            return $this->translations[$category] = $this->translations['*'] = Yii::createObject($source);
199
        }
200
201
        throw new InvalidConfigException("Unable to locate message source for category '$category'.");
202
    }
203
}
204