Completed
Push — master ( d02bdd...0a5fc3 )
by Andrii
02:11
created

EnumPrice::calculateSum()   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 1
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\price;
12
13
use hiqdev\php\billing\target\TargetInterface;
14
use hiqdev\php\billing\type\TypeInterface;
15
use hiqdev\php\units\QuantityInterface;
16
use hiqdev\php\units\UnitInterface;
17
18
/**
19
 * Enum Price:
20
 * - listed quantities only else exception.
21
 * @see PriceInterface
22
 *
23
 * @author Andrii Vasyliev <[email protected]>
24
 */
25
class EnumPrice extends AbstractPrice
26
{
27
    /**
28
     * @var UnitInterface
29
     */
30
    protected $unit;
31
32
    /**
33
     * @var MoneyInterface[] amount => price
34
     */
35
    protected $prices;
36
37 2
    public function __construct(
38
                            $id,
39
        TypeInterface       $type,
40
        TargetInterface     $target,
41
        UnitInterface       $unit,
42
        array               $prices
43
    ) {
44 2
        parent::__construct($id, $type, $target);
45 2
        $this->unit = $unit;
46 2
        $this->prices = $prices;
47 2
    }
48
49 1
    public function getUnit()
50
    {
51 1
        return $this->unit;
52
    }
53
54 1
    public function getPrices()
55
    {
56 1
        return $this->prices;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62 3
    public function calculateSum(QuantityInterface $quantity)
63
    {
64 3
        return $this->calculatePrice($quantity);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->calculatePrice($quantity); (hiqdev\php\billing\price\MoneyInterface) is incompatible with the return type declared by the interface hiqdev\php\billing\price...Interface::calculateSum of type Money\Money|null.

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
    /**
68
     * {@inheritdoc}
69
     */
70 3
    public function calculatePrice(QuantityInterface $quantity)
71
    {
72 3
        $usage = (string) $this->calculateUsage($quantity)->getQuantity();
73 3
        foreach ($this->prices as $value => $price) {
74 3
            if ((string) $value === (string) $usage) {
75 3
                return $price;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $price; (hiqdev\php\billing\price\MoneyInterface) is incompatible with the return type declared by the interface hiqdev\php\billing\price...terface::calculatePrice of type Money\Money|null.

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...
76
            }
77 3
        }
78
79
        throw new FailedCalculatePriceException('not enumed quantity: ' . $usage);
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85 3
    public function calculateUsage(QuantityInterface $quantity)
86
    {
87 3
        return $quantity->convert($this->unit);
88
    }
89
}
90