Completed
Push — master ( ae53ec...716429 )
by Dmitry
15:10
created

VdsTariffManager   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 67
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 1
cbo 6
dl 67
loc 67
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 8 8 2
A buildForm() 9 9 1
A insert() 14 14 2
A update() 12 12 2
A getZones() 4 4 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 hipanel\modules\finance\logic;
4
5
use hipanel\modules\finance\forms\DomainTariffForm;
6
use hipanel\modules\finance\models\Tariff;
7
use hiqdev\hiart\ErrorResponseException;
8
use Yii;
9
use yii\web\NotFoundHttpException;
10
use yii\web\UnprocessableEntityHttpException;
11
12 View Code Duplication
class VdsTariffManager extends AbstractTariffManager
0 ignored issues
show
Duplication introduced by
This class 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...
13
{
14
    /**
15
     * @var VdsTariffForm
16
     */
17
    public $form;
18
19
    /**
20
     * @inheritdoc
21
     */
22
    protected $type = 'server';
23
24
    public function init()
25
    {
26
        parent::init();
27
28
        if (!Yii::getAlias('@server', true)) {
29
            throw new NotFoundHttpException('Domain module is missing');
30
        }
31
    }
32
33
    protected function buildForm()
34
    {
35
        $this->form = new DomainTariffForm([
0 ignored issues
show
Documentation Bug introduced by
It seems like new \hipanel\modules\fin...iff' => $this->tariff)) of type object<hipanel\modules\f...forms\DomainTariffForm> is incompatible with the declared type object<hipanel\modules\f...ce\logic\VdsTariffForm> of property $form.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
36
            'scenario' => $this->scenario,
37
            'zones' => $this->getZones(),
38
            'baseTariffs' => $this->baseTariffs,
39
            'tariff' => $this->tariff
40
        ]);
41
    }
42
43
    public function insert()
44
    {
45
        $data = $this->form->toArray();
46
47
        try {
48
            $result = Tariff::perform('Create', $data);
49
        } catch (ErrorResponseException $e) {
50
            throw new UnprocessableEntityHttpException($e->getMessage(), 0, $e);
51
        }
52
53
        $this->form->id = $result['id'];
54
55
        return true;
56
    }
57
58
    public function update()
59
    {
60
        $data = $this->form->toArray();
61
62
        try {
63
            $result = Tariff::perform('Update', $data);
0 ignored issues
show
Unused Code introduced by
$result 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...
64
        } catch (ErrorResponseException $e) {
65
            throw new UnprocessableEntityHttpException($e->getMessage(), 0, $e);
66
        }
67
68
        return true;
69
    }
70
71
    /**
72
     * @return array
73
     */
74
    protected function getZones()
75
    {
76
        return Yii::$app->hiart->get('getZones');
77
    }
78
}
79