Passed
Pull Request — master (#549)
by
unknown
15:13 queued 13:32
created

PackedItem::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 0
c 0
b 0
f 0
nc 1
nop 7
dl 0
loc 9
ccs 1
cts 1
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
    public readonly int $volume;
21
22 82
    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
        public readonly int $depth
30
    ) {
31 82
    }
32
33 82
    public static function fromOrientatedItem(OrientatedItem $orientatedItem, int $x, int $y, int $z): self
34
    {
35 82
        return new self(
36 82
            $orientatedItem->item,
37 82
            $x,
38 82
            $y,
39 82
            $z,
40 82
            $orientatedItem->width,
41 82
            $orientatedItem->length,
42 82
            $orientatedItem->depth,
43 82
        );
44
    }
45
46
    public function jsonSerialize(): array
47
    {
48
        $userValues = [];
49
50
        if ($this->item instanceof JsonSerializable) {
51
            $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
                $userValues = $userSerialisation;
54
            } else {
55
                $userValues = ['extra' => $userSerialisation];
56
            }
57
        }
58
59
        return [
60
            'x' => $this->x,
61
            'y' => $this->y,
62
            'z' => $this->z,
63
            'width' => $this->width,
64
            'length' => $this->length,
65
            'depth' => $this->depth,
66
            'item' => [
67
                ...$userValues,
68
                'description' => $this->item->getDescription(),
69
                'width' => $this->item->getWidth(),
70
                'length' => $this->item->getLength(),
71
                'depth' => $this->item->getDepth(),
72
                'allowedRotation' => $this->item->getAllowedRotation(),
73
            ],
74
        ];
75
    }
76
}
77