Completed
Push — master ( 7f45a1...6afdf1 )
by Andrey
01:18
created

Module::t()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 4
1
<?php
2
3
namespace Itstructure\AdminModule;
4
5
use Yii;
6
use yii\helpers\ArrayHelper;
7
use yii\base\Module as BaseModule;
8
use Itstructure\AdminModule\components\AdminView;
9
10
/**
11
 * Admin module class.
12
 *
13
 * @property string $layout Main layout for other child templates.
14
 * @property null|string|array $loginUrl Login url.
15
 * @property bool $isMultilanguage Set multilanguage mode.
16
 * @property array $accessRoles Array of roles to module access.
17
 * @property AdminView $_view View component to render content.
18
 *
19
 * @package Itstructure\AdminModule
20
 *
21
 * @author Andrey Girnik <[email protected]>
22
 */
23
class Module extends BaseModule
24
{
25
    /**
26
     * Main layout for other child templates.
27
     *
28
     * @var string
29
     */
30
    public $layout = '@admin/views/layouts/main-admin.php';
31
32
    /**
33
     * Login url.
34
     *
35
     * @var null|string|array
36
     */
37
    public $loginUrl = null;
38
39
    /**
40
     * Set multilanguage mode.
41
     *
42
     * @var bool
43
     */
44
    public $isMultilanguage = false;
45
46
    /**
47
     * Array of roles to module access.
48
     *
49
     * @var array
50
     */
51
    public $accessRoles = ['@'];
52
53
    /**
54
     * View component to render content.
55
     *
56
     * @var AdminView
57
     */
58
    private $_view = null;
59
60
    /**
61
     * Module translations.
62
     *
63
     * @var array|null
64
     */
65
    private static $_translations = null;
66
67
    /**
68
     * @inheritdoc
69
     */
70
    public function init()
71
    {
72
        parent::init();
73
74
        Yii::setAlias('@admin', static::getBaseDir());
75
76
        if (null !== $this->loginUrl && method_exists(Yii::$app, 'getUser')) {
77
            Yii::$app->getUser()->loginUrl = $this->loginUrl;
0 ignored issues
show
Bug introduced by
The method getUser does only exist in yii\web\Application, but not in yii\console\Application.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
78
        }
79
80
        self::registerTranslations();
81
82
        /**
83
         * Set Profile validate component
84
         */
85
        $this->setComponents(
86
            ArrayHelper::merge(
87
                require __DIR__ . '/config/view.php',
88
                $this->components
89
            )
90
        );
91
    }
92
93
    /**
94
     * Get the view.
95
     *
96
     * @return AdminView
97
     */
98
    public function getView()
99
    {
100
        if (null === $this->_view) {
101
            $this->_view = $this->get('view');
102
        }
103
104
        return $this->_view;
105
    }
106
107
    /**
108
     * Returns module root directory.
109
     *
110
     * @return string
111
     */
112
    public static function getBaseDir(): string
113
    {
114
        return __DIR__;
115
    }
116
117
    /**
118
     * Module translator.
119
     *
120
     * @param       $category
121
     * @param       $message
122
     * @param array $params
123
     * @param null  $language
124
     *
125
     * @return string
126
     */
127
    public static function t($category, $message, $params = [], $language = null)
128
    {
129
        if (null === self::$_translations) {
130
            self::registerTranslations();
131
        }
132
133
        return Yii::t('modules/admin/' . $category, $message, $params, $language);
134
    }
135
136
    /**
137
     * Set i18N component.
138
     *
139
     * @return void
140
     */
141
    private static function registerTranslations(): void
142
    {
143
        self::$_translations = [
144
            'modules/admin/*' => [
145
                'class'          => 'yii\i18n\PhpMessageSource',
146
                'forceTranslation' => true,
147
                'sourceLanguage' => Yii::$app->language,
148
                'basePath'       => '@admin/messages',
149
                'fileMap'        => [
150
                    'modules/admin/main' => 'main.php',
151
                    'modules/admin/languages' => 'languages.php',
152
                    'modules/admin/admin-menu' => 'admin-menu.php',
153
                ],
154
            ]
155
        ];
156
157
        Yii::$app->i18n->translations = ArrayHelper::merge(
158
            self::$_translations,
159
            Yii::$app->i18n->translations
160
        );
161
    }
162
}
163