TariffController   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 153
Duplicated Lines 64.05 %

Coupling/Cohesion

Components 1
Dependencies 14

Test Coverage

Coverage 9.84%

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 14
dl 98
loc 153
ccs 6
cts 61
cp 0.0984
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A behaviors() 14 14 1
A actions() 0 27 1
A actionCreateDomain() 14 14 3
A actionCreateSvds() 15 15 3
A actionCreateOvds() 14 14 3
A actionCreateCertificate() 14 14 3
A actionCreateServer() 14 14 3
A actionUpdate() 13 14 3
A actionCopy() 0 9 1
A actionView() 0 6 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
 * Finance module for HiPanel
4
 *
5
 * @link      https://github.com/hiqdev/hipanel-module-finance
6
 * @package   hipanel-module-finance
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\modules\finance\controllers;
12
13
use hipanel\actions\ComboSearchAction;
14
use hipanel\actions\IndexAction;
15
use hipanel\actions\SmartDeleteAction;
16
use hipanel\actions\SmartUpdateAction;
17
use hipanel\actions\ValidateFormAction;
18
use hipanel\filters\EasyAccessControl;
19
use hipanel\models\Ref;
20
use hipanel\modules\finance\logic\AbstractTariffManager;
21
use hipanel\modules\finance\logic\CertificateTariffManager;
22
use hipanel\modules\finance\logic\DomainTariffManager;
23
use hipanel\modules\finance\logic\OvdsTariffManager;
24
use hipanel\modules\finance\logic\ServerTariffManager;
25
use hipanel\modules\finance\logic\SvdsTariffManager;
26
use hipanel\modules\finance\logic\TariffManagerFactory;
27
use Yii;
28
29
class TariffController extends \hipanel\base\CrudController
30
{
31 View Code Duplication
    public function behaviors()
0 ignored issues
show
Duplication introduced by pkolomiy
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...
32
    {
33
        return array_merge(parent::behaviors(), [
0 ignored issues
show
Bug Best Practice introduced by Andrii Vasyliev
The return type of return array_merge(paren...'*' => 'plan.read')))); (array<*,array>) is incompatible with the return type of the parent method hipanel\base\Controller::behaviors of type array[].

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

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

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
34
            'access-control' => [
35
                'class' => EasyAccessControl::class,
36
                'actions' => [
37
                    'create,import,copy' => 'plan.create',
38
                    'update'    => 'plan.update',
39
                    'delete'    => 'plan.delete',
40
                    '*'         => 'plan.read',
41
                ],
42
            ],
43
        ]);
44
    }
45
46 1
    public function actions()
47
    {
48 1
        return array_merge(parent::actions(), [
49 1
            'index' => [
50
                'class' => IndexAction::class,
51
                'data' => function () {
52
                    return [
53
                        'types' => Ref::getList('type,tariff', 'hipanel:finance:tariff:types'),
54
                    ];
55 1
                },
56
            ],
57
            'search' => [
58
                'class' => ComboSearchAction::class,
59
            ],
60
            'validate-form' => [
61
                'class' => ValidateFormAction::class,
62
            ],
63
            'set-note' => [
64
                'class' => SmartUpdateAction::class,
65 1
                'success' => Yii::t('hipanel', 'Note updated'),
66
            ],
67
            'delete' => [
68
                'class' => SmartDeleteAction::class,
69 1
                'success' => Yii::t('hipanel:finance:tariff', 'Tariff deleted'),
70
            ],
71
        ]);
72
    }
73
74 View Code Duplication
    public function actionCreateDomain($parent_id = null)
0 ignored issues
show
Duplication introduced by SilverFire - Dmitry Naumenko
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...
75
    {
76
        /** @var DomainTariffManager $manager */
77
        $manager = TariffManagerFactory::createByType('domain', $parent_id);
78
        $form = $manager->form;
79
80
        if (Yii::$app->request->isPost && $form->load(Yii::$app->request->post())) {
81
            $manager->insert();
82
83
            return $this->redirect(['view', 'id' => $form->id]);
84
        }
85
86
        return $this->render('domain/create', ['model' => $form]);
87
    }
88
89 View Code Duplication
    public function actionCreateSvds($parent_id = null)
0 ignored issues
show
Duplication introduced by SilverFire - Dmitry Naumenko
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...
90
    {
91
        /** @var SvdsTariffManager $manager */
92
        $manager = TariffManagerFactory::createByType('svds', $parent_id);
93
94
        $form = $manager->form;
95
96
        if (Yii::$app->request->isPost && $form->load(Yii::$app->request->post())) {
97
            $manager->insert();
98
99
            return $this->redirect(['view', 'id' => $form->id]);
100
        }
101
102
        return $this->render('vds/create', ['model' => $form]);
103
    }
104
105 View Code Duplication
    public function actionCreateOvds($parent_id = null)
0 ignored issues
show
Duplication introduced by SilverFire - Dmitry Naumenko
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
        /** @var OvdsTariffManager $manager */
108
        $manager = TariffManagerFactory::createByType('ovds', $parent_id);
109
        $form = $manager->form;
110
111
        if (Yii::$app->request->isPost && $form->load(Yii::$app->request->post())) {
112
            $manager->insert();
113
114
            return $this->redirect(['view', 'id' => $form->id]);
115
        }
116
117
        return $this->render('vds/create', ['model' => $form]);
118
    }
119
120 View Code Duplication
    public function actionCreateCertificate($parent_id = null)
0 ignored issues
show
Duplication introduced by SilverFire - Dmitry Naumenko
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...
121
    {
122
        /** @var CertificateTariffManager $manager */
123
        $manager = TariffManagerFactory::createByType('certificate', $parent_id);
124
        $form = $manager->form;
125
126
        if (Yii::$app->request->isPost && $form->load(Yii::$app->request->post())) {
127
            $manager->insert();
128
129
            return $this->redirect(['view', 'id' => $form->id]);
130
        }
131
132
        return $this->render('certificate/create', ['model' => $form]);
133
    }
134
135 View Code Duplication
    public function actionCreateServer($parent_id = null)
0 ignored issues
show
Duplication introduced by SilverFire - Dmitry Naumenko
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...
136
    {
137
        /** @var ServerTariffManager $manager */
138
        $manager = TariffManagerFactory::createByType('server', $parent_id);
139
        $form = $manager->form;
140
141
        if (Yii::$app->request->isPost && $form->load(Yii::$app->request->post())) {
142
            $manager->insert();
143
144
            return $this->redirect(['view', 'id' => $form->id]);
145
        }
146
147
        return $this->render('server/create', ['model' => $form]);
148
    }
149
150 View Code Duplication
    public function actionUpdate($id)
0 ignored issues
show
Duplication introduced by SilverFire - Dmitry Naumenko
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...
151
    {
152
        /** @var AbstractTariffManager $manager */
153
        $manager = TariffManagerFactory::createById($id, ['scenario' => 'update']);
154
        $form = $manager->form;
155
156
        if (Yii::$app->request->isPost && $form->load(Yii::$app->request->post())) {
157
            $manager->update();
158
159
            return $this->redirect(['view', 'id' => $form->id]);
160
        }
161
162
        return $this->render($manager->getType() . '/update', ['model' => $form]);
163
    }
164
165
    public function actionCopy()
166
    {
167
        $id = Yii::$app->request->post('selection')[0];
168
        $manager = TariffManagerFactory::createById($id, ['scenario' => 'create']);
169
        $form = $manager->form;
170
        $form->id = null;
171
172
        return $this->render($manager->getType() . '/copy', ['model' => $form]);
173
    }
174
175
    public function actionView($id)
176
    {
177
        $manager = TariffManagerFactory::createById($id);
178
179
        return $this->render('view', ['manager' => $manager]);
180
    }
181
}
182