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

PackedItem::getVolume()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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