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.

ViewController   A
last analyzed

Complexity

Total Complexity 29

Size/Duplication

Total Lines 178
Duplicated Lines 15.73 %

Coupling/Cohesion

Components 2
Dependencies 9

Importance

Changes 0
Metric Value
wmc 29
lcom 2
cbo 9
dl 28
loc 178
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
B getTree() 0 36 8
A behaviors() 14 14 1
A actions() 0 14 1
A actionIndex() 0 11 1
A actionAdd() 0 24 5
A actionEdit() 0 20 5
A actionDelete() 14 14 4
A actionRemoveAll() 0 12 3
A actionGetViews() 0 5 1

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 app\backend\traits\BackendRedirect;
6
use app\models\View;
7
use Yii;
8
use yii\filters\AccessControl;
9
use yii\helpers\Url;
10
use yii\web\Controller;
11
use yii\web\NotFoundHttpException;
12
use yii\web\Response;
13
14
class ViewController extends Controller
15
{
16
    use BackendRedirect;
17
18
    protected function getTree($path = '', $level = 0)
19
    {
20
        if (is_null($this->view->theme) || !file_exists($this->view->theme->getBaseUrl())) {
21
            return [];
22
        }
23
        $result = [];
24
        $basePath = $this->view->theme->getBaseUrl();
25
        $dir = new \DirectoryIterator($basePath . $path);
26
        /** @var \DirectoryIterator $file */
27
        foreach ($dir as $file) {
28
            if ($file->isDot()) {
29
                continue;
30
            }
31
            $id = '#' . preg_replace('#[^\w\d]#', '_', $file->getFilename()) . "_lev{$level}";
32
            if ($file->isDir()) {
33
                $result[] = [
34
                    'id' => $id,
35
                    'children' => $this->getTree($path . DIRECTORY_SEPARATOR . $file->getBasename(), $level + 1),
36
                    'text' => $file->getBasename(),
37
                    'type' => 'dir',
38
                ];
39
            } elseif ($file->isFile() && 'php' === $file->getExtension()) {
40
                $result[] = [
41
                    'id' => $id,
42
                    'text' => $file->getBasename(),
43
                    'a_attr' => [
44
                        'data-file' => '@webroot/theme/views'.str_replace($basePath, '', $file->getRealPath()),
45
                        'data-toggle' => 'tooltip',
46
                        'title' => $file->getBasename()
47
                    ],
48
                    'type' => 'file',
49
                ];
50
            }
51
        }
52
        return $result;
53
    }
54
55
    /**
56
     * @inheritdoc
57
     */
58 View Code Duplication
    public function behaviors()
59
    {
60
        return [
61
            'access' => [
62
                '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...
63
                'rules' => [
64
                    [
65
                        'allow' => true,
66
                        'roles' => ['view manage'],
67
                    ],
68
                ],
69
            ],
70
        ];
71
    }
72
73
    /**
74
     * @inheritdoc
75
     */
76
    public function actions()
77
    {
78
        return [
79
            'error' => [
80
                'class' => 'yii\web\ErrorAction',
81
            ],
82
            'autocomplete' => [
83
                'class' => 'app\backend\actions\AutocompleteAction',
84
                'modelName' => 'app\models\View',
85
                'json_attributes' => ['name', 'id', 'category', 'internal_name',],
86
                'search_attributes' => ['name', 'category', 'internal_name'],
87
            ],
88
        ];
89
    }
90
91
    /*
92
     *
93
     */
94
    public function actionIndex()
95
    {
96
        $model = new View();
97
        return $this->render(
98
            'index',
99
            [
100
                'searchModel' => $model,
101
                'dataProvider' => $model->search(Yii::$app->request->get()),
102
            ]
103
        );
104
    }
105
106
    /*
107
     *
108
     */
109
    public function actionAdd($id = null)
110
    {
111
        $model = new View();
112
        if (null !== $id) {
113
            $id = intval($id);
114
            if (null !== View::findOne(['id' => $id])) {
115
                return $this->redirect(Url::toRoute(['edit', 'id' => $id]));
116
            }
117
            $model->id = $id;
118
        }
119
120
        if ($model->load(\Yii::$app->request->post())) {
121
            if ($model->save()) {
122
                return $this->redirectUser($model->id, true, 'edit');
123
            }
124
        }
125
126
        return $this->render(
127
            'edit',
128
            [
129
                'model' => $model
130
            ]
131
        );
132
    }
133
134
    /*
135
     *
136
     */
137
    public function actionEdit($id = null)
138
    {
139
        if ((null === $id) || (null === $model = View::findOne(['id' => $id]))) {
140
            return $this->redirect(Url::toRoute(['add', 'id' => $id]));
141
        }
142
143
        /** @var View $model */
144
        if ($model->load(\Yii::$app->request->post())) {
145
            if ($model->save()) {
146
                return $this->redirectUser($model->id);
147
            }
148
        }
149
150
        return $this->render(
151
            'edit',
152
            [
153
                'model' => $model
154
            ]
155
        );
156
    }
157
158 View Code Duplication
    public function actionDelete($id = null)
159
    {
160
        if ((null === $id) || (null === $model = View::findOne($id))) {
161
            throw new NotFoundHttpException;
162
        }
163
164
        if (!$model->delete()) {
165
            Yii::$app->session->setFlash('error', Yii::t('app', 'Object not removed'));
166
        } else {
167
            Yii::$app->session->setFlash('info', Yii::t('app', 'Object removed'));
168
        }
169
170
        return $this->redirect(Url::toRoute('index'));
171
    }
172
173
    public function actionRemoveAll()
174
    {
175
        $items = Yii::$app->request->post('items', []);
176
        if (!empty($items)) {
177
            $items = View::find()->where(['in', 'id', $items])->all();
178
            foreach ($items as $item) {
179
                $item->delete();
180
            }
181
        }
182
183
        return $this->redirect(['index']);
184
    }
185
186
    public function actionGetViews()
187
    {
188
        Yii::$app->response->format = Response::FORMAT_JSON;
189
        return $this->getTree('', 0);
190
    }
191
}
192