Test Failed
Push — 2.x-dev ( 5e90af...eccc16 )
by Doug
03:16
created

BoxList::compare()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 30
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 15
c 1
b 0
f 0
nc 7
nop 2
dl 0
loc 30
ccs 16
cts 16
cp 1
crap 7
rs 8.8333
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
 * List of boxes available to put items into, ordered by volume.
12
 *
13
 * @author Doug Wright
14
 */
15
class BoxList extends \SplMinHeap
16
{
17
    /**
18
     * Compare elements in order to place them correctly in the heap while sifting up.
19
     *
20
     * @see \SplMinHeap::compare()
21
     *
22
     * @param Box $boxA
23
     * @param Box $boxB
24
     *
25
     * @return int
26
     */
27 13
    public function compare($boxA, $boxB)
28
    {
29 13
        $boxAVolume = $boxA->getInnerWidth() * $boxA->getInnerLength() * $boxA->getInnerDepth();
30 13
        $boxBVolume = $boxB->getInnerWidth() * $boxB->getInnerLength() * $boxB->getInnerDepth();
31
32
        // try smallest box first
33 13
        if ($boxBVolume > $boxAVolume) {
34 11
            return 1;
35
        }
36 5
        if ($boxAVolume > $boxBVolume) {
37 1
            return -1;
38
        }
39
40
        // smallest empty weight
41 5
        if ($boxB->getEmptyWeight() > $boxA->getEmptyWeight()) {
42 1
            return 1;
43
        }
44 5
        if ($boxA->getEmptyWeight() > $boxB->getEmptyWeight()) {
45 2
            return -1;
46
        }
47
48
        // maximum weight capacity as fallback decider
49 4
        if (($boxA->getMaxWeight() - $boxA->getEmptyWeight()) > ($boxB->getMaxWeight() - $boxB->getEmptyWeight())) {
50 1
            return -1;
51
        }
52 4
        if (($boxB->getMaxWeight() - $boxB->getEmptyWeight()) > ($boxA->getMaxWeight() - $boxA->getEmptyWeight())) {
53 1
            return 1;
54
        }
55
56 4
        return 0;
57
    }
58
}
59