Passed
Push — master ( 723f6d...528355 )
by Doug
14:53
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 99
    public readonly int $volume;
21
22
    public function __construct(
23
        public readonly Item $item,
24
        public readonly int $x,
25
        public readonly int $y,
26
        public readonly int $z,
27
        public readonly int $width,
28
        public readonly int $length,
29 99
        public readonly int $depth
30
    ) {
31 95
    }
32
33 95
    public static function fromOrientatedItem(OrientatedItem $orientatedItem, int $x, int $y, int $z): self
34
    {
35
        return new self(
36 95
            $orientatedItem->item,
37
            $x,
38 95
            $y,
39
            $z,
40
            $orientatedItem->width,
41 95
            $orientatedItem->length,
42
            $orientatedItem->depth,
43 95
        );
44
    }
45
46 95
    public function jsonSerialize(): array
47
    {
48 95
        $userValues = [];
49
50
        if ($this->item instanceof JsonSerializable) {
51 95
            $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

51
            /** @scrutinizer ignore-call */ 
52
            $userSerialisation = $this->item->jsonSerialize();
Loading history...
52
            if (is_iterable($userSerialisation)) {
53 95
                $userValues = $userSerialisation;
54
            } else {
55
                $userValues = ['extra' => $userSerialisation];
56 95
            }
57
        }
58 95
59
        return [
60
            'x' => $this->x,
61 95
            'y' => $this->y,
62
            'z' => $this->z,
63 95
            'width' => $this->width,
64
            'length' => $this->length,
65
            'depth' => $this->depth,
66 95
            'item' => [
67
                ...$userValues,
68 95
                'description' => $this->item->getDescription(),
69
                'width' => $this->item->getWidth(),
70
                'length' => $this->item->getLength(),
71 94
                'depth' => $this->item->getDepth(),
72
                'allowedRotation' => $this->item->getAllowedRotation(),
73 94
            ],
74 94
        ];
75 94
    }
76
}
77