Completed
Push — master ( 36242a...d93503 )
by zacksleo
05:54
created

MyController   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 74
Duplicated Lines 32.43 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 24
loc 74
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A actionAvatar() 12 12 3
A actionChangePassword() 0 12 4
A actionProfile() 12 12 3

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 zacksleo\yii2\backend\controllers;
4
5
use zacksleo\yii2\backend\models\forms\ChangePasswordForm;
6
use yii;
7
use zacksleo\yii2\backend\models\Admin;
8
use yii\web\Controller;
9
use yii\filters\AccessControl;
10
11
class MyController extends Controller
12
{
13
    public $layout = '@vendor/zacksleo/yii2-backend/src/views/my/layouts';
14
15
    public function behaviors()
16
    {
17
        return [
18
            'access' => [
19
                'class' => AccessControl::className(),
20
                'rules' => [
21
                    [
22
                        'allow' => true,
23
                        'actions' => ['profile', 'change-password', 'avatar'],
24
                        'roles' => ['@'],
25
                    ]
26
                ],
27
            ]
28
        ];
29
    }
30
31
    /**
32
     * 设置头像
33
     * @return string|yii\web\Response
34
     */
35 View Code Duplication
    public function actionAvatar()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
36
    {
37
        $model = Admin::findOne(Yii::$app->user->id);
38
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
39
            Yii::$app->session->setFlash('success', '修改成功');
40
            return $this->redirect(['avatar']);
41
        } else {
42
            return $this->render('avatar', [
43
                'model' => $model
44
            ]);
45
        }
46
    }
47
48
    /**
49
     * 修改密码
50
     * @return string|yii\web\Response
51
     */
52
    public function actionChangePassword()
53
    {
54
        $model = new ChangePasswordForm();
55
        if ($model->load(Yii::$app->request->post()) && $model->validate() && $model->resetPassword()) {
56
            Yii::$app->session->setFlash('success', '修改成功');
57
            return $this->redirect(['change-password']);
58
        } else {
59
            return $this->render('change-password', [
60
                'model' => $model
61
            ]);
62
        }
63
    }
64
65
    /**
66
     * 设置个人信息
67
     * @return string|yii\web\Response
68
     */
69 View Code Duplication
    public function actionProfile()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
70
    {
71
        $model = Admin::findOne(Yii::$app->user->id);
72
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
73
            Yii::$app->session->setFlash('success', '修改成功');
74
            return $this->redirect(['profile']);
75
        } else {
76
            return $this->render('profile', [
77
                'model' => $model
78
            ]);
79
        }
80
    }
81
}
82