RequestController::getStates()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * Hosting Plugin for HiPanel
4
 *
5
 * @link      https://github.com/hiqdev/hipanel-module-hosting
6
 * @package   hipanel-module-hosting
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
/**
12
 * @see    http://hiqdev.com/hipanel-module-hosting
13
 * @license http://hiqdev.com/hipanel-module-hosting/license
14
 * @copyright Copyright (c) 2015 HiQDev
15
 */
16
17
namespace hipanel\modules\hosting\controllers;
18
19
use hipanel\actions\IndexAction;
20
use hipanel\actions\SmartDeleteAction;
21
use hipanel\actions\ViewAction;
22
use Yii;
23
24
class RequestController extends \hipanel\base\CrudController
25
{
26
    public function actions()
27
    {
28
        return array_merge(parent::actions(), [
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array_merge(paren...o delete request.')))); (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...
29
            'index' => [
30
                'class' => IndexAction::class,
31
                'findOptions' => [
32
                    'object_class_ni' => 'domain',
33
                ],
34
                '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...
35
                    return [
36
                        'objects' => $this->getObjects(),
37
                        'states' => $this->getFilteredStates(),
38
                    ];
39
                },
40
                'filterStorageMap' => [
41
                    'state' => 'hosting.request.state',
42
                    'server' => 'server.server.name',
43
                    'account' => 'hosting.account.login',
44
                    'client_id' => 'client.client.id',
45
                ],
46
            ],
47
            'view' => [
48
                'class' => ViewAction::class,
49
                'findOptions' => [
50
                    'object_class_ni' => 'domain',
51
                ],
52
            ],
53
            'delete' => [
54
                'class' => SmartDeleteAction::class,
55
                'success' => Yii::t('hipanel:hosting', 'Deleted'),
56
                'error' => Yii::t('hipanel:hosting', 'An error occurred when trying to delete request.'),
57
            ],
58
        ]);
59
    }
60
61
    public function getFilteredStates()
62
    {
63
        $result = $this->getStates();
64
        unset($result['done']);
65
66
        return $result;
67
    }
68
69
    public function getStates()
70
    {
71
        return $this->getRefs('state,request', 'hipanel:hosting');
72
    }
73
74
    public function getObjects()
75
    {
76
        return [
77
            'db'        => Yii::t('hipanel:hosting', 'Database'),
78
            'hdomain'   => Yii::t('hipanel:hosting', 'Domain'),
79
            'device'    => Yii::t('hipanel:hosting', 'Server'),
80
            'service'   => Yii::t('hipanel:hosting', 'Service'),
81
        ];
82
    }
83
}
84