TariffProfileController::behaviors()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 14
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 14
loc 14
ccs 0
cts 14
cp 0
rs 9.7998
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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\SmartCreateAction;
16
use hipanel\actions\SmartDeleteAction;
17
use hipanel\actions\SmartUpdateAction;
18
use hipanel\actions\ValidateFormAction;
19
use hipanel\actions\ViewAction;
20
use hipanel\filters\EasyAccessControl;
21
use Yii;
22
23
class TariffProfileController extends \hipanel\base\CrudController
24
{
25 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...
26
    {
27
        return array_merge(parent::behaviors(), [
0 ignored issues
show
Bug Best Practice introduced by
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...
28
            'access-control' => [
29
                'class' => EasyAccessControl::class,
30
                'actions' => [
31
                    'create' => 'plan.create',
32
                    'update' => 'plan.update',
33
                    'delete' => 'plan.delete',
34
                    '*'      => 'plan.read',
35
                ],
36
            ],
37
        ]);
38
    }
39
40
    public function actions()
41
    {
42
        return array_merge(parent::actions(), [
43
            'index' => [
44
                'class' => IndexAction::class,
45
            ],
46
            'create' => [
47
                'class' => SmartCreateAction::class,
48
                'data' => function(\hipanel\actions\RenderAction $action) : array {
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
                    $user = Yii::$app->user->identity;
50
                    if ($user->type === 'reseller') {
51
                        return [
52
                            'client' => $user->username,
53
                            'client_id' => $user->id,
54
                        ];
55
                    }
56
                    return [
57
                        'client' => $user->seller,
58
                        'client_id' => $user->seller_id,
59
                    ];
60
                }
61
            ],
62
            'update' => [
63
                'class' => SmartUpdateAction::class,
64
                'data'  => function(\hipanel\actions\RenderAction $action, array $data) : array {
65
                    $model = $data['model'];
66
                    return [
67
                        'client' => $model->client,
68
                        'client_id' => $model->client_id,
69
                    ];
70
                },
71
            ],
72
            'search' => [
73
                'class' => ComboSearchAction::class,
74
            ],
75
            'validate-form' => [
76
                'class' => ValidateFormAction::class,
77
            ],
78
            'view' => [
79
                'class' => ViewAction::class,
80
            ],
81
            'delete' => [
82
                'class' => SmartDeleteAction::class,
83
                'success' => Yii::t('hipanel.finance.tariffprofile', 'Profile deleted'),
84
            ],
85
        ]);
86
    }
87
}
88