Passed
Pull Request — master (#521)
by
unknown
13:53 queued 12:16
created

PackedItem   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Test Coverage

Coverage 57.14%

Importance

Changes 7
Bugs 3 Features 0
Metric Value
eloc 37
c 7
b 3
f 0
dl 0
loc 93
ccs 28
cts 49
cp 0.5714
rs 10
wmc 13

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getVolume() 0 3 1
A __construct() 0 9 1
A getZ() 0 3 1
A getY() 0 3 1
A getX() 0 3 1
A getItem() 0 3 1
A getLength() 0 3 1
A getWidth() 0 3 1
A getDepth() 0 3 1
A fromOrientatedItem() 0 10 1
A jsonSerialize() 0 27 3
1
<?php
2
/**
3
 * Box packing (3D bin packing, knapsack problem).
4
 *
5
 * @author Doug Wright
6
 */
7
declare(strict_types=1);
8
9
namespace DVDoug\BoxPacker;
10
11
use JsonSerializable;
12
13
use function is_iterable;
14
15
/**
16
 * A packed item.
17
 */
18
class PackedItem implements JsonSerializable
19
{
20 82
    public function __construct(
21
        protected Item $item,
22
        protected int $x,
23
        protected int $y,
24
        protected int $z,
25
        protected int $width,
26
        protected int $length,
27
        protected int $depth
28
    ) {
29 82
    }
30
31 82
    public function getX(): int
32
    {
33 82
        return $this->x;
34
    }
35
36 82
    public function getY(): int
37
    {
38 82
        return $this->y;
39
    }
40
41 82
    public function getZ(): int
42
    {
43 82
        return $this->z;
44
    }
45
46 82
    public function getItem(): Item
47
    {
48 82
        return $this->item;
49
    }
50
51 82
    public function getWidth(): int
52
    {
53 82
        return $this->width;
54
    }
55
56 82
    public function getLength(): int
57
    {
58 82
        return $this->length;
59
    }
60
61 82
    public function getDepth(): int
62
    {
63 82
        return $this->depth;
64
    }
65
66 82
    public function getVolume(): int
67
    {
68 82
        return $this->width * $this->length * $this->depth;
69
    }
70
71 82
    public static function fromOrientatedItem(OrientatedItem $orientatedItem, int $x, int $y, int $z): self
72
    {
73 82
        return new self(
74 82
            $orientatedItem->getItem(),
75 82
            $x,
76 82
            $y,
77 82
            $z,
78 82
            $orientatedItem->getWidth(),
79 82
            $orientatedItem->getLength(),
80 82
            $orientatedItem->getDepth()
81 82
        );
82
    }
83
84
    public function jsonSerialize(): array
85
    {
86
        $userValues = [];
87
88
        if ($this->item instanceof JsonSerializable) {
89
            $userSerialisation = $this->item->jsonSerialize();
0 ignored issues
show
Bug introduced by
The method jsonSerialize() does not exist on DVDoug\BoxPacker\Item. It seems like you code against a sub-type of said class. However, the method does not exist in DVDoug\BoxPacker\ConstrainedPlacementItem or DVDoug\BoxPacker\Test\THPackTestItem. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

89
            /** @scrutinizer ignore-call */ 
90
            $userSerialisation = $this->item->jsonSerialize();
Loading history...
90
            if (is_iterable($userSerialisation)) {
91
                $userValues = $userSerialisation;
92
            } else {
93
                $userValues = ['extra' => $userSerialisation];
94
            }
95
        }
96
97
        return [
98
            'x' => $this->x,
99
            'y' => $this->y,
100
            'z' => $this->z,
101
            'width' => $this->width,
102
            'length' => $this->length,
103
            'depth' => $this->depth,
104
            'item' => [
105
                ...$userValues,
106
                'description' => $this->item->getDescription(),
107
                'width' => $this->item->getWidth(),
108
                'length' => $this->item->getLength(),
109
                'depth' => $this->item->getDepth(),
110
                'allowedRotation' => $this->item->getAllowedRotation(),
111
            ],
112
        ];
113
    }
114
}
115