Completed
Push — master ( c2bc12...ef5bc2 )
by Dmitry
04:55
created

src/controllers/TariffProfileController.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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()
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
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