IspResourceDecorator   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 22
ccs 0
cts 16
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A displayTitle() 0 4 1
A displayPrepaidAmount() 0 4 2
A prepaidAmountType() 0 4 1
A amountOptions() 0 4 1
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\models\decorators\server;
12
13
use hipanel\inputs\OptionsInput;
14
use Yii;
15
16
class IspResourceDecorator extends AbstractServerResourceDecorator
17
{
18
    public function displayTitle()
19
    {
20
        return Yii::t('hipanel:server:order', 'ISP Manager');
21
    }
22
23
    public function displayPrepaidAmount()
24
    {
25
        return $this->getPrepaidQuantity() > 0 ? $this->amountOptions()[1] : $this->amountOptions()[0];
26
    }
27
28
    public function prepaidAmountType()
29
    {
30
        return new OptionsInput($this->amountOptions());
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \hipanel\inpu...this->amountOptions()); (hipanel\inputs\OptionsInput) is incompatible with the return type of the parent method hipanel\modules\finance\...ator::prepaidAmountType of type hipanel\inputs\TextInput.

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...
31
    }
32
33
    private function amountOptions()
34
    {
35
        return [0 => Yii::t('hipanel', 'Disabled'), 1 => Yii::t('hipanel', 'Enabled')];
36
    }
37
}
38