AdminController   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 98
Duplicated Lines 28.57 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 28
loc 98
rs 10
c 0
b 0
f 0
ccs 0
cts 53
cp 0
wmc 10
lcom 1
cbo 7

7 Methods

Rating   Name   Duplication   Size   Complexity  
A actionIndex() 0 6 1
A actionView() 0 4 1
A actionCreate() 0 6 2
A actionUpdate() 0 5 2
A actionDelete() 0 4 1
A behaviors() 20 20 1
A findModel() 8 8 2

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\tests\controllers;
4
5
use Yii;
6
use zacksleo\yii2\backend\models\Admin;
7
use yii\data\ActiveDataProvider;
8
use yii\web\Controller;
9
use yii\web\NotFoundHttpException;
10
use yii\filters\VerbFilter;
11
use yii\filters\AccessControl;
12
13
/**
14
 * AdminController implements the CRUD actions for Admin model.
15
 */
16
class AdminController extends Controller
17
{
18
    /**
19
     * @inheritdoc
20
     */
21 View Code Duplication
    public function behaviors()
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...
22
    {
23
        return [
24
            'access' => [
25
                '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...
26
                'rules' => [
27
                    [
28
                        'allow' => true,
29
                        'roles' => ['@'],
30
                    ]
31
                ]
32
            ],
33
            'verbs' => [
34
                'class' => VerbFilter::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...
35
                'actions' => [
36
                    'delete' => ['GET'],
37
                ],
38
            ],
39
        ];
40
    }
41
42
    /**
43
     * Lists all Admin models.
44
     * @return yii\data\ActiveDataProvider
45
     */
46
    public function actionIndex()
47
    {
48
        return $dataProvider = new ActiveDataProvider([
0 ignored issues
show
Unused Code introduced by
$dataProvider is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
49
            'query' => Admin::find(),
50
        ]);
51
    }
52
53
    /**
54
     * Displays a single Admin model.
55
     * @param integer $id
56
     * @return mixed
57
     */
58
    public function actionView($id)
59
    {
60
        return $this->findModel($id);
61
    }
62
63
    /**
64
     * Creates a new Admin model.
65
     * If creation is successful, the browser will be redirected to the 'view' page.
66
     * @return mixed
67
     */
68
    public function actionCreate()
69
    {
70
        $model = new Admin();
71
        $model->load(Yii::$app->request->post()) && $model->save();
72
        return $model;
73
    }
74
75
    /**
76
     * Updates an existing Admin model.
77
     * If update is successful, the browser will be redirected to the 'view' page.
78
     * @param integer $id
79
     * @return mixed
80
     */
81
    public function actionUpdate($id)
82
    {
83
        $model = $this->findModel($id);
84
        return $model->load(Yii::$app->request->post()) && $model->save();
85
    }
86
87
    /**
88
     * Deletes an existing Admin model.
89
     * If deletion is successful, the browser will be redirected to the 'index' page.
90
     * @param integer $id
91
     * @return mixed
92
     */
93
    public function actionDelete($id)
94
    {
95
        return $this->findModel($id)->delete();
96
    }
97
98
    /**
99
     * Finds the Admin model based on its primary key value.
100
     * If the model is not found, a 404 HTTP exception will be thrown.
101
     * @param integer $id
102
     * @return Admin the loaded model
103
     * @throws NotFoundHttpException if the model cannot be found
104
     */
105 View Code Duplication
    protected function findModel($id)
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...
106
    {
107
        if (($model = Admin::findOne($id)) !== null) {
0 ignored issues
show
Bug Compatibility introduced by
The expression \zacksleo\yii2\backend\models\Admin::findOne($id); of type yii\db\ActiveRecordInterface|array|null adds the type array to the return on line 108 which is incompatible with the return type documented by zacksleo\yii2\backend\te...inController::findModel of type zacksleo\yii2\backend\models\Admin.
Loading history...
108
            return $model;
109
        } else {
110
            throw new NotFoundHttpException('The requested page does not exist.');
111
        }
112
    }
113
}
114