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

OrientatedItem   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Test Coverage

Coverage 71.88%

Importance

Changes 7
Bugs 3 Features 0
Metric Value
eloc 24
c 7
b 3
f 0
dl 0
loc 108
ccs 23
cts 32
cp 0.7188
rs 10
wmc 12

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getDepth() 0 3 1
A getWidth() 0 3 1
A __construct() 0 10 1
A isSameDimensions() 0 10 2
A getLength() 0 3 1
A getSurfaceFootprint() 0 3 1
A isStable() 0 5 2
A getItem() 0 3 1
A jsonSerialize() 0 7 1
A __toString() 0 3 1
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
use Stringable;
13
14
use function atan;
15
use function min;
16
use function sort;
17
18
/**
19
 * An item to be packed.
20
 */
21
class OrientatedItem implements JsonSerializable, Stringable
22
{
23
    protected readonly int $surfaceFootprint;
24
25
    /**
26
     * @var array<string, bool>
27
     */
28
    protected static array $stabilityCache = [];
29
30
    /**
31
     * @var int[]
32
     */
33
    protected array $dimensionsAsArray;
34
35 82
    public function __construct(
36
        protected Item $item,
37
        protected int $width,
38
        protected int $length,
39
        protected int $depth
40
    ) {
41 82
        $this->surfaceFootprint = $width * $length;
0 ignored issues
show
Bug introduced by
The property surfaceFootprint is declared read-only in DVDoug\BoxPacker\OrientatedItem.
Loading history...
42
43 82
        $this->dimensionsAsArray = [$width, $length, $depth];
44 82
        sort($this->dimensionsAsArray);
45
    }
46
47
    /**
48
     * Item.
49
     */
50 82
    public function getItem(): Item
51
    {
52 82
        return $this->item;
53
    }
54
55
    /**
56
     * Item width in mm in it's packed orientation.
57
     */
58 82
    public function getWidth(): int
59
    {
60 82
        return $this->width;
61
    }
62
63
    /**
64
     * Item length in mm in it's packed orientation.
65
     */
66 82
    public function getLength(): int
67
    {
68 82
        return $this->length;
69
    }
70
71
    /**
72
     * Item depth in mm in it's packed orientation.
73
     */
74 82
    public function getDepth(): int
75
    {
76 82
        return $this->depth;
77
    }
78
79
    /**
80
     * Calculate the surface footprint of the current orientation.
81
     */
82 4
    public function getSurfaceFootprint(): int
83
    {
84 4
        return $this->surfaceFootprint;
85
    }
86
87
    /**
88
     * Is this item stable (low centre of gravity), calculated as if the tipping point is >15 degrees.
89
     *
90
     * N.B. Assumes equal weight distribution.
91
     */
92 82
    public function isStable(): bool
93
    {
94 82
        $cacheKey = $this->width . '|' . $this->length . '|' . $this->depth;
95
96 82
        return static::$stabilityCache[$cacheKey] ?? (static::$stabilityCache[$cacheKey] = atan(min($this->length, $this->width) / ($this->depth ?: 1)) > 0.261);
97
    }
98
99
    /**
100
     * Is the supplied item the same size as this one?
101
     *
102
     * @internal
103
     */
104 78
    public function isSameDimensions(Item $item): bool
105
    {
106 78
        if ($item === $this->item) {
107 50
            return true;
108
        }
109
110 54
        $itemDimensions = [$item->getWidth(), $item->getLength(), $item->getDepth()];
111 54
        sort($itemDimensions);
112
113 54
        return $this->dimensionsAsArray === $itemDimensions;
114
    }
115
116
    public function jsonSerialize(): array
117
    {
118
        return [
119
            'item' => $this->item,
120
            'width' => $this->width,
121
            'length' => $this->length,
122
            'depth' => $this->depth,
123
        ];
124
    }
125
126
    public function __toString(): string
127
    {
128
        return $this->width . '|' . $this->length . '|' . $this->depth;
129
    }
130
}
131