DefaultController   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 79
Duplicated Lines 29.11 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 4
c 2
b 0
f 0
lcom 1
cbo 4
dl 23
loc 79
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A actionIndex() 0 4 1
A behaviors() 23 23 1
A actions() 0 8 1
A actionViewConfig() 0 20 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\modules\backend\controllers;
4
5
use Yii;
6
use yii\filters\AccessControl;
7
use yii\web\Controller;
8
9
/**
10
 * Default backend controller.
11
 *
12
 * Usually renders a customized dashboard for logged in users
13
 */
14
class DefaultController extends Controller
15
{
16
    /**
17
     * {@inheritdoc}
18
     */
19 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...
20
    {
21
        return [
22
            'access' => [
23
                'class' => AccessControl::className(),
24
                'rules' => [
25
                    [
26
                        'allow' => true,
27
                        'actions' => ['error'],
28
                    ],
29
                    [
30
                        'allow' => true,
31
                        'matchCallback' => function ($rule, $action) {
32
                            return \Yii::$app->user->can(
33
                                $this->module->id.'_'.$this->id.'_'.$action->id,
34
                                ['route' => true]
35
                            );
36
                        },
37
                    ],
38
                ],
39
            ],
40
        ];
41
    }
42
43
    /**
44
     * Actions defined in classes, eg. error page.
45
     *
46
     * @return array
47
     */
48
    public function actions()
49
    {
50
        return [
51
            'error' => [
52
                'class' => 'yii\web\ErrorAction',
53
            ],
54
        ];
55
    }
56
57
    /**
58
     * Application dashboard.
59
     *
60
     * @return string
61
     */
62
    public function actionIndex()
63
    {
64
        return $this->render('index');
65
    }
66
67
    /**
68
     * Application configuration.
69
     *
70
     * @return string
71
     */
72
    public function actionViewConfig()
0 ignored issues
show
Coding Style introduced by
actionViewConfig uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
73
    {
74
        $config = $GLOBALS['config'];
75
        $modules = $config['modules'];
76
        unset($config['modules']);
77
        $components = $config['components'];
78
        unset($config['components']);
79
        $params = $config['params'];
80
        unset($config['params']);
81
82
        return $this->render(
83
            'view-config',
84
            [
85
                'config' => $config,
86
                'modules' => $modules,
87
                'components' => $components,
88
                'params' => $params,
89
            ]
90
        );
91
    }
92
}
93