PackedLayer::getEndZ()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 0
Metric Value
cc 3
eloc 6
c 0
b 0
f 0
nc 3
nop 0
dl 0
loc 12
ccs 5
cts 6
cp 0.8333
crap 3.0416
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 function max;
13
use function min;
14
15
/**
16
 * A packed layer.
17
 * @internal
18
 */
19
class PackedLayer
20
{
21
    /**
22
     * @var PackedItem[]
23
     */
24
    protected array $items = [];
25
26
    /**
27
     * Add a packed item to this layer.
28 99
     */
29
    public function insert(PackedItem $packedItem): void
30 99
    {
31
        $this->items[] = $packedItem;
32
    }
33
34
    /**
35
     * Get the packed items.
36
     *
37
     * @return PackedItem[]
38 101
     */
39
    public function getItems(): array
40 101
    {
41
        return $this->items;
42
    }
43
44
    /**
45
     * Calculate footprint area of this layer.
46
     *
47
     * @return int mm^2
48 51
     */
49
    public function getFootprint(): int
50 51
    {
51
        return $this->getWidth() * $this->getLength();
52
    }
53 1
54
    public function getStartX(): int
55 1
    {
56
        if (!$this->items) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->items of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
57
            return 0;
58
        }
59 1
60 1
        $values = [];
61 1
        foreach ($this->items as $item) {
62
            $values[] = $item->x;
63
        }
64 1
65
        return min($values);
66
    }
67 99
68
    public function getEndX(): int
69 99
    {
70
        if (!$this->items) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->items of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
71
            return 0;
72
        }
73 99
74 99
        $values = [];
75 99
        foreach ($this->items as $item) {
76
            $values[] = $item->x + $item->width;
77
        }
78 99
79
        return max($values);
80
    }
81 51
82
    public function getWidth(): int
83 51
    {
84
        if (!$this->items) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->items of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
85
            return 0;
86
        }
87 51
88 51
        $start = [];
89 51
        $end = [];
90 51
        foreach ($this->items as $item) {
91 51
            $start[] = $item->x;
92
            $end[] = $item->x + $item->width;
93
        }
94 51
95
        return max($end) - min($start);
96
    }
97 1
98
    public function getStartY(): int
99 1
    {
100
        if (!$this->items) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->items of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
101
            return 0;
102
        }
103 1
104 1
        $values = [];
105 1
        foreach ($this->items as $item) {
106
            $values[] = $item->y;
107
        }
108 1
109
        return min($values);
110
    }
111 99
112
    public function getEndY(): int
113 99
    {
114 97
        if (!$this->items) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->items of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
115
            return 0;
116
        }
117 99
118 99
        $values = [];
119 99
        foreach ($this->items as $item) {
120
            $values[] = $item->y + $item->length;
121
        }
122 99
123
        return max($values);
124
    }
125 51
126
    public function getLength(): int
127 51
    {
128
        if (!$this->items) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->items of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
129
            return 0;
130
        }
131 51
132 51
        $start = [];
133 51
        $end = [];
134 51
        foreach ($this->items as $item) {
135 51
            $start[] = $item->y;
136
            $end[] = $item->y + $item->length;
137
        }
138 51
139
        return max($end) - min($start);
140
    }
141 98
142
    public function getStartZ(): int
143 98
    {
144
        if (!$this->items) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->items of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
145
            return 0;
146
        }
147 98
148 98
        $values = [];
149 98
        foreach ($this->items as $item) {
150
            $values[] = $item->z;
151
        }
152 98
153
        return min($values);
154
    }
155 1
156
    public function getEndZ(): int
157 1
    {
158
        if (!$this->items) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->items of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
159
            return 0;
160
        }
161 1
162 1
        $values = [];
163 1
        foreach ($this->items as $item) {
164
            $values[] = $item->z + $item->depth;
165
        }
166 1
167
        return max($values);
168
    }
169 99
170
    public function getDepth(): int
171 99
    {
172
        if (!$this->items) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->items of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
173
            return 0;
174
        }
175 99
176 99
        $start = [];
177 99
        $end = [];
178 99
        foreach ($this->items as $item) {
179 99
            $start[] = $item->z;
180
            $end[] = $item->z + $item->depth;
181
        }
182 99
183
        return max($end) - min($start);
184
    }
185 1
186
    public function getWeight(): int
187 1
    {
188 1
        $weight = 0;
189 1
        foreach ($this->items as $item) {
190
            $weight += $item->item->getWeight();
191
        }
192 1
193
        return $weight;
194
    }
195 98
196
    public function merge(self $otherLayer): void
197 98
    {
198 25
        foreach ($otherLayer->items as $packedItem) {
199
            $this->items[] = $packedItem;
200
        }
201
    }
202
}
203