Completed
Push — master ( 6b14db...a7e110 )
by Andrii
01:47
created

AbstractAction   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 2
dl 0
loc 105
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A getClient() 0 4 1
A getTarget() 0 4 1
A getQuantity() 0 4 1
A getTime() 0 4 1
A calculateCharge() 0 18 4
isApplicable() 0 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;
12
13
use DateTime;
14
15
/**
16
 * Chargable Action.
17
 *
18
 * @see ActionInterface
19
 *
20
 * @author Andrii Vasyliev <[email protected]>
21
 */
22
abstract class AbstractAction implements ActionInterface
23
{
24
    /**
25
     * @var int
26
     */
27
    protected $id;
28
29
    /**
30
     * @var ClientInterface
31
     */
32
    protected $client;
33
34
    /**
35
     * @var TargetInterface
36
     */
37
    protected $target;
38
39
    /**
40
     * @var QuantityInterface
41
     */
42
    protected $quantity;
43
44
    /**
45
     * @var DateTime
46
     */
47
    protected $time;
48
49
    /**
50
     * @param ClientInterface $client
51
     * @param TargetInterface $target
52
     * @param QuantityInterface $quantity
53
     * @param DateTime $time
54
     */
55
    public function __construct(
56
        ClientInterface $client,
57
        TargetInterface $target,
58
        QuantityInterface $quantity,
59
        DateTime $time
60
    ) {
61
        $this->client = $client;
62
        $this->target = $target;
63
        $this->quantity = $quantity;
64
        $this->time = $time;
65
    }
66
67
    /**
68
     * @inheritdoc
69
     */
70
    public function getClient()
71
    {
72
        return $this->client;
73
    }
74
75
    /**
76
     * @inheritdoc
77
     */
78
    public function getTarget()
79
    {
80
        return $this->target;
81
    }
82
83
    /**
84
     * @inheritdoc
85
     */
86
    public function getQuantity()
87
    {
88
        return $this->quantity;
89
    }
90
91
    /**
92
     * @inheritdoc
93
     */
94
    public function getTime()
95
    {
96
        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\ActionInterface::getTime of type hiqdev\php\billing\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...
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    public function calculateCharge(PriceInterface $price)
103
    {
104
        if (!$this->isApplicable($price)) {
105
            return null;
106
        }
107
108
        $usage = $price->calculateUsage($this->getQuantity());
109
        if ($usage === null) {
110
            return null;
111
        }
112
113
        $sum = $this->calculateSum($this->getQuantity());
0 ignored issues
show
Bug introduced by
The method calculateSum() does not seem to exist on object<hiqdev\php\billing\AbstractAction>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
114
        if ($sum === null) {
115
            return null;
116
        }
117
118
        return new Charge($this, $price->getTarget(), $price->getType(), $usage, $sum);
0 ignored issues
show
Documentation introduced by
$usage is of type object<hiqdev\php\units\QuantityInterface>, but the function expects a object<hiqdev\php\billing\QuantityInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Bug Best Practice introduced by
The return type of return new \hiqdev\php\b...tType(), $usage, $sum); (hiqdev\php\billing\Charge) is incompatible with the return type declared by the interface hiqdev\php\billing\Actio...erface::calculateCharge of type hiqdev\php\billing\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...
119
    }
120
121
122
    /**
123
     * @inheritdoc
124
     */
125
    abstract public function isApplicable(PriceInterface $price);
126
}
127