Completed
Push — master ( 79ae68...af4be8 )
by Dmitry
13:18
created

RefuseController::actions()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 70
Code Lines 48

Duplication

Lines 70
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 70
loc 70
rs 9.1724
cc 3
eloc 48
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace hipanel\modules\server\controllers;
4
5
use hipanel\actions\Action;
6
use hipanel\actions\IndexAction;
7
use hipanel\actions\PrepareBulkAction;
8
use hipanel\actions\RedirectAction;
9
use hipanel\actions\SmartUpdateAction;
10
use hipanel\base\CrudController;
11
use hipanel\modules\server\models\Change;
12
use Yii;
13
use yii\base\Event;
14
use yii\filters\AccessControl;
15
16 View Code Duplication
class RefuseController extends CrudController
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...
17
{
18
    public static function modelClassName()
19
    {
20
        return Change::class;
21
    }
22
23
    public function behaviors()
24
    {
25
        return array_merge(parent::behaviors(), [
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array_merge(paren...> array('manage')))))); (array<string,array>) is incompatible with the return type of the parent method hipanel\base\Controller::behaviors of type array<string,array<strin...g,string[]|boolean>[]>>.

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...
26
            'access' => [
27
                'class' => AccessControl::className(),
28
                'rules' => [
29
                    [
30
                        'allow'   => true,
31
                        'roles'   => ['manage'],
32
                    ],
33
                ],
34
            ],
35
        ]);
36
    }
37
38
    public function actions()
39
    {
40
        return [
41
            'index' => [
42
                'class' => IndexAction::class,
43
                'findOptions' => ['state' => 'new', 'class' => 'server'],
44
                'data' => function ($action) {
0 ignored issues
show
Unused Code introduced by
The parameter $action is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
45
                    return [
46
                        'states' => Change::getStates(),
47
                    ];
48
                },
49
            ],
50
            'bulk-approve' => [
51
                'class' => SmartUpdateAction::class,
52
                'scenario' => 'approve',
53
                'success' => Yii::t('hipanel/server', 'Server refuse was approved successfully'),
54
                'error' => Yii::t('hipanel/server', 'Error occurred during server refuse approving'),
55
                'POST html' => [
56
                    'save'    => true,
57
                    'success' => [
58
                        'class' => RedirectAction::class,
59
                    ],
60
                ],
61
                'on beforeSave' => function (Event $event) {
62
                    /** @var \hipanel\actions\Action $action */
63
                    $action = $event->sender;
64
                    $comment = Yii::$app->request->post('comment');
65
                    foreach ($action->collection->models as $model) {
66
                        $model->setAttribute('comment', $comment);
67
                    }
68
                },
69
            ],
70
            'bulk-approve-modal' => [
71
                'class' => PrepareBulkAction::class,
72
                'scenario' => 'approve',
73
                'view' => '_bulkApprove',
74
                'findOptions' => [
75
                    'state' => Change::STATE_NEW
76
                ]
77
            ],
78
            'bulk-reject' => [
79
                'class' => SmartUpdateAction::class,
80
                'scenario' => 'reject',
81
                'success' => Yii::t('hipanel/server', 'Server refuse was rejected successfully'),
82
                'error' => Yii::t('hipanel/server', 'Error occurred during server refuse rejecting'),
83
                'POST html' => [
84
                    'save'    => true,
85
                    'success' => [
86
                        'class' => RedirectAction::class,
87
                    ],
88
                ],
89
                'on beforeSave' => function (Event $event) {
90
                    /** @var \hipanel\actions\Action $action */
91
                    $action = $event->sender;
92
                    $comment = Yii::$app->request->post('comment');
93
                    foreach ($action->collection->models as $model) {
94
                        $model->setAttribute('comment', $comment);
95
                    }
96
                },
97
            ],
98
            'bulk-reject-modal' => [
99
                'class' => PrepareBulkAction::class,
100
                'scenario' => 'reject',
101
                'view' => '_bulkReject',
102
                'findOptions' => [
103
                    'state' => Change::STATE_NEW
104
                ]
105
            ],
106
        ];
107
    }
108
}
109