Item::getKeepFlat()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 * @package Packer
5
 * @author Iurii Makukh <[email protected]>
6
 * @copyright Copyright (c) 2018, Iurii Makukh <[email protected]>
7
 * @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL-3.0-or-later
8
 */
9
10
namespace gplcart\modules\packer\helpers;
11
12
use DVDoug\BoxPacker\Item as ItemInterface;
13
14
/**
15
 * Item object used by Packer library
16
 */
17
class Item implements ItemInterface
18
{
19
    /**
20
     * Itam array data
21
     * @var array
22
     */
23
    private $item = array();
24
25
    /**
26
     * Item constructor.
27
     * @param array $item
28
     */
29
    public function __construct(array $item)
30
    {
31
        $this->item = $item;
32
    }
33
34
    /**
35
     * @return array
36
     */
37
    public function getItemData()
38
    {
39
        return $this->item;
40
    }
41
42
    /**
43
     * @return string
44
     */
45
    public function getDescription()
46
    {
47
        return $this->item['title'];
48
    }
49
50
    /**
51
     * @return int
52
     */
53
    public function getWidth()
54
    {
55
        return $this->item['width'];
56
    }
57
58
    /**
59
     * @return int
60
     */
61
    public function getLength()
62
    {
63
        return $this->item['length'];
64
    }
65
66
    /**
67
     * @return int
68
     */
69
    public function getDepth()
70
    {
71
        return $this->item['width'];
72
    }
73
74
    /**
75
     * @return int
76
     */
77
    public function getWeight()
78
    {
79
        return $this->item['weight'];
80
    }
81
82
    /**
83
     * @return int
84
     */
85
    public function getVolume()
86
    {
87
        return $this->item['volume'];
88
    }
89
90
    /**
91
     * @return int
92
     */
93
    public function getKeepFlat()
94
    {
95
        return (int) !empty($this->item['flat']);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return (int) (!empty($this->item['flat'])); (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...
96
    }
97
}