Passed
Push — 2.x-dev ( e5166d...a75e6c )
by Doug
02:18
created

PackedBox::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 9
dl 0
loc 20
ccs 10
cts 10
cp 1
crap 1
rs 9.9666
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
/**
3
 * Box packing (3D bin packing, knapsack problem).
4
 *
5
 * @author Doug Wright
6
 */
7
8
namespace DVDoug\BoxPacker;
9
10
/**
11
 * A "box" with items.
12
 *
13
 * @author Doug Wright
14
 */
15
class PackedBox
16
{
17
    /**
18
     * Box used.
19
     *
20
     * @var Box
21
     */
22
    protected $box;
23
24
    /**
25
     * Items in the box.
26
     *
27
     * @var ItemList
28
     */
29
    protected $items;
30
31
    /**
32
     * Total weight of box.
33
     *
34
     * @var int
35
     */
36
    protected $weight;
37
38
    /**
39
     * Total weight of items in the box.
40
     *
41
     * @var int
42
     */
43
    protected $itemWeight;
44
45
    /**
46
     * Remaining width inside box for another item.
47
     *
48
     * @var int
49
     */
50
    protected $remainingWidth;
51
52
    /**
53
     * Remaining length inside box for another item.
54
     *
55
     * @var int
56
     */
57
    protected $remainingLength;
58
59
    /**
60
     * Remaining depth inside box for another item.
61
     *
62
     * @var int
63
     */
64
    protected $remainingDepth;
65
66
    /**
67
     * Remaining weight inside box for another item.
68
     *
69
     * @var int
70
     */
71
    protected $remainingWeight;
72
73
    /**
74
     * Used width inside box for packing items.
75
     *
76
     * @var int
77
     */
78
    protected $usedWidth;
79
80
    /**
81
     * Used length inside box for packing items.
82
     *
83
     * @var int
84
     */
85
    protected $usedLength;
86
87
    /**
88
     * Used depth inside box for packing items.
89
     *
90
     * @var int
91
     */
92
    protected $usedDepth;
93
94
    /**
95
     * Get box used.
96
     *
97
     * @return Box
98
     */
99 6
    public function getBox()
100
    {
101 6
        return $this->box;
102
    }
103
104
    /**
105
     * Get items packed.
106
     *
107
     * @return ItemList
108
     */
109 21
    public function getItems()
110
    {
111 21
        return $this->items;
112
    }
113
114
    /**
115
     * Get packed weight.
116
     *
117
     * @return int weight in grams
118
     */
119 2
    public function getWeight()
120
    {
121 2
        return $this->box->getEmptyWeight() + $this->getItemWeight();
122
    }
123
124
    /**
125
     * Get packed weight of the items only.
126
     *
127
     * @return int weight in grams
128
     */
129 3
    public function getItemWeight()
130
    {
131 3
        if (!is_null($this->itemWeight)) {
0 ignored issues
show
introduced by
The condition is_null($this->itemWeight) is always false.
Loading history...
132 1
            return $this->itemWeight;
133
        }
134 3
        $this->itemWeight = 0;
135
        /** @var Item $item */
136 3
        foreach (clone $this->items as $item) {
137 3
            $this->itemWeight += $item->getWeight();
138
        }
139
140 3
        return $this->itemWeight;
141
    }
142
143
    /**
144
     * Get remaining width inside box for another item.
145
     *
146
     * @return int
147
     */
148 1
    public function getRemainingWidth()
149
    {
150 1
        return $this->remainingWidth;
151
    }
152
153
    /**
154
     * Get remaining length inside box for another item.
155
     *
156
     * @return int
157
     */
158 1
    public function getRemainingLength()
159
    {
160 1
        return $this->remainingLength;
161
    }
162
163
    /**
164
     * Get remaining depth inside box for another item.
165
     *
166
     * @return int
167
     */
168 1
    public function getRemainingDepth()
169
    {
170 1
        return $this->remainingDepth;
171
    }
172
173
    /**
174
     * Used width inside box for packing items.
175
     *
176
     * @return int
177
     */
178
    public function getUsedWidth()
179
    {
180
        return $this->usedWidth;
181
    }
182
183
    /**
184
     * Used length inside box for packing items.
185
     *
186
     * @return int
187
     */
188
    public function getUsedLength()
189
    {
190
        return $this->usedLength;
191
    }
192
193
    /**
194
     * Used depth inside box for packing items.
195
     *
196
     * @return int
197
     */
198
    public function getUsedDepth()
199
    {
200
        return $this->usedDepth;
201
    }
202
203
    /**
204
     * Get remaining weight inside box for another item.
205
     *
206
     * @return int
207
     */
208 1
    public function getRemainingWeight()
209
    {
210 1
        return $this->remainingWeight;
211
    }
212
213
    /**
214
     * @return int
215
     */
216 2
    public function getInnerVolume()
217
    {
218 2
        return $this->box->getInnerWidth() * $this->box->getInnerLength() * $this->box->getInnerDepth();
219
    }
220
221
    /**
222
     * Get used volume of the packed box.
223
     *
224
     * @return int
225
     */
226 2
    public function getUsedVolume()
227
    {
228 2
        $volume = 0;
229
        /** @var PackedItem $item */
230 2
        foreach (clone $this->items as $item) {
231 2
            $volume += ($item->getWidth() * $item->getLength() * $item->getDepth());
232
        }
233
234 2
        return $volume;
235
    }
236
237
    /**
238
     * Get unused volume of the packed box.
239
     *
240
     * @return int
241
     */
242 1
    public function getUnusedVolume()
243
    {
244 1
        return $this->getInnerVolume() - $this->getUsedVolume();
245
    }
246
247
    /**
248
     * Get volume utilisation of the packed box.
249
     *
250
     * @return float
251
     */
252 2
    public function getVolumeUtilisation()
253
    {
254 2
        $itemVolume = 0;
255
256
        /** @var Item $item */
257 2
        foreach (clone $this->items as $item) {
258 2
            $itemVolume += $item->getVolume();
259
        }
260
261 2
        return round($itemVolume / $this->box->getInnerVolume() * 100, 1);
262
    }
263
264
    /**
265
     * Legacy constructor.
266
     *
267
     * @deprecated
268
     *
269
     * @param Box      $box
270
     * @param ItemList $itemList
271
     * @param int      $remainingWidth
272
     * @param int      $remainingLength
273
     * @param int      $remainingDepth
274
     * @param int      $remainingWeight
275
     * @param int      $usedWidth
276
     * @param int      $usedLength
277
     * @param int      $usedDepth
278
     */
279 23
    public function __construct(
280
        Box $box,
281
        ItemList $itemList,
282
        $remainingWidth,
283
        $remainingLength,
284
        $remainingDepth,
285
        $remainingWeight,
286
        $usedWidth,
287
        $usedLength,
288
        $usedDepth
289
    ) {
290 23
        $this->box = $box;
291 23
        $this->items = $itemList;
292 23
        $this->remainingWidth = $remainingWidth;
293 23
        $this->remainingLength = $remainingLength;
294 23
        $this->remainingDepth = $remainingDepth;
295 23
        $this->remainingWeight = $remainingWeight;
296 23
        $this->usedWidth = $usedWidth;
297 23
        $this->usedLength = $usedLength;
298 23
        $this->usedDepth = $usedDepth;
299 23
    }
300
301
    /**
302
     * The constructor from v3.
303
     *
304
     * @param Box            $box
305
     * @param PackedItemList $packedItems
306
     *
307
     * @return self
308
     */
309 23
    public static function fromPackedItemList(Box $box, PackedItemList $packedItems)
310
    {
311 23
        $maxWidth = $maxLength = $maxDepth = $weight = 0;
312
        /** @var PackedItem $item */
313 23
        foreach (clone $packedItems as $item) {
314 23
            $maxWidth = max($maxWidth, $item->getX() + $item->getWidth());
315 23
            $maxLength = max($maxLength, $item->getY() + $item->getLength());
316 23
            $maxDepth = max($maxDepth, $item->getZ() + $item->getDepth());
317 23
            $weight += $item->getItem()->getWeight();
318
        }
319
320 23
        $packedBox = new self(
321 23
            $box,
322 23
            $packedItems->asItemList(),
323 23
            $box->getInnerWidth() - $maxWidth,
324 23
            $box->getInnerLength() - $maxLength,
325 23
            $box->getInnerDepth() - $maxDepth,
326 23
            $box->getMaxWeight() - $box->getEmptyWeight() - $weight,
327 23
            $maxWidth,
328 23
            $maxLength,
329 23
            $maxDepth
330
        );
331
332 23
        return $packedBox;
333
    }
334
}
335