Completed
Push — master ( 6584d5...14a2bb )
by Andrii
08:21
created

AbstractAction::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 4
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\action;
12
13
use hiqdev\php\billing\charge\Charge;
14
use hiqdev\php\billing\price\PriceInterface;
15
use hiqdev\php\billing\target\TargetInterface;
16
use hiqdev\php\billing\customer\CustomerInterface;
17
use DateTime;
18
use hiqdev\php\units\QuantityInterface;
19
20
/**
21
 * Chargable Action.
22
 *
23
 * @see ActionInterface
24
 *
25
 * @author Andrii Vasyliev <[email protected]>
26
 */
27
abstract class AbstractAction implements ActionInterface
28
{
29
    /**
30
     * @var int
31
     */
32
    protected $id;
33
34
    /**
35
     * @var CustomerInterface
36
     */
37
    protected $customer;
38
39
    /**
40
     * @var TargetInterface
41
     */
42
    protected $target;
43
44
    /**
45
     * @var QuantityInterface
46
     */
47
    protected $quantity;
48
49
    /**
50
     * @var DateTime
51
     */
52
    protected $time;
53
54
    /**
55
     * @param CustomerInterface $customer
56
     * @param TargetInterface $target
57
     * @param QuantityInterface $quantity
58
     * @param DateTime $time
59
     */
60 2
    public function __construct(
61
        CustomerInterface $customer,
62
        TargetInterface $target,
63
        QuantityInterface $quantity,
64
        DateTime $time
65
    ) {
66 2
        $this->customer = $customer;
67 2
        $this->target = $target;
68 2
        $this->quantity = $quantity;
69 2
        $this->time = $time;
70 2
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function getCustomer()
76
    {
77
        return $this->customer;
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function getTarget()
84
    {
85
        return $this->target;
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91 2
    public function getQuantity()
92
    {
93 2
        return $this->quantity;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->quantity; (hiqdev\php\units\QuantityInterface) is incompatible with the return type declared by the interface hiqdev\php\billing\actio...nInterface::getQuantity of type hiqdev\php\billing\action\QuantityInterface.

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...
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99
    public function getTime()
100
    {
101
        return $this->time;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->time; (DateTime) is incompatible with the return type declared by the interface hiqdev\php\billing\action\ActionInterface::getTime of type hiqdev\php\billing\action\DateTime.

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...
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107 2
    public function calculateCharge(PriceInterface $price)
108
    {
109 2
        if (!$this->isApplicable($price)) {
110
            return null;
111
        }
112
113 2
        $usage = $price->calculateUsage($this->getQuantity());
114 2
        if ($usage === null) {
115 1
            return null;
116
        }
117
118 1
        $sum = $price->calculateSum($this->getQuantity());
119 1
        if ($sum === null) {
120
            return null;
121
        }
122
123 1
        return new Charge($this, $price->getTarget(), $price->getType(), $usage, $sum);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \hiqdev\php\b...tType(), $usage, $sum); (hiqdev\php\billing\charge\Charge) is incompatible with the return type declared by the interface hiqdev\php\billing\actio...erface::calculateCharge of type hiqdev\php\billing\action\ChargeInterface.

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...
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129
    abstract public function isApplicable(PriceInterface $price);
130
}
131