Completed
Push — 2.x-dev ( 55a3c1...c10cce )
by Doug
33:44
created

TestItem   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
lcom 0
cbo 0
dl 0
loc 116
rs 10
c 1
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A getDescription() 0 4 1
A getWidth() 0 4 1
A getLength() 0 4 1
A getDepth() 0 4 1
A getWeight() 0 4 1
A getVolume() 0 4 1
A getKeepFlat() 0 4 1
1
<?php
2
/**
3
 * Box packing (3D bin packing, knapsack problem)
4
 * @package BoxPacker
5
 * @author Doug Wright
6
 */
7
8
namespace DVDoug\BoxPacker\Test;
9
10
use DVDoug\BoxPacker\Item;
11
12
class TestItem implements Item
13
{
14
15
    /**
16
     * @var string
17
     */
18
    private $description;
19
20
    /**
21
     * @var int
22
     */
23
    private $width;
24
25
    /**
26
     * @var int
27
     */
28
    private $length;
29
30
    /**
31
     * @var int
32
     */
33
    private $depth;
34
35
    /**
36
     * @var int
37
     */
38
    private $weight;
39
40
    /**
41
     * @var int
42
     */
43
    private $keepFlat;
44
45
    /**
46
     * @var int
47
     */
48
    private $volume;
49
50
    /**
51
     * TestItem constructor.
52
     *
53
     * @param string $description
54
     * @param int $width
55
     * @param int $length
56
     * @param int $depth
57
     * @param int $weight
58
     * @param int $keepFlat
59
     */
60
    public function __construct($description, $width, $length, $depth, $weight, $keepFlat)
61
    {
62
        $this->description = $description;
63
        $this->width = $width;
64
        $this->length = $length;
65
        $this->depth = $depth;
66
        $this->weight = $weight;
67
        $this->keepFlat = $keepFlat;
68
69
        $this->volume = $this->width * $this->length * $this->depth;
70
    }
71
72
    /**
73
     * @return string
74
     */
75
    public function getDescription()
76
    {
77
        return $this->description;
78
    }
79
80
    /**
81
     * @return int
82
     */
83
    public function getWidth()
84
    {
85
        return $this->width;
86
    }
87
88
    /**
89
     * @return int
90
     */
91
    public function getLength()
92
    {
93
        return $this->length;
94
    }
95
96
    /**
97
     * @return int
98
     */
99
    public function getDepth()
100
    {
101
        return $this->depth;
102
    }
103
104
    /**
105
     * @return int
106
     */
107
    public function getWeight()
108
    {
109
        return $this->weight;
110
    }
111
112
    /**
113
     * @return int
114
     */
115
    public function getVolume()
116
    {
117
        return $this->volume;
118
    }
119
120
    /**
121
     * @return int
122
     */
123
    public function getKeepFlat()
124
    {
125
        return $this->keepFlat;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->keepFlat; (integer) is incompatible with the return type declared by the interface DVDoug\BoxPacker\Item::getKeepFlat of type boolean.

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...
126
    }
127
}
128
129
130