Completed
Branch develop (740555)
by Tobias
03:44
created

DefaultController   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 83
Duplicated Lines 27.71 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A behaviors() 23 23 1
A actions() 0 8 1
A actionIndex() 0 4 1
B actionViewConfig() 0 24 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 dmstr\helpers\Metadata;
6
use Yii;
7
use yii\data\ArrayDataProvider;
8
use yii\filters\AccessControl;
9
use yii\web\Controller;
10
11
/**
12
 * Default backend controller.
13
 *
14
 * Usually renders a customized dashboard for logged in users
15
 */
16
class DefaultController 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(),
26
                'rules' => [
27
                    [
28
                        'allow' => true,
29
                        'actions' => ['error'],
30
                    ],
31
                    [
32
                        'allow' => true,
33
                        'matchCallback' => function ($rule, $action) {
34
                            return \Yii::$app->user->can(
35
                                $this->module->id.'_'.$this->id.'_'.$action->id,
36
                                ['route' => true]
37
                            );
38
                        },
39
                    ],
40
                ],
41
            ],
42
        ];
43
    }
44
45
    /**
46
     * Actions defined in classes, eg. error page.
47
     *
48
     * @return array
49
     */
50
    public function actions()
51
    {
52
        return [
53
            'error' => [
54
                'class' => 'yii\web\ErrorAction',
55
            ],
56
        ];
57
    }
58
59
    /**
60
     * Application dashboard.
61
     *
62
     * @return string
63
     */
64
    public function actionIndex()
65
    {
66
        return $this->render('index');
67
    }
68
69
    /**
70
     * Application configuration.
71
     *
72
     * @return string
73
     */
74
    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...
75
    {
76
        $config = $GLOBALS['config'];
77
        $modules = $config['modules'];
78
        unset($config['modules']);
79
        $components = $config['components'];
80
        unset($config['components']);
81
        $params = $config['params'];
82
        unset($config['params']);
83
84
        $loadedModules = Metadata::getModules();
85
        $loadedModulesDataProvider = new ArrayDataProvider(['allModels' => $loadedModules]);
86
87
        return $this->render(
88
            'view-config',
89
            [
90
                'config' => $config,
91
                'modules' => $modules,
92
                'components' => $components,
93
                'params' => $params,
94
                'loadedModulesDataProvider' => $loadedModulesDataProvider,
95
            ]
96
        );
97
    }
98
}
99