Completed
Push — master ( fa9e98...bd4ae6 )
by Dmitry
01:56
created

TemplatePrice   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A jsonSerialize() 0 6 1
A getSubprices() 0 4 1
1
<?php
2
/**
3
 * API for Billing
4
 *
5
 * @link      https://github.com/hiqdev/billing-hiapi
6
 * @package   billing-hiapi
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2017-2018, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\billing\hiapi\price;
12
13
use hiqdev\php\billing\plan\PlanInterface;
14
use hiqdev\php\billing\price\SinglePrice;
15
use hiqdev\php\billing\target\TargetInterface;
16
use hiqdev\php\billing\type\TypeInterface;
17
use hiqdev\php\units\QuantityInterface;
18
use Money\Money;
19
20
/**
21
 * Class TemplatePrice.
22
 *
23
 * @author Dmytro Naumenko <[email protected]>
24
 */
25
class TemplatePrice extends SinglePrice
26
{
27
    /**
28
     * @var Money[]
29
     */
30
    protected $subprices = [];
31
32
    public function __construct(
33
        $id,
34
        TypeInterface $type,
35
        TargetInterface $target,
36
        PlanInterface $plan = null,
37
        QuantityInterface $prepaid,
38
        Money $price,
39
        array $subprices
40
    ) {
41
        parent::__construct($id, $type, $target, $plan, $prepaid, $price);
42
43
        $this->subprices = $subprices;
44
    }
45
46
    public function jsonSerialize()
47
    {
48
        return array_merge(parent::jsonSerialize(), [
0 ignored issues
show
Best Practice introduced by
The expression return array_merge(paren... => $this->subprices)); seems to be an array, but some of its elements' types (Money\Money[]) are incompatible with the return type of the parent method hiqdev\php\billing\price...glePrice::jsonSerialize of type array<string,integer|hiq...s\Quantity|Money\Money>.

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 new Author('Johannes');
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return '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...
49
            'subprices' => $this->subprices,
50
        ]);
51
    }
52
53
    /**
54
     * @return Money[]
55
     */
56
    public function getSubprices(): array
57
    {
58
        return $this->subprices;
59
    }
60
}
61