Completed
Push — master ( 7261bd...99740c )
by Dmitry
05:03 queued 11s
created

HeldPaymentsController::actions()   B

Complexity

Conditions 3
Paths 1

Size

Total Lines 70

Duplication

Lines 16
Ratio 22.86 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 16
loc 70
ccs 0
cts 65
cp 0
rs 8.6545
c 0
b 0
f 0
cc 3
nc 1
nop 0
crap 12

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
 * 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\IndexAction;
14
use hipanel\actions\PrepareBulkAction;
15
use hipanel\actions\RedirectAction;
16
use hipanel\actions\SmartUpdateAction;
17
use hipanel\base\CrudController;
18
use hipanel\filters\EasyAccessControl;
19
use hipanel\modules\finance\models\Change;
20
use Yii;
21
use yii\base\Event;
22
23
class HeldPaymentsController extends CrudController
24
{
25
    public static function modelClassName()
26
    {
27
        return Change::class;
28
    }
29
30
    public function behaviors()
31
    {
32
        return array_merge(parent::behaviors(), [
33
            [
34
                'class' => EasyAccessControl::class,
35
                'actions' => [
36
                    '*' => 'resell',
37
                ],
38
            ],
39
        ]);
40
    }
41
42
    public function actions()
43
    {
44
        return [
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array('index' => ...s\Change::STATE_NEW))); (array<string,array>) is incompatible with the return type of the parent method hipanel\base\Controller::actions of type array<string,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...
45
            'index' => [
46
                'class' => IndexAction::class,
47
                'findOptions' => ['state' => 'new', 'class' => 'merchant'],
48
                '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...
49
                    return [
50
                        'states' => Change::getStates(),
51
                    ];
52
                },
53
            ],
54
            'bulk-approve' => [
55
                'class' => SmartUpdateAction::class,
56
                'scenario' => 'approve',
57
                'success' => Yii::t('hipanel:finance:change', 'Held payments were approved successfully'),
58
                'error' => Yii::t('hipanel:finance:change', 'Error occurred during held payments approving'),
59
                'POST html' => [
60
                    'save'    => true,
61
                    'success' => [
62
                        'class' => RedirectAction::class,
63
                    ],
64
                ],
65 View Code Duplication
                'on beforeSave' => function (Event $event) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
66
                    /** @var \hipanel\actions\Action $action */
67
                    $action = $event->sender;
68
                    $comment = Yii::$app->request->post('comment');
69
                    foreach ($action->collection->models as $model) {
70
                        $model->setAttribute('comment', $comment);
71
                    }
72
                },
73
            ],
74
            'bulk-approve-modal' => [
75
                'class' => PrepareBulkAction::class,
76
                'scenario' => 'approve',
77
                'view' => '_bulkApprove',
78
                'findOptions' => [
79
                    'state' => Change::STATE_NEW,
80
                ],
81
            ],
82
            'bulk-reject' => [
83
                'class' => SmartUpdateAction::class,
84
                'scenario' => 'reject',
85
                'success' => Yii::t('hipanel:finance:change', 'Held payments were rejected successfully'),
86
                'error' => Yii::t('hipanel:finance:change', 'Error occurred during held payments rejecting'),
87
                'POST html' => [
88
                    'save'    => true,
89
                    'success' => [
90
                        'class' => RedirectAction::class,
91
                    ],
92
                ],
93 View Code Duplication
                'on beforeSave' => function (Event $event) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
94
                    /** @var \hipanel\actions\Action $action */
95
                    $action = $event->sender;
96
                    $comment = Yii::$app->request->post('comment');
97
                    foreach ($action->collection->models as $model) {
98
                        $model->setAttribute('comment', $comment);
99
                    }
100
                },
101
            ],
102
            'bulk-reject-modal' => [
103
                'class' => PrepareBulkAction::class,
104
                'scenario' => 'reject',
105
                'view' => '_bulkReject',
106
                'findOptions' => [
107
                    'state' => Change::STATE_NEW,
108
                ],
109
            ],
110
        ];
111
    }
112
}
113