Completed
Push — master ( 3d080b...428142 )
by Andrii
02:09
created

Order::getActions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * PHP Billing Library
4
 *
5
 * @link      https://github.com/hiqdev/php-billing
6
 * @package   php-billing
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\php\billing\order;
12
13
use hiqdev\php\billing\action\ActionInterface;
14
use hiqdev\php\billing\customer\CustomerInterface;
15
16
/**
17
 * @author Andrii Vasyliev <[email protected]>
18
 */
19
class Order implements OrderInterface
20
{
21
    /**
22
     * @var int|string|null
23
     */
24
    protected $id;
25
26
    /**
27
     * @var CustomerInterface
28
     */
29
    protected $customer;
30
31
    /**
32
     * @var ActionInterface[] array: actionKey => action
33
     */
34
    protected $actions = [];
35
36 1
    public function __construct($id, CustomerInterface $customer, array $actions = [])
37
    {
38 1
        $this->id = $id;
39 1
        $this->customer = $customer;
40 1
        $this->actions = $actions;
41 1
    }
42
43
    public static function fromAction(ActionInterface $action)
44
    {
45
        return new static(null, $action->getCustomer(), [$action]);
46
    }
47
48
    public function getId()
49
    {
50
        return $this->id;
51
    }
52
53
    public function getCustomer()
54
    {
55
        return $this->customer;
56
    }
57
58
    /**
59
     * Returns actions.
60
     * @return ActionInterface[] array: actionKey => action
61
     */
62 1
    public function getActions()
63
    {
64 1
        return $this->actions;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->actions; (hiqdev\php\billing\action\ActionInterface[]) is incompatible with the return type declared by the interface hiqdev\php\billing\order...erInterface::getActions of type hiqdev\php\billing\order\ActionInterface[].

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...
65
    }
66
67
    public function jsonSerialize()
68
    {
69
        return get_object_vars($this);
70
    }
71
}
72