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.

I18nController   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 134
Duplicated Lines 10.45 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 8
dl 14
loc 134
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B getAliases() 0 27 8
A behaviors() 14 14 1
A actionIndex() 0 15 1
C actionUpdate() 0 66 11

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace app\backend\controllers;
4
5
use Yii;
6
use yii\data\ArrayDataProvider;
7
use yii\filters\AccessControl;
8
use yii\helpers\Json;
9
use yii\i18n\PhpMessageSource;
10
use yii\web\Controller;
11
use yii\web\Cookie;
12
use yii\web\NotFoundHttpException;
13
14
class I18nController extends Controller
15
{
16
    private $aliases;
17
18
    /**
19
     * @return array
20
     * @throws \yii\base\InvalidConfigException
21
     */
22
    private function getAliases()
23
    {
24
        if ($this->aliases === null) {
25
            $this->aliases = [];
26
            foreach (Yii::$app->i18n->translations as $name => $translation) {
27
                if (is_array($translation)) {
28
                    $translation = Yii::createObject($translation);
29
                }
30
                if (!($translation instanceof PhpMessageSource) || $name == 'yii') {
31
                    continue;
32
                }
33
                $basePath = Yii::getAlias($translation->basePath);
34
                $rdi = new \RecursiveDirectoryIterator(
35
                    $basePath,
36
                    \RecursiveDirectoryIterator::SKIP_DOTS
37
                );
38
                foreach (new \RecursiveIteratorIterator($rdi, \RecursiveIteratorIterator::CHILD_FIRST) as $file) {
39
                    $fileName = $file->getRealpath();
40
                    if (pathinfo($fileName, PATHINFO_EXTENSION) == 'php') {
41
                        $alias = $translation->basePath . substr($fileName, strlen($basePath));
42
                        $this->aliases[$alias] = $fileName;
43
                    }
44
                }
45
            }
46
        }
47
        return $this->aliases;
48
    }
49
50 View Code Duplication
    public function behaviors()
51
    {
52
        return [
53
            'access' => [
54
                'class' => AccessControl::className(),
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
55
                'rules' => [
56
                    [
57
                        'allow' => true,
58
                        'roles' => ['setting manage'],
59
                    ],
60
                ],
61
            ],
62
        ];
63
    }
64
65
    public function actionIndex()
66
    {
67
        $aliases = $this->getAliases();
68
        $dataProvider = new ArrayDataProvider(
69
            [
70
                'allModels' => $aliases,
71
            ]
72
        );
73
        return $this->render(
74
            'index',
75
            [
76
                'dataProvider' => $dataProvider,
77
            ]
78
        );
79
    }
80
81
    public function actionUpdate($id)
82
    {
83
        $aliases = $this->getAliases();
84
        if (!isset($aliases[$id])) {
85
            throw new NotFoundHttpException;
86
        }
87
        if (!is_writable($aliases[$id])) {
88
            Yii::$app->session->setFlash(
89
                'warning',
90
                Yii::t(
91
                    'app',
92
                    'File "{file}" is not writable',
93
                    ['file' => $aliases[$id]]
94
                )
95
            );
96
        }
97
        if (Yii::$app->request->isPost && !is_null(Yii::$app->request->post('messages'))) {
98
            Yii::$app->response->cookies->add(
99
                new Cookie(
100
                    [
101
                        'name' => 'sortMessages',
102
                        'value' => Yii::$app->request->post('ksort') == 1,
103
                    ]
104
                )
105
            );
106
            $messages = Yii::$app->request->post('messages');
107
            $data = Json::decode($messages);
108
            $hasErrors = false;
109
            foreach ((array) $data as $message => $translation) {
110
                if (!is_string($translation)) {
111
                    $hasErrors = true;
112
                    break;
113
                }
114
            }
115
            if (!$hasErrors) {
116
                try {
117
                    if (Yii::$app->request->post('ksort') == 1) {
118
                        ksort($data, SORT_NATURAL | SORT_FLAG_CASE);
119
                    }
120
                    file_put_contents($aliases[$id], "<?php\n\nreturn " . var_export($data, true) . ";\n");
121
                    Yii::$app->session->setFlash('success', Yii::t('app', 'Messages has been saved'));
122
                    $this->refresh();
123
                    Yii::$app->end();
124
                } catch (\Exception $e) {
125
                    Yii::$app->session->setFlash('error', Yii::t('app', 'Cannot save messages'));
126
                }
127
            } else {
128
                Yii::$app->session->setFlash('error', Yii::t('app', 'Wrong data'));
129
            }
130
        } else {
131
            try {
132
                $messages = Json::encode(include $aliases[$id]);
133
            } catch (\Exception $e) {
134
                $messages = '{}';
135
                Yii::$app->session->setFlash('error', Yii::t('app', 'Cannot read messages'));
136
            }
137
        }
138
        return $this->render(
139
            'update',
140
            [
141
                'alias' => $id,
142
                'file' => $aliases[$id],
143
                'messages' => $messages,
144
            ]
145
        );
146
    }
147
}
148