PackedItem::__construct()   A
last analyzed

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
/**
4
 * Box packing (3D bin packing, knapsack problem).
5
 *
6
 * @author Doug Wright
7
 */
8
declare(strict_types=1);
9
10
namespace DVDoug\BoxPacker;
11
12
use JsonSerializable;
13
14
use function is_iterable;
15
16
/**
17
 * A packed item.
18
 */
19
readonly class PackedItem implements JsonSerializable
20
{
21
    public int $volume;
22 102
23
    public function __construct(
24
        public Item $item,
25
        public int $x,
26
        public int $y,
27
        public int $z,
28
        public int $width,
29
        public int $length,
30
        public int $depth
31 102
    ) {
32
    }
33 98
34
    public static function fromOrientatedItem(OrientatedItem $orientatedItem, int $x, int $y, int $z): self
35 98
    {
36 98
        return new self(
37 98
            $orientatedItem->item,
38 98
            $x,
39 98
            $y,
40 98
            $z,
41 98
            $orientatedItem->width,
42 98
            $orientatedItem->length,
43 98
            $orientatedItem->depth,
44
        );
45
    }
46 3
47
    public function jsonSerialize(): array
48 3
    {
49
        $userValues = [];
50 3
51 2
        if ($this->item instanceof JsonSerializable) {
52 2
            $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

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