ContentTypeController   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 76
Duplicated Lines 35.53 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 4
dl 27
loc 76
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A actionIndex() 10 10 1
A actionToggle() 9 9 1
A findModel() 8 8 2
A behaviors() 0 18 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\controllers;
4
5
use Yii;
6
use app\models\ContentType;
7
use yii\data\ActiveDataProvider;
8
use yii\web\NotFoundHttpException;
9
use yii\filters\AccessControl;
10
use yii\filters\VerbFilter;
11
12
/**
13
 * ContentController implements the CRUD actions for ContentType model.
14
 */
15
class ContentTypeController extends BaseController
16
{
17
    /**
18
     * {@inheritdoc}
19
     */
20
    public function behaviors()
21
    {
22
        return [
23
            'verbs' => [
24
                'class' => VerbFilter::class,
25
                'actions' => [
26
                    'delete' => ['POST'],
27
                ],
28
            ],
29
            'access' => [
30
                'class' => AccessControl::class,
31
                'only' => ['index'],
32
                'rules' => [
33
                    ['allow' => true, 'actions' => ['index'], 'roles' => ['setContentTypes']],
34
                ],
35
            ],
36
        ];
37
    }
38
39
    /**
40
     * Lists all ContentType models.
41
     *
42
     * @return string
43
     */
44 View Code Duplication
    public function actionIndex()
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...
45
    {
46
        $dataProvider = new ActiveDataProvider([
47
            'query' => ContentType::find(),
48
        ]);
49
50
        return $this->render('index', [
51
            'dataProvider' => $dataProvider,
52
        ]);
53
    }
54
55
    /**
56
     * Enables or disables a Device.
57
     *
58
     * @param int $id screen id
59
     *
60
     * @return \yii\web\Response
61
     */
62 View Code Duplication
    public function actionToggle($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...
63
    {
64
        $model = $this->findModel($id);
65
66
        $model->enabled = !$model->enabled;
67
        $model->save();
68
69
        return $this->smartGoBack();
70
    }
71
72
    /**
73
     * Finds the ContentType model based on its primary key value.
74
     * If the model is not found, a 404 HTTP exception will be thrown.
75
     *
76
     * @param int $id
77
     *
78
     * @return ContentType the loaded model
79
     *
80
     * @throws NotFoundHttpException if the model cannot be found
81
     */
82 View Code Duplication
    protected function findModel($id)
83
    {
84
        if (($model = ContentType::findOne($id)) !== null) {
0 ignored issues
show
Bug Compatibility introduced by
The expression \app\models\ContentType::findOne($id); of type yii\db\ActiveRecordInterface|array|null adds the type array to the return on line 85 which is incompatible with the return type documented by app\controllers\ContentTypeController::findModel of type app\models\ContentType.
Loading history...
85
            return $model;
86
        } else {
87
            throw new NotFoundHttpException(Yii::t('app', 'The requested content does not exist.'));
88
        }
89
    }
90
}
91