Passed
Pull Request — master (#596)
by
unknown
10:29 queued 08:47
created

OrientatedItem   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Test Coverage

Coverage 59.09%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 19
c 4
b 0
f 0
dl 0
loc 68
ccs 13
cts 22
cp 0.5909
rs 10
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A isSameDimensions() 0 10 2
A isStable() 0 5 2
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
    public 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
        public readonly Item $item,
37
        public readonly int $width,
38
        public readonly int $length,
39
        public readonly 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
     * Is this item stable (low centre of gravity), calculated as if the tipping point is >15 degrees.
49
     *
50
     * N.B. Assumes equal weight distribution.
51
     */
52 82
    public function isStable(): bool
53
    {
54 82
        $cacheKey = $this->width . '|' . $this->length . '|' . $this->depth;
55
56 82
        return static::$stabilityCache[$cacheKey] ?? (static::$stabilityCache[$cacheKey] = atan(min($this->length, $this->width) / ($this->depth ?: 1)) > 0.261);
57
    }
58
59
    /**
60
     * Is the supplied item the same size as this one?
61
     *
62
     * @internal
63
     */
64 78
    public function isSameDimensions(Item $item): bool
65
    {
66 78
        if ($item === $this->item) {
67 50
            return true;
68
        }
69
70 54
        $itemDimensions = [$item->getWidth(), $item->getLength(), $item->getDepth()];
71 54
        sort($itemDimensions);
72
73 54
        return $this->dimensionsAsArray === $itemDimensions;
74
    }
75
76
    public function jsonSerialize(): array
77
    {
78
        return [
79
            'item' => $this->item,
80
            'width' => $this->width,
81
            'length' => $this->length,
82
            'depth' => $this->depth,
83
        ];
84
    }
85
86
    public function __toString(): string
87
    {
88
        return $this->width . '|' . $this->length . '|' . $this->depth;
89
    }
90
}
91