Completed
Push — master ( b864d9...3fa98b )
by Dmitry
07:42 queued 03:17
created

MonthlyQuantity::setContext()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 5
cp 0
rs 9.9332
c 0
b 0
f 0
cc 3
nc 2
nop 1
crap 12
1
<?php
2
3
namespace hipanel\modules\finance\logic\bill;
4
5
use hipanel\modules\finance\models\Bill;
6
use hipanel\modules\finance\models\Charge;
7
use Yii;
8
9
/**
10
 * Class MonthlyQuantity
11
 *
12
 * @author Dmytro Naumenko <[email protected]>
13
 */
14
class MonthlyQuantity extends DefaultQuantityFormatter implements ContextAwareQuantityFormatter
15
{
16
    /** @var Bill|Charge */
17
    protected $model;
18
19
    /**
20
     * @inheritdoc
21
     */
22
    public function format(): string
23
    {
24
        $text = Yii::t('hipanel:finance', '{quantity, plural, one{# day} other{# days}}', ['quantity' => $this->getClientValue()]);
25
26
        return $text;
27
    }
28
29
    /**
30
     * @inheritdoc
31
     */
32
    public function getValue(): string
33
    {
34
        return $this->getQuantity() / $this->getNumberOfDays();
35
    }
36
37
    /**
38
     * @inheritdoc
39
     */
40
    public function getClientValue(): string
41
    {
42
        return round($this->getQuantity()->getQuantity() * $this->getNumberOfDays());
43
    }
44
45
    /**
46
     * @return false|string
47
     */
48
    protected function getNumberOfDays()
49
    {
50
        return date('t', strtotime($this->model->time));
51
    }
52
53
    /**
54
     * @param $context
55
     * @return ContextAwareQuantityFormatter
56
     */
57
    public function setContext($context): ContextAwareQuantityFormatter
58
    {
59
        if (!$context instanceof Bill && !$context instanceof Charge) {
60
            throw new \OutOfBoundsException('Context is not supported by Monthly quantity');
61
        }
62
63
        $this->model = $context;
64
65
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (hipanel\modules\finance\logic\bill\MonthlyQuantity) is incompatible with the return type declared by the interface hipanel\modules\finance\...tyFormatter::setContext of type self.

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...
66
    }
67
}
68