These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * Yii2 module for language switching |
||
4 | * |
||
5 | * @link https://github.com/hiqdev/yii2-language |
||
6 | * @package yii2-language |
||
7 | * @license BSD-3-Clause |
||
8 | * @copyright Copyright (c) 2016, HiQDev (http://hiqdev.com/) |
||
9 | */ |
||
10 | |||
11 | namespace hiqdev\yii2\language\menus; |
||
12 | |||
13 | use codemix\localeurls\UrlManager; |
||
14 | use Yii; |
||
15 | use yii\helpers\Url; |
||
16 | |||
17 | /** |
||
18 | * Language Menu. |
||
19 | * |
||
20 | * @property Module $module The module to be used, can be found by default. |
||
21 | */ |
||
22 | class LanguageMenu extends \yii\base\Widget |
||
23 | { |
||
24 | use \hiqdev\yii2\language\GetModuleTrait; |
||
25 | |||
26 | public function getLanguages() |
||
27 | { |
||
28 | return $this->getModule()->languages; |
||
29 | } |
||
30 | |||
31 | public function getLanguage() |
||
32 | { |
||
33 | return Yii::$app->language; |
||
34 | } |
||
35 | |||
36 | public function items() |
||
37 | { |
||
38 | $url = $this->getSelectUrl(); |
||
39 | $language = $this->getLanguage(); |
||
40 | $items = []; |
||
41 | foreach ($this->getLanguages() as $code => $lang) { |
||
42 | $items[$code] = [ |
||
43 | 'label' => $lang, |
||
44 | 'url' => Url::to(array_merge($url, ['language' => $code])), |
||
45 | 'active' => mb_stristr($language, $code), |
||
46 | ]; |
||
47 | } |
||
48 | |||
49 | return $items; |
||
50 | } |
||
51 | |||
52 | View Code Duplication | public function getSelectUrl() |
|
0 ignored issues
–
show
|
|||
53 | { |
||
54 | if (Yii::$app->urlManager instanceof UrlManager) { |
||
55 | $route = Yii::$app->controller->route; |
||
56 | $params = Yii::$app->request->get(); |
||
57 | array_unshift($params, '/' . $route); |
||
58 | |||
59 | return $params; |
||
60 | } |
||
61 | |||
62 | return ['/' . $this->getModule()->id . '/language/select']; |
||
63 | } |
||
64 | } |
||
65 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.