Passed
Push — 2.x-dev ( 9c54be...ba24d3 )
by Doug
04:59
created

BoxList::compare()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3.009

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 2
dl 0
loc 17
ccs 9
cts 10
cp 0.9
crap 3.009
rs 9.9666
c 0
b 0
f 0
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 9
    public function compare($boxA, $boxB)
28
    {
29 9
        $boxAVolume = $boxA->getInnerWidth() * $boxA->getInnerLength() * $boxA->getInnerDepth();
30 9
        $boxBVolume = $boxB->getInnerWidth() * $boxB->getInnerLength() * $boxB->getInnerDepth();
31
32 9
        $volumeDecider = $boxBVolume - $boxAVolume; // try smallest box first
33 9
        $emptyWeightDecider = $boxA->getEmptyWeight() - $boxB->getEmptyWeight(); // with smallest empty weight
34
35 9
        if ($volumeDecider !== 0) {
36 8
            return $volumeDecider;
37
         }
38 4
        if ($emptyWeightDecider !== 0) {
39
            return $emptyWeightDecider;
40
        }
41
42
        // maximum weight capacity as fallback decider
43 4
        return ($boxB->getMaxWeight() - $boxB->getEmptyWeight()) - ($boxA->getMaxWeight() - $boxA->getEmptyWeight());
44
    }
45
}
46